VBA code for Access Parameter query

SamBalsini

New Member
Joined
Mar 13, 2014
Messages
3
Hi there,
I am quiet new to this forum and also to VBA. I have created an Access database with a few tables and queries. I linked all queries to an excel sheet to be able to build a reconciliations. The front page of workbook has got three parameters (Start date, finish data and store no). I set the same parameter to all queries in database. I am looking for a VBA code to set those parameters in access queries and update the excel sheets. Any help would be appreciated. Thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
i dont know your specifics, but to give you an idea, it would be something like this

Code:
Dim C As ADODB.Command: Set C = New ADODB.Command
Dim mconn As ADODB.Connection: Set mconn = New ADODB.Connection
mconn.Open "your connection string"
With C
    .ActiveConnection = mconn
    .CommandType = adCmdStoredProc
    .CommandText = "your query name"
    .Parameters.Append .CreateParameter("param name", adInteger, adParamInput)
    .Parameters("param name").Value = "your paramater value"
End With
Dim rs As ADODB.Recordset: set rs = new adodb.recordset 
rs.Open C
 
Last edited:
Upvote 0
Thanks for replying so quickly. I tried to write the code but still struggling. I managed to write the below code but it will copy and save the parameter table rather than updating any advice would be appreciated.

Sub RunParameterQueries()

'Step 1: Declare your variables
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim i As Integer

'Step 2: Identify the database and query
Set MyDatabase = DBEngine.OpenDatabase _
("C:\Reconciliation\ Database V1.accdb")
Set MyQueryDef = MyDatabase.QueryDefs("Cards Till Diffs")

'Step 3: Define the Parameters
With MyQueryDef
.Parameters("[Number]") = Range("Store_No").Value
.Parameters("[Start date]") = Range("Date_from").Value
.Parameters("[End date]") = Range("Date_to").Value
End With

'Step 4: Open the query
Set MyRecordset = MyQueryDef.OpenRecordset


'Step 5: Clear previous contents
Sheets("Cards Till Diffs").Select
ActiveSheet.Range("A8:H10000").ClearContents


'Step 6: Copy the recordset to Excel
ActiveSheet.Range("A8").CopyFromRecordset MyRecordset


'Step 7: Save reconciliation
Dim Fname As String
Fname = Range("Store_Name").Value
ActiveWorkbook.SaveCopyAs "C:\Reconciliation\" & "Cash & Banking Review" & " " & Fname & Format(Now(), " dd_mm_yyyy") & ".xlsm"


MsgBox "Reconciliation has finished"

End Sub
 
Upvote 0
to update the database you can use an update query
your code above is pulling data from access instead
 
Upvote 0
The aim is update the query in access with those parameters and refresh query table in excel and save a copy of workbook.
 
Upvote 0

Forum statistics

Threads
1,221,469
Messages
6,160,027
Members
451,611
Latest member
PattiButche

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