Solved

Adding values from a multiselect listbox?

Posted on 2006-06-16
5
513 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

786 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