Call query with parameters from excel

Vbanoob98

Board Regular
Joined
Sep 13, 2019
Messages
128
Hi everyone, I have this code that calls a query from my access database. The issue is that I need to call a query with 2 parameters, how can I add this to the code?

Thanks in advance :)

VBA Code:
Sub test111()

Const cstrPath As String = "C:\Data.mdb"
Const cstrQuery As String = "View_qry"
Dim dbe As Object 'DAO.DBEngine '
Dim rs As Object 'DAO.Recordset '
Dim ws As Worksheet

Application.DisplayAlerts = True 'leave alerts on during testing '
Set dbe = CreateObject("DAO.DBEngine.120")
Set rs = dbe.OpenDatabase(cstrPath).OpenRecordset(cstrQuery)

For i = 0 To rs.Fields.Count - 1
        Sheets("Sheet1").Cells(1, i + 1) = rs.Fields(i).Name
    Next i
If Not rs.EOF Then
    Set ws = ThisWorkbook.Sheets("Sheet1")
    ws.Range("A2").CopyFromRecordset rs
End If

rs.Close
Application.DisplayAlerts = True

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Here is an example from Microsoft's documents:

Code:
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset

Set dbs = CurrentDb

'Get the parameter query
Set qfd = dbs.QueryDefs("qryMyParameterQuery")

'Supply the parameter value
qdf.Parameters("EnterStartDate") = Date
qdf.Parameters("EnterEndDate") = Date + 7

'Open a Recordset based on the parameter query
Set rst = qdf.OpenRecordset()
 
Upvote 0
as in the code sample. just set dbs = currentdb. Or as you did before I would think would still work. Or don't do it at all and just use currentdb straight up, which is what I usually do.
 
Upvote 0
Yea I've been reading on currentdb, but I still dont understand how it will know which one. As I have like 3 different ones
 
Upvote 0
Its the one you are running the code in. By definition there can only be one.
 
Upvote 0
You don't need a currentdb. just use your dbe variable as you did in your original code (usually this is by convention dbs or db, not dbe but it doesn't matter).
 
Upvote 0
Managed to make it work. But it seems I was only able to take 35k lines. Is there a way to take more?
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,331
Members
449,077
Latest member
jmsotelo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top