Is there a way in Vb to call a query from an Access database?
Printable View
Is there a way in Vb to call a query from an Access database?
Absolutely, take your pick at what object library you're going to use(ADO, RDO, DAO) and go ahead. I would suggest using ADO, since according to Microsoft, ADO is going to be the ultimate data access library (until they'll have something better).
Add a reference to Microsoft ActiveX Data Objects and do something like this (I assume that you query is meant to bring a recordset back i.e. it is a Select statement):
Code:Dim cn As New ADODB.Connection
Dim cm As New ADODB.Command
Dim rs As New ADODB.Recordset
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDatabase.mdb"
With cm
Set .ActiveConnection = cn
.CommandType = adCmdStoredProc
.CommandText = "YourQueryName"
'If you have parameter(s) then you have to do something like this
'In this example I'm passing an Integer type parameter with value of 5
.Parameters.Append .CreateParameter("ParamName", adInteger, adParamInput, , 5)
Set rs = cm.Execute
End With
If Not rs.EOF Then
'Do your stuff with recordset here
End If
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
Regards,
You can also do it by opening an instance of Access, if Serge hasn't given you an approach you like...