[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2015-01-23
2
Medium Priority
?
580 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 53

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Suggested Courses

649 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