473,394 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Passing parameters to a Select Query in VBA

Hi All,

I have a table, tblCustomers, with fields SalutationID and Firstname.
I made a query, qrySelect = "Select FirstName from tblCustomers Where
SalutationID = [prmSalutationId]"
If I run this query by itself, it works fine. It asks me for the parameter
and then it shows the records . What I want is to be able to pass the
parameter to it in VBA

I am using the following code:

Dim stDocName as String
Dim db as Database
Dim qdf as QueryDef

Set db = CurrentDb
Set qdf = db.QueryDefs("qrySelect")
qdf.Parameters["prmSalutationId"] = 1

DoCmd.OpenQuery "qrySelect", acNormal, acEdit

When I run this code, the query is asking for the parameter, as if I didn't
pass it.
I don't want to let the query pick the value of a component from the form

Any help appreciated

Regards,
Nicolae



Nov 13 '05 #1
3 47261
Nicolae, the reason it prompts you for the parameter is because you are
opening the query with your last statement. Instead, issue the
openrecordset method on the recordset object. Also, not sure, but think you
need parenthesis instead of brackets. Try this:

Dim stDocName as String
Dim db as Database
Dim rs as Recordset
Dim qdf as QueryDef

Set db = CurrentDb
Set qdf = db.QueryDefs("qrySelect")

qdf.Parameters("Please enter a Salutations ID") = 1 'or a variable could be
used here

Set rs = qdf.OpenRecordset

Hope this helps.

--
Reggie

----------
"Nicolae Fieraru" <no****@please.cxm> wrote in message
news:41********@duster.adelaide.on.net...
Hi All,

I have a table, tblCustomers, with fields SalutationID and Firstname.
I made a query, qrySelect = "Select FirstName from tblCustomers Where
SalutationID = [prmSalutationId]"
If I run this query by itself, it works fine. It asks me for the parameter
and then it shows the records . What I want is to be able to pass the
parameter to it in VBA

I am using the following code:

Dim stDocName as String
Dim db as Database
Dim qdf as QueryDef

Set db = CurrentDb
Set qdf = db.QueryDefs("qrySelect")
qdf.Parameters["prmSalutationId"] = 1

DoCmd.OpenQuery "qrySelect", acNormal, acEdit

When I run this code, the query is asking for the parameter, as if I didn't pass it.
I don't want to let the query pick the value of a component from the form

Any help appreciated

Regards,
Nicolae


Nov 13 '05 #2
Dear Reggie,

Thank you for your help. The brackets were a typo, I wrote the code in the
email and put by mistake brakets.
The qdf.OpenRecordset command works fine, but it doesn't open the query on
the screen. I need to view the results of the query on the screen.

Regards,
Nicolae

"Reggie" <No**********@NoSpamsmittysinet.com> wrote in message
news:Vc********************@comcast.com...
Nicolae, the reason it prompts you for the parameter is because you are
opening the query with your last statement. Instead, issue the
openrecordset method on the recordset object. Also, not sure, but think you need parenthesis instead of brackets. Try this:

Dim stDocName as String
Dim db as Database
Dim rs as Recordset
Dim qdf as QueryDef

Set db = CurrentDb
Set qdf = db.QueryDefs("qrySelect")

qdf.Parameters("Please enter a Salutations ID") = 1 'or a variable could be used here

Set rs = qdf.OpenRecordset

Hope this helps.

--
Reggie

Nov 13 '05 #3
One way I know to do it is design 2 procedures in a standard module
something like this (got from
(http://www.mvps.org/access/queries/qry0005.htm)
Option Compare Database
Option Explicit
Public strname As String

Function SetValue(str As String)
strname = str
End Function

Function GetValue()
GetValue = strname
End Function
Now on click of button or whatever, pass a value to set the public variable
in the standard module. In your query on the criteria line of the field you
are filtering for type in =GetValue() and it will read the value you passed
to the SetValue function. Then it will open the query. Change the data
types as appopriate. What I have here opens my query showing me all records
where the fName field = Reggie. Hope this helps

Private Sub Command10_Click()
Dim str As String
str = "Reggie"
Call SetValue(str)
DoCmd.OpenQuery "qry1", , acEdit
End Sub

--
Reggie

----------
"Nicolae Fieraru" <no****@please.cxm> wrote in message
news:41********@duster.adelaide.on.net...
Dear Reggie,

Thank you for your help. The brackets were a typo, I wrote the code in the
email and put by mistake brakets.
The qdf.OpenRecordset command works fine, but it doesn't open the query on
the screen. I need to view the results of the query on the screen.

Regards,
Nicolae

"Reggie" <No**********@NoSpamsmittysinet.com> wrote in message
news:Vc********************@comcast.com...
Nicolae, the reason it prompts you for the parameter is because you are
opening the query with your last statement. Instead, issue the
openrecordset method on the recordset object. Also, not sure, but think you
need parenthesis instead of brackets. Try this:

Dim stDocName as String
Dim db as Database
Dim rs as Recordset
Dim qdf as QueryDef

Set db = CurrentDb
Set qdf = db.QueryDefs("qrySelect")

qdf.Parameters("Please enter a Salutations ID") = 1 'or a variable

could be
used here

Set rs = qdf.OpenRecordset

Hope this helps.

--
Reggie


Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can...
10
by: Resant | last post by:
I have a query : Exec 'Select * From Receiving Where Code In (' + @pCode + ')' @pCode will contain more than one string parameter, eg : A1, A2, A3 How can i write that parameters, I try use :...
3
by: MX1 | last post by:
I'm ready to pull the hair out of my head. I have a query with a couple of parameters that I want to get from combo boxes on a form. One parameter is a date with a dynamically calculated year and...
0
by: Zlatko Matić | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems...
4
by: Mike Dinnis | last post by:
Hi, I've been working through a number of turorials to try to learn more about retrieving data from a SQL database. I think i've mastered techniques where i create a sql string in the page and...
1
by: owengoodhew | last post by:
Guys I need your help/Advice... In my Access Database I have a query (lets say qry1) and in this query i have 2 fields for start and end date, which is provided by 2 Get functions. also i...
1
by: ShadesOfGrey | last post by:
Hello again. Thanks to Gord, I have this cool query that puts my student attendance records in a calendar format in Access 2003. PARAMETERS Text ( 255 ), IEEEDouble; TRANSFORM...
11
by: kennthompson | last post by:
Trouble passing mysql table name in php. If I use an existing table name already defined everything works fine as the following script illustrates. <?php function fms_get_info() { $result =...
2
by: vksundari | last post by:
I am executing a select query in a C# component and populating a ASP Grid in Search.aspx. Now one of the columns in the Grid in Search.aspx is Policy Number(hyper link) Basicallly I want to call a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.