Solved

Adding values from a multiselect listbox?

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
fix34  challenge 9 98
Reading variable length EBCDIC in SAS 9 79
sumHeights2  challenge 7 78
firstChar challenge 13 86
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now