Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding values from a multiselect listbox?

Posted on 2006-06-16
5
Medium Priority
?
517 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 672 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 664 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 664 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Introduction to Processes

722 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