
11-07-2000, 03:43 AM
|
|
Senior Contributor
* Guru *
|
|
Join Date: Mar 2000
Location: Christchurch, New Zealand
Posts: 470
|
|
Re: How to run an access query from visual basic
|
If you are using DAO, you can open a recordset based on an existing query. (you need to set a reference to DAO 3.51 for Access 97 or DAO 3.6 for Access 2000)
E.G. Say your query is called qselData then you could access your query like this:<CODE><PRE>Private Sub GetData()
Dim dbRef As DAO.Database
Dim recData As DAO.Recordset
Set dbRef = DAO.OpenDatabase("D:VBData.mdb")
<font color=blue>With dbRef.QueryDefs("qselData")
Set recData = .OpenRecordset(dbOpenDynaset)
End With</font color=blue>
Do Until recData.EOF
' Do whatever you need
recData.MoveNext
Loop
End Sub</CODE></PRE>
An alternative method is:<CODE><PRE>Private Sub GetData()
Dim dbRef As DAO.Database
Dim recData As DAO.Recordset
Set dbRef = DAO.OpenDatabase("D:VBData.mdb")
<font color=red>Set recData = dbRef.OpenRecordset("qselData", dbOpenDynaset)</font color=red>
Do Until recData.EOF
' Do whatever you need
recData.MoveNext
Loop
End Sub</CODE></PRE>
For larger database's, SQL is the go. But if you have already done all the work and you are only looking at using access for now, use your existing queries.
HTH
|
|