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

x
?
Solved

Populate Combo Boxes

Posted on 2000-03-17
10
Medium Priority
?
300 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

715 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