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
Solved

Populate Combo Boxes

Posted on 2000-03-17
10
295 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB error "Type mismatch" 2 60
TT Auto Dashboard 13 95
how to know if my Checkbox is True in VB6.0? 9 60
VBA: loop recent folder and copy txt file. 8 34
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

829 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