Access query in VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello !

I'm currently developping an Excel-Access application. I would like, for my
import/export function in VBA, to use pre-saved access query in my vba code

Is it possible to do so? How?

Many thanks
 
Laurent

Certainly

It's easier to set up if you record yourself (Tools>Macros...Record new
macro) doing the following

Data>External Data>New Database Query... Select MS Access as the source>
Navigate to the database>Select the query>Complete the rest of the
wizard>Return Data to Excel.

If you then press Alt+F11, you will find the code written for you in
'Module1'

--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
(e-mail address removed)
 
If you wanted to use an ADODB recordset which can be hand here is a function
that I use. You will have to strip out the errorhandler... It returns a
connected or disconnected recordset depending on the value of the final
argument. You can take the queries you have in Access and switch them to SQL
view to get the Select statement whicu becomes the parameters of the
function... If you leave the connection open then you can modify the data in
the Access database.

Public Function RunQuery(ByVal strSelect As String, ByVal strFrom As String, _
ByVal strWhere As String, ByVal strOrderBy, ByVal blnConnected As Boolean)
As ADODB.Recordset
Dim strConnection As String

On Error GoTo ErrorHandler
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" &
m_cDBLocation & ";"

Set RunQuery = New ADODB.Recordset
With RunQuery
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockBatchOptimistic
End With

RunQuery.Open strSelect & " " & strFrom & " " & strWhere & " " &
strOrderBy, strConnection, , , adCmdText
If blnConnected = False Then Set RunQuery.ActiveConnection = Nothing
Exit Function

ErrorHandler:
modErrors.HandleError m_cModule, "RunQuery"
End Function
 
thanks nick your answer seems nice, but i don't have time to test it yet

jim > i dont 't get where you specify the access query, i'm actually quite a
beginner in vba
 
Stic with Nick's suggestion. Mine is a little more advanced in that you dont
specify the query but rather the SQL statement that generates the query
results...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top