| | |
adding totals in listboxes
![]() |
•
•
Join Date: Nov 2003
Posts: 11
Reputation:
Solved Threads: 0
Hi there this is my first time here. I am working on a problem for school. I am working hard on solving a problem and hope someone can help me.
I am making an order form. I have the form all done. It asks for the name, address etc....
Next I made three listboxes: one with products by name, one by Product ID number, and one by product price. When the user clicks the save button I have the customer info go to one file and the order info to another. But I know I am doing something wrong.
On my form I also have a total for the order. My problem is that I can write the info to the files but I don't know how to add the total from the price listbox. Especiall since everytime I hit the save command button the listboxes are clearing. here is the code I have so far.
I don't want my homework done for me just a push or example in the right direction would be great.
Option Explicit
Private Sub cmdSaveFile_Click()
Dim strProductId As String
Dim strProductName As String
Dim curUnitPrice As Currency
Dim intQuantity As Integer
Dim strFname As String
Dim strLname As String
Dim strPaymentCode As String
Dim strCreditCode As String
Dim strCreditCardNumber As String
Dim strAddress As String
Dim strCity As String
Dim strState As String
Dim strZip As String
Dim Number As Integer
If lstProductId.ListIndex = -1 Then
MsgBox "Please Pick an item to order"
Exit Sub
End If
If vscQuantity.Value = 0 Then
MsgBox "Please pick a quantity"
Exit Sub
End If
Open "A:/P_SCSTMR.DAT" For Append As #20
strFname = txtFname.Text
strLname = txtLname.Text
If optPaymentCode(0) = True Then
strPaymentCode = 0
Else
strPaymentCode = 1
End If
If optCode(0) = True Then
strCreditCode = "V"
End If
If optCode(1) = True Then
strCreditCode = "M"
End If
If optCode(2) = True Then
strCreditCode = "N"
End If
If lblCreditNumber.Caption = "" Then
strCreditCardNumber = "X"
Else
strCreditCardNumber = lblCreditNumber.Caption
End If
strAddress = txtAddress.Text
strCity = txtCity.Text
strState = txtState.Text
strZip = txtZip.Text
Write #20, strFname, strLname, strPaymentCode, strCreditCode, strCreditCardNumber, strAddress, strCity, strState, strZip, Number
Close #20
Open "A:/P_SORDRS.DAT" For Append As #10
strProductId = lstProductId.List(lstProductId.ListIndex)
strProductName = lstProductName.List(lstProductId.ListIndex)
curUnitPrice = CCur(lstPrice.List(lstProductId.ListIndex))
intQuantity = vscQuantity.Value
Write #10, strProductId, strProductName, curUnitPrice, intQuantity
Close #10
lstProductId.ListIndex = -1
vscQuantity.Value = 0
End Sub
Private Sub Form_Load()
Dim strProductId As String
Dim strProductName As String
Dim curUnitPrice As Currency
Dim intRecordnum As Integer
intRecordnum = FreeFile
Open "A:/P_SPRDCT.DAT" For Input As #intRecordnum
Do While Not EOF(intRecordnum)
Input #intRecordnum, strProductId, strProductName, curUnitPrice
lstProductId.AddItem strProductId
lstProductName.AddItem strProductName
lstPrice.AddItem curUnitPrice
Loop
Close #intRecordnum
Open "a:/P_SCSTMR.DAT" For Output As #1
Close #1
Open "a:/P_SORDRS.DAT" For Output As #2
Close #2
End Sub
Private Sub VScroll1_Change()
lblAmount.Caption = VScroll1.Value
End Sub
Private Sub lstPrice_Click()
lstPrice.ListIndex = lstProductName.ListIndex
End Sub
Private Sub lstProductId_Click()
lstProductName.ListIndex = lstProductId.ListIndex
lstPrice.ListIndex = lstProductId.ListIndex
End Sub
Private Sub lstProductName_Click()
lstProductId.ListIndex = lstProductName.ListIndex
lstPrice.ListIndex = lstProductName.ListIndex
End Sub
Private Sub optPaymentCode_Click(Index As Integer)
If optPaymentCode(1) = True Then
lblCreditNumber.Caption = InputBox("Please enter your credit card number")
lblMonth.Caption = InputBox("Enter your card's experation month (ie..00)")
lblYear.Caption = InputBox("Enter your card's experation year(ie. 03)")
End If
End Sub
Private Sub vscQuantity_Change()
lblQuantity.Caption = vscQuantity.Value
End Sub
I am making an order form. I have the form all done. It asks for the name, address etc....
Next I made three listboxes: one with products by name, one by Product ID number, and one by product price. When the user clicks the save button I have the customer info go to one file and the order info to another. But I know I am doing something wrong.
On my form I also have a total for the order. My problem is that I can write the info to the files but I don't know how to add the total from the price listbox. Especiall since everytime I hit the save command button the listboxes are clearing. here is the code I have so far.
I don't want my homework done for me just a push or example in the right direction would be great.
Option Explicit
Private Sub cmdSaveFile_Click()
Dim strProductId As String
Dim strProductName As String
Dim curUnitPrice As Currency
Dim intQuantity As Integer
Dim strFname As String
Dim strLname As String
Dim strPaymentCode As String
Dim strCreditCode As String
Dim strCreditCardNumber As String
Dim strAddress As String
Dim strCity As String
Dim strState As String
Dim strZip As String
Dim Number As Integer
If lstProductId.ListIndex = -1 Then
MsgBox "Please Pick an item to order"
Exit Sub
End If
If vscQuantity.Value = 0 Then
MsgBox "Please pick a quantity"
Exit Sub
End If
Open "A:/P_SCSTMR.DAT" For Append As #20
strFname = txtFname.Text
strLname = txtLname.Text
If optPaymentCode(0) = True Then
strPaymentCode = 0
Else
strPaymentCode = 1
End If
If optCode(0) = True Then
strCreditCode = "V"
End If
If optCode(1) = True Then
strCreditCode = "M"
End If
If optCode(2) = True Then
strCreditCode = "N"
End If
If lblCreditNumber.Caption = "" Then
strCreditCardNumber = "X"
Else
strCreditCardNumber = lblCreditNumber.Caption
End If
strAddress = txtAddress.Text
strCity = txtCity.Text
strState = txtState.Text
strZip = txtZip.Text
Write #20, strFname, strLname, strPaymentCode, strCreditCode, strCreditCardNumber, strAddress, strCity, strState, strZip, Number
Close #20
Open "A:/P_SORDRS.DAT" For Append As #10
strProductId = lstProductId.List(lstProductId.ListIndex)
strProductName = lstProductName.List(lstProductId.ListIndex)
curUnitPrice = CCur(lstPrice.List(lstProductId.ListIndex))
intQuantity = vscQuantity.Value
Write #10, strProductId, strProductName, curUnitPrice, intQuantity
Close #10
lstProductId.ListIndex = -1
vscQuantity.Value = 0
End Sub
Private Sub Form_Load()
Dim strProductId As String
Dim strProductName As String
Dim curUnitPrice As Currency
Dim intRecordnum As Integer
intRecordnum = FreeFile
Open "A:/P_SPRDCT.DAT" For Input As #intRecordnum
Do While Not EOF(intRecordnum)
Input #intRecordnum, strProductId, strProductName, curUnitPrice
lstProductId.AddItem strProductId
lstProductName.AddItem strProductName
lstPrice.AddItem curUnitPrice
Loop
Close #intRecordnum
Open "a:/P_SCSTMR.DAT" For Output As #1
Close #1
Open "a:/P_SORDRS.DAT" For Output As #2
Close #2
End Sub
Private Sub VScroll1_Change()
lblAmount.Caption = VScroll1.Value
End Sub
Private Sub lstPrice_Click()
lstPrice.ListIndex = lstProductName.ListIndex
End Sub
Private Sub lstProductId_Click()
lstProductName.ListIndex = lstProductId.ListIndex
lstPrice.ListIndex = lstProductId.ListIndex
End Sub
Private Sub lstProductName_Click()
lstProductId.ListIndex = lstProductName.ListIndex
lstPrice.ListIndex = lstProductName.ListIndex
End Sub
Private Sub optPaymentCode_Click(Index As Integer)
If optPaymentCode(1) = True Then
lblCreditNumber.Caption = InputBox("Please enter your credit card number")
lblMonth.Caption = InputBox("Enter your card's experation month (ie..00)")
lblYear.Caption = InputBox("Enter your card's experation year(ie. 03)")
End If
End Sub
Private Sub vscQuantity_Change()
lblQuantity.Caption = vscQuantity.Value
End Sub
Your code seems to be incomplete, as there are end subs with no "subroutine starts"... but besides that, why not look through the values in the listbox and total them up into a variable as you need to or even total them up at the end. Which ever is easier. A For loop would do the job...
Hope this nudge works. And if you give a more detailed batch of code I could point you into to good coding practice routes, as well as some other ideas... if you so like. :-)
Hope this nudge works. And if you give a more detailed batch of code I could point you into to good coding practice routes, as well as some other ideas... if you so like. :-)
Sure you do. Don't make it harder than it is...
Looking over the code in the Save_Click, you have the above. Where you grab the ID, ProductName, UnitPrice, and Quantity. Well totals are Quantity * UnitPrice. Are they not? So just create a new variable and store an accumulating total to be written when Save is clicked.
i.e. intTotal = CInt(curUnitPrice) * intQuantity
But here is a couple of things to consider. From the code (although I ran through it rather quickly) you have provided you seem to allow only one item selection. What if the customer wants to select more than one item. And then for doing that total, just use the For Loop like I suggested to move through the listbox accumulating totals.
i.e. For ......
intTotal = inTotal + ((lstPrice.List(lstProductId.ListIndex) * vscQuantity.Value
Next intIndex
Or something to that effect.
Other piece of advice, take it for what it is worth, it is allways simplier to store values as the same data type. i.e. Integers, rather than currency and integer and double data types. You can easily convert them in code. Just my practice....
Hope this helps you in the right direction.!
p.s. I know the problem you are working on, as I have had to do it in C++ (command line program even) but we had to also validate the products (which were books) by their ISBN numbers!
•
•
•
•
strProductId = lstProductId.List(lstProductId.ListIndex)
strProductName = lstProductName.List(lstProductId.ListIndex)
curUnitPrice = CCur(lstPrice.List(lstProductId.ListIndex))
intQuantity = vscQuantity.Value
Looking over the code in the Save_Click, you have the above. Where you grab the ID, ProductName, UnitPrice, and Quantity. Well totals are Quantity * UnitPrice. Are they not? So just create a new variable and store an accumulating total to be written when Save is clicked.
i.e. intTotal = CInt(curUnitPrice) * intQuantity
But here is a couple of things to consider. From the code (although I ran through it rather quickly) you have provided you seem to allow only one item selection. What if the customer wants to select more than one item. And then for doing that total, just use the For Loop like I suggested to move through the listbox accumulating totals.
i.e. For ......
intTotal = inTotal + ((lstPrice.List(lstProductId.ListIndex) * vscQuantity.Value
Next intIndex
Or something to that effect.
Other piece of advice, take it for what it is worth, it is allways simplier to store values as the same data type. i.e. Integers, rather than currency and integer and double data types. You can easily convert them in code. Just my practice....
Hope this helps you in the right direction.!
p.s. I know the problem you are working on, as I have had to do it in C++ (command line program even) but we had to also validate the products (which were books) by their ISBN numbers!
Last edited by Paladine; Nov 6th, 2003 at 12:32 pm.
Hey Kat, I am glad the advice is helping, but I am still confused as to why you are unable to add the totals of each item.
IntGrandTotal = intGrandTotal + intTotal
How is it that this won't work?
Or better yet, why not use an Array. Store each item total in the array, then when the sale is completed (or save is hit) just cycle through the array to total up the item costs/totals.
By the way are you doing this in Visual Basic or VBA (Access)?
Example for the array:
Hope this helps..
IntGrandTotal = intGrandTotal + intTotal
How is it that this won't work?
Or better yet, why not use an Array. Store each item total in the array, then when the sale is completed (or save is hit) just cycle through the array to total up the item costs/totals.
By the way are you doing this in Visual Basic or VBA (Access)?
Example for the array:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
' Maximum Number of Items to be able to purchase is 10, because the are only ten items in the store or to be sold. Dim adblTotals(1 to 10) as Double ' You array Dim intItemsSelected as Integer ' Incremented for each item Selected Dim intCount as Integer Dim dblGrandTotal as Double ' Fill array - Let you do this code 'Add up totals on sale commit by cycling back through the array For intCount = 1 to intItemsSelected dblGrandTotal = dblGrandTotal + adblTotal(inCount) Next intCount
Hope this helps..
No problem Kat. That is the great thing about this forum, we are here to help.
Let me know if you need some more help, I love this language, and love teaching, so don't be affraid to ask anything.
I am the same way, if I can't understand where to code is going it is hard to wrap my head around it.
Good luck.
Let me know if you need some more help, I love this language, and love teaching, so don't be affraid to ask anything.
I am the same way, if I can't understand where to code is going it is hard to wrap my head around it.
Good luck.
•
•
Join Date: Nov 2003
Posts: 11
Reputation:
Solved Threads: 0
Well after a few long days of thought and work I have the counter working. I also have the total of the single product ordered working.
Here is the code I have
curTotal = CInt(curUnitPrice) * intQuantity
curOrderTotal = curOrderTotal + curTotal
lblMerchandiseTotal.Caption = curOrderTotal
But this only gives me the total of one item. Each time I enter a new item and hit the save button (instructor wants it this way) I get a new total. How do I make curOrderTotal equal the entire order and not just one product?
Kat
Here is the code I have
curTotal = CInt(curUnitPrice) * intQuantity
curOrderTotal = curOrderTotal + curTotal
lblMerchandiseTotal.Caption = curOrderTotal
But this only gives me the total of one item. Each time I enter a new item and hit the save button (instructor wants it this way) I get a new total. How do I make curOrderTotal equal the entire order and not just one product?
Kat
![]() |
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: multiform windows appliction in VB . NET
- Next Thread: Building D.L.L in Matlab
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





