?
Solved

I need help passing parameters to a query in Access from VBA

Posted on 2015-01-23
2
Medium Priority
?
586 Views
Last Modified: 2015-01-25
Hi Experts,
I have the created a query in Access 2013 and named it qryTest3.  The values for some of the fields are hard coded.  I want to be a ble to pass in the parameters for the following fields in my query:  SK.Skill, SK.NumberLevel, and TmpDate.  I want to pass in the values/parameters from my VBA code and place the results in a recordset.  How do I do that?  Do I have to rewrite my query?  Below is a the sample of my query:

SELECT AV.Tech_Name, AV.TmpDate, AV.AVAILABILITY, AV.NOTES, AV.Skill_ID, SK.Skill, SK.Level, SK.NumberLevel
FROM qryAvailability2NEW AS AV INNER JOIN tblSkills AS SK ON SK.SkillID = AV.Skill_ID
WHERE SK.Skill in ('Programmer') AND SK.NumberLevel <= 2
AND (AV.TmpDate >= '2/3/2015' AND AV.TmpDate <= '2/5/2015');

Open in new window



Thank you very much in advance.
mrotor
0
Comment
Question by:mainrotor
2 Comments
 
LVL 54

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 400 total points
ID: 40567905
Hi,

pls try

strSQL = "SELECT AV.Tech_Name, AV.TmpDate, AV.AVAILABILITY, AV.NOTES, AV.Skill_ID, SK.Skill, SK.Level, SK.NumberLevel
FROM qryAvailability2NEW AS AV INNER JOIN tblSkills AS SK ON SK.SkillID = AV.Skill_ID
WHERE SK.Skill in ('" & Param1 & "') AND SK.NumberLevel <= " & Param2 & "
AND (AV.TmpDate >= #" & Param3 & "# AND AV.TmpDate <= #" & Param4 & "# );

Open in new window


refer to
https://msdn.microsoft.com/en-us/library/dd627355(v=office.12).aspx

Regards
0
 
LVL 52

Accepted Solution

by:
Gustav Brock earned 1600 total points
ID: 40567944
You need to format the date parameters, and it could be simplified a little:

' Your parameter variables.
Dim strProgrammer As String
Dim intNumberLevel As Integer
Dim datDateFrom As Date
Dim datDateTo As Date

' Other variables.
Dim strDateFrom As String
Dim strDateTo As String
Dim strSQL As Date

strDateFrom = Format(datDateFrom, "yyyy\/mm\/dd")
strDateTo = Format(datDateTo, "yyyy\/mm\/dd")

strSQL = "SELECT AV.Tech_Name, AV.TmpDate, AV.AVAILABILITY, AV.NOTES, AV.Skill_ID, SK.Skill, SK.Level, SK.NumberLevel
FROM qryAvailability2NEW AS AV INNER JOIN tblSkills AS SK ON SK.SkillID = AV.Skill_ID
WHERE SK.Skill = '" & strProgrammer & "' AND SK.NumberLevel <= " & intNumberLevel & " AND (AV.TmpDate Between #" & strDateFrom & "# AND #" & strDateTo & "#)";

To open a recordset:

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strSQL)

/gustav
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question