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
 
Hi, there's no reason why anything in this code would limit your query to 35k records. Have you confirmed that the result is correct given your parameters?
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Indeed. I fixed it, it was a matter of learning the syntax. Thanks a lot, you guys do God's work
 
Upvote 0

Forum statistics

Threads
1,215,483
Messages
6,125,065
Members
449,206
Latest member
Healthydogs

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