Solved

Populate Combo Boxes

Posted on 2000-03-17
10
297 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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 …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

752 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