Solved

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

Posted on 2015-01-23
2
563 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 51

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 100 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 50

Accepted Solution

by:
Gustav Brock earned 400 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

689 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