Solved

Populating a Combo Box

Posted on 2004-09-01
6
196 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:Mike Miller
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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:Mike Miller
ID: 11957522
Sweet. Thanks guys, I see what I was doing wrong now. THANKS!!!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…

828 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