Solved

Connecting an access database to a combobox using Visual Basic 6.0

Posted on 2004-04-29
5
171 Views
Last Modified: 2013-11-25
Hi,

 I would like to know how to establish a connection between access and visual basic using a combobox in vb. I need to pull all states from my STATE table and then my second combobox needs to be populated with the counties that belong to that state.

Thanks
Andre
0
Comment
Question by:virgilar
[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
  • 2
5 Comments
 
LVL 6

Expert Comment

by:LeeHenry
ID: 10954574
0
 
LVL 19

Accepted Solution

by:
Shauli earned 125 total points
ID: 10954739
assuming you have a database with two tables: table states with list of states and table counties with two columns = state and county, then the code below will do it. If your database is different, then post here how it is built in terms of tables and fields.


'======in form declaration area
Option Explicit
Dim myConn As ADODB.Connection
Dim myRec As ADODB.Recordset
Dim sqlString As String

'======load the counties to combo2
Private Sub Combo1_Click()
sqlString = "SELECT * FROM counties WHERE county='" & Combo1.Text & "' ORDER By county"
Combo2.Clear
myRec.Open sqlString, myConn, adOpenKeyset, adLockReadOnly
    With myRec
        Do Until .EOF
            Combo2.AddItem !county
            .MoveNext
        Loop
    End With
myRec.Close
End Sub

'=======form load event. open connection and load states to combo1
Private Sub Form_Load()

Set myConn = New ADODB.Connection
Set myRec = New ADODB.Recordset
Combo1.Clear
myConn.Open "PROVIDER=Microsoft.jet.OLEDB.4.0;data source=C:\db1.mdb;" 'replace with the path and filename of your mdb database
sqlString = "SELECT state FROM states ORDER BY state"
myRec.Open sqlString, myConn, adOpenKeyset, adLockReadOnly
    With myRec
        Do Until .EOF
            Combo1.AddItem !State
            .MoveNext
        Loop
    End With
myRec.Close
End Sub

'========form unload event. close connection
Private Sub Form_Unload(Cancel As Integer)
myConn.Close
End Sub

S
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10954828
One correction :)

'======load the counties to combo2
Private Sub Combo1_Click() '                          VVVV
sqlString = "SELECT * FROM counties WHERE state='" & Combo1.Text & "' ORDER By county"
Combo2.Clear
myRec.Open sqlString, myConn, adOpenKeyset, adLockReadOnly
    With myRec
        Do Until .EOF
            Combo2.AddItem !county
            .MoveNext
        Loop
    End With
myRec.Close
End Sub

S
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

734 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