VBA to run queries with parameters

RichardMGreen

Well-known Member
Joined
Feb 20, 2006
Messages
2,177
Hi all

I'm fairly new to this so be gentle with me.
I've got a few queries to run in a database but some need parameters.
Here's what I've got so far:-
Code:
Function test()
    Dim qdf As QueryDef

    reducer = 7
    While Format(Date - reducer, "Dddd") <> "Monday"
        reducer = reducer + 1
    Wend
    agent_start_date = Date - reducer
    leave_start_date = agent_start_date - 98
    
    qrystartdate = InputBox("Enter Start date of data", "First day of Data Required", Date - 7)

    DoCmd.OpenQuery ("Update_Agent_Information")

    DoCmd.OpenQuery ("Append_Agent_Information")

    qdf.Parameters(0) = agent_start_date
    DoCmd.OpenQuery ("Delete_Agent_Leave")

    qdf.Parameters(0) = agent_start_date
    DoCmd.OpenQuery ("Update_Agent_Leave")

    qdf.Parameters(0) = agent_start_date
    DoCmd.OpenQuery ("Append_Agent_Leave")

    qdf.Parameters(0) = leave_start_date
    DoCmd.OpenQuery ("Output_Query")
End Function

I've got the dates sorted but I can't seem to pass them to the query to use instead of a prompt box.

Anyone any ideas?
 

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.
here's a really crappy way of doing it
Code:
Sub doit()
 
    Dim db As dao.Database
    Set db = CurrentDb()
 
    Dim sql As String
    Dim qdf As QueryDef
 
    ' this is just a junk temp querydef
    ' it exists only in this sub
On Error Resume Next
    db.QueryDefs.Delete ("doit_1")
    Err.Clear
On Error GoTo 0
 
    Set qdf = db.QueryDefs("Delete_Agent_Leave")
 
    ' rebuild the sql so that it contains the actual parameter value instead of a parameter object
    ' fill this in with your date
    sql = Replace( qdf.sql,  qdf.Parameters(0).Name, "#1/1/2010#" )
    Debug.Print sql
 
    Set qdf = Nothing
 
    ' now create our temp querydef
    Set qdf = db.CreateQueryDef("doit_1")
 
    ' and give it our good sql
    qdf.sql = sql
 
    DoCmd.OpenQuery "doit_1"
 
    db.QueryDefs.Delete ("doit_1")
 
    Set db = Nothing
 
End Sub
 
Last edited:
Upvote 0
Thanks for that.
Another quick question, can I replace the actual date you've used with a variable which will work out the date for me?
 
Upvote 0
Solved it.
I built just the date functions I needed in a VBA module and then used them like built-in functions in the queries.
Works now!
 
Upvote 0

Forum statistics

Threads
1,213,486
Messages
6,113,932
Members
448,533
Latest member
thietbibeboiwasaco

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