Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 518
  • Last Modified:

Adding values from a multiselect listbox?

I’m doing a self study in Visual basic. I’m currently working on exercise 13.3.2 of ‘An Introduction to Programming using Visual Basic 6 by David I. Schneider’ with a few adjustments!!

The exercise is about a Fast Food Restaurant.

Here’s what I’ve done so far:

First I created a database in Access called FOCS, I have two tables called MenuItems & DrinkItems  –

MenuItems
--------------
ItemNo (PK)
Item (Text)
Price (Currency)

DrinkItems
--------------
DrinkNo (PK)
DrinkItem (Text)
DrinkPrice (Currency)

Next, I created a form in Visual Basic 6. I have two ADODC controls with connections to FOCS; ADOCMenu & ADOCDrinks.

I placed two DataList boxes onto the form; DataList1 & DataList2

DataList1 source is ADOCMenu & DataList2 source is ADOCDrinks.

Ok, this work perfectly fine. I can display my Food & Drinks Items in DataList1 & DataList2.

Next I created a listbox (lstOrder) for my orders and cmdButtons (button >>> & button <<<)

I want to multi-select items from DataList1 & DataList2 and display these items in lsdOrder – the code I’ve used is:

Private Sub cmdMoveItem_Click()

adoMenu.Recordset.MoveFirst

Do While adoMenu.Recordset.Fields("Items") <> DataList1.Text
adoMenu.Recordset.MoveNext
Loop

lstOrder.AddItem adoMenu.Recordset.Fields("Items") & " " & FormatCurrency(adoMenu.Recordset.Fields("Price"))

End Sub

Ok, so far, all is well. I can multi-select Food & Drink items, click >>> or <<< Button’s and the items and price are then displayed in lstOrder box.

The next pit is where I’m stuck…….I’m tying to create a Total Cost (txtTotalCost) of all the items and price displayed in lstOrder……….can any help??
0
irwells
Asked:
irwells
3 Solutions
 
basicinstinctCommented:
good book - i learned to program from that book

what you need to do is write a loop to go throgh all of the items in lstOrder and add each price to a variable, a bit like this:

Private Sub AddOrder()
    Dim i, iTotal As Integer
    For i = 0 To lstOrder.ListCount - 1
        iTotal = iTotal + lstOrder.List(i)
    Next
    MsgBox iTotal
End Sub
0
 
alainbrydenCommented:
you could even add another label to the form that displays the running total. Turn his sub into a function

Private Function AddOrder() As Integer
...
...
...
     AddOrder = iTotal
End Function

you can have the following.

Private Sub lstOrder_Change() 'This will execute every time it's changed
    lblTotal = AddOrder
    refresh lblTotal
End Sub

since the AddOrder function does not take long to execute, it will be a neat feature and you can watch the total add up in real time as it's being entered. Then when you want to store it just say

..... .Total = val(lblTotal)

And it's yours for the taking


Have fun!

Alain
0
 
AHMKC1Commented:
You call following function at your cmdMoveItem_Click


Private Function CalculatePrice()
   
    Dim Rs As Recordset, I, TotalPrice As Integer
   
    For I = 0 To lstOrder.ListCount - 1
    Set Rs = New Recordset
    Rs.open "Select Price  From MenuItems Where Item  & ' ' & Price='" & lstOrder.List(I) & "'", Cnn, OpenStatic
    '******* Cnn is connection string
    If Rs.Recordcount <> 0 Then
        TotalPrice = TotalPrice + Rs!Price
    Else
        Set Rs = Nothing
        Set Rs = New Recordset
        Rs.open "Select DrinkPrice  From MenuItems Where Item  & ' ' & DrinkPrice='" & lstOrder.List(I) & "'", Cnn, OpenStatic
        If Rs.Recordcount <> 0 Then
            TotalPrice = TotalPrice + Rs!DrinkPrice
        End If

    End If
        Set Rs = Nothing
    Next
   
   
   txtTotalCost.Text = TotalPrice


End Function


thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now