Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Populate Combo Boxes

Posted on 2000-03-17
10
Medium Priority
?
301 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 300 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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 70

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

926 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