Solved

Adding values from a multiselect listbox?

Posted on 2006-06-16
5
516 Views
Last Modified: 2008-02-01
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
Comment
Question by:irwells
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 23

Accepted Solution

by:
basicinstinct earned 168 total points
ID: 16924457
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
 
LVL 20

Assisted Solution

by:alainbryden
alainbryden earned 166 total points
ID: 16925438
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
 
LVL 6

Assisted Solution

by:AHMKC1
AHMKC1 earned 166 total points
ID: 16951226
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Make the most of your online learning experience.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question