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

Recommended Answers

All 11 Replies

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. :-)

my problem is that I have to hit a save button everytime I put in an order and the dat file is updated, this part works fine. My problem is that I don't know how to take those value and put them into a variable so when I hit the calculate button the total of the order should show.

Kat

Sure you do. Don't make it harder than it is...

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!

I had the program allowing the user multiple choices of items but the instructor did not want us to do that. I give this a try. Thanks for the advice.

kat

okay I have it adding the totals per item but how do I now take three different item totals and add them.

For example I have the lblTotal.caption equaling the intTotal but that's just for one item. The order actually has three items.

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:

' 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..

its a visual basic class. I do alright in it if I can "see" what the code is supposed to do. The instructor is not very helpful. But thanks for the help. I will work on this and let you know.

Kat

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.

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

I'm a little confused. I haven't looked at VB in a a bunch of years, but that code you just posted looks right? The only help I can offer is to DIM your global variables in Option Explicit. It seems you have them all declared upon hitting the "Save" button. This resets the value of curOrderTotal back to 0 each time the Save button is pressed. (So essentially you'er saying curOrderTotal = 0 + curTotal each time, which is why it's not accumulating.

I THINK, ANYWAYS! I haven't looked at VB since 10th grade and I'm now a college senior!

Dani is write, but I would stay away from Global variables. Nasty, and prof's don't usually like seeing them. IMHO and Experience.

But in saying that, maybe you need to create a global variable called curTotal, and have it accumulate each time you process and item. Or have it stay in the save button, but recall it and store it to the file each time you add an item.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.