Solved

Populate Combo Boxes

Posted on 2000-03-17
10
292 Views
Last Modified: 2010-05-02
I am fairly new to VB6, but have done a lot with VBA.  My confusion here lies in populating combo boxes.  I need to populate a combo box, allow for adds and removals.  Whatever is selected, a query runs to pick out select the records that meet teh criteria for the selection.  These are returned in a list box.  From there, I need a multi select list box.

Can you help?
0
Comment
Question by:ambulance
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2629695
the combobox supports both the AddItem method and the RemoveItem method.  I'm not quite clear on how you want this implemented however
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2629702
Please explain me more about your problem.
0
 
LVL 4

Accepted Solution

by:
wileecoy earned 100 total points
ID: 2630216
Below is an example I put together.

Add the code where indicated then press F5.  When the form displays, select an order number from the combo box.

You will need to add a reference to Microsofts DAO 2.5/3.51 Compatibility Library.

This example takes the Orders and Order Details tables in the NWIND.MDB sample database that comes w/ VB 6 and populates the combo box with the order numbers and, when selected, enters the order detail in the listbox.

'Enter Code Below:

'In the General Declarations of the main form (Form1)
' Add a ComboBox, "Combo1"
' Add a ListBox, "List1"


Dim sSampleDataPath As String
Dim db As Database
Dim rsMain As Recordset
Dim rsDetail As Recordset


Private Sub Combo1_Click()

If Combo1.Text <> "Select an Order" Then
    List1.Clear
    Set rsDetail = db.OpenRecordset("Select * From [Order Details] Where [OrderID] = " & Left(Combo1.Text, 5))
    If rsDetail.RecordCount > 0 Then
        With rsDetail
            List1.AddItem (![OrderID] & Space(10 - (Len(![OrderID]))) & Format(![UnitPrice], "$#,##0.00") & Space(10 - (Len(![UnitPrice]))) & "Qty - " & ![Quantity])
        End With
    End If
End If
End Sub

Private Sub Form_Load()
    'Enter your path for NWIND.MDB below
    sSampleDataPath = "D:\Program Files\Microsoft Visual Studio\vb98"
   
    Set db = DBEngine.Workspaces(0).OpenDatabase(sSampleDataPath & "\Nwind.mdb")
    Set rsMain = db.OpenRecordset("Select [OrderID], [CustomerID] from Orders")
   
    With rsMain
        Do While Not .EOF
            Combo1.AddItem rsMain![OrderID] & " " & ![CustomerID]
            .MoveNext
        Loop
    End With
    Combo1.Text = "Select an Order"
End Sub
0
 
LVL 1

Expert Comment

by:vsankarv
ID: 2630438
please explain the q more clearly.
0
 

Author Comment

by:ambulance
ID: 2641154
I entered the code as written above...copy/paste method.  And came up with a "User-defined type not defined" compile error.  The highlighted text was in the form_load event, dim db as database.

This happens when this is declares as a local & global variable.  

Any way around that?  How do I fix it to see if this code works?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 28

Expert Comment

by:AzraSound
ID: 2641259
the dim statements should not be in the form load event.  they should be at the beginning of the code where it says option explicit
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2641488
Have you set your reference to DAO as wileecoy said?
0
 
LVL 4

Expert Comment

by:wileecoy
ID: 2643121
emoreau is correct.  you need to set the DAO reference.

Go to Project, References - then scroll through the list box of options for:

Microsofts DAO 2.5/3.51 Compatibility Library

Select the check box to the left and select OK.

Then the code should work as long as you also added the combobox (combo1) and listbox (list1).

Let me know if this works.

0
 

Author Comment

by:ambulance
ID: 2644932
This did work.  I did set the references and it works.  Thank you very much.

-A
0
 
LVL 4

Expert Comment

by:wileecoy
ID: 2657533
Cool - Glad it worked.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

15 Experts available now in Live!

Get 1:1 Help Now