[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Connecting an access database to a combobox using Visual Basic 6.0

Posted on 2004-04-29
5
Medium Priority
?
186 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 19

Accepted Solution

by:
Shauli earned 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Suggested Courses

649 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