Solved

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

Posted on 2015-01-23
2
537 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 48

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 100 total points
Comment Utility
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 49

Accepted Solution

by:
Gustav Brock earned 400 total points
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now