Solved

Populating a Combo Box

Posted on 2004-09-01
6
195 Views
Last Modified: 2010-05-02
I have an SQL DB with a table named EmployeeData. It has a column named EmployeeName.

I have a form with a drop down called cmbName

When the form loads I want to populate the drop down in alphabetic order with the data from the EmployeeName column.
0
Comment
Question by:mwmiller78
  • 3
  • 2
6 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 11957351
Add a reference to the ActiveX Data Object libraries. The add code something like:    

    Dim oConn As New ADODB.Connection
    Dim oRS As ADODB.Recordset

    oConn.Open "Your connection string here"
    Set oRS = oConn.Execute("SELECT EmployeeName FROM EmployeeData ORDER BY EmployeeName")

    oRS.MoveFirst

    If Not oRS.BOF And Not oRS.EOF Then
        While Not oRS.EOF
            Combo1.AddItem oRS("EmployeeName")
            oRS.MoveNext
        Wend
    End If

    oConn.Close

    Set oRS = Nothing
    Set oConn = Nothing

Hope this helps.
0
 
LVL 6

Expert Comment

by:bkthompson2112
ID: 11957358
Hi mwmiller78,

this should do it:

  Dim sSQL As String
  sSQL = "SELECT EmployeName FROM EmployeeData ORDER BY EmployeeName"
         
  Dim rs As ADODB.Recordset
  Set rs = New ADODB.Recordset

  rs.Open sSQL, Conn, adOpenForwardOnly, adLockReadOnly, adCmdText

  Do While rs.EOF = False
    cmbName.AddItem rs.Fields("EmployeeName").Value & ""
    rs.MoveNext
  Loop

  rs.Close
  Set rs = Nothing

bkt
0
 
LVL 6

Expert Comment

by:bkthompson2112
ID: 11957367
carl_tawn's got it.
(i left the connection object out of mine, sorry)
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 11957400
Great minds think alike :o)
0
 
LVL 6

Expert Comment

by:bkthompson2112
ID: 11957442
Yep
0
 

Author Comment

by:mwmiller78
ID: 11957522
Sweet. Thanks guys, I see what I was doing wrong now. THANKS!!!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

786 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