|
Subject:
|
passing a parameter to an Access query!
|
|
Posted By:
|
pankaj_daga
|
Post Date:
|
1/7/2006 12:21:01 PM
|
Hi everyone,
I have been trying this for hours and to no avail. I do not know why this should be so difficult!
I have a query in Access say something simple like:
Select * from Employees
All I want to do is pass it a parameter like:
Select * from Employee where Name = "My name"
How can I achieve something so trivial from VBA? I was hoping that a runSQL command with the query name and the WHERE clause would do it, but apparantly not.
Thanks! Pankaj
|
|
Reply By:
|
elansolutionsltd
|
Reply Date:
|
1/8/2006 11:29:15 AM
|
The code you need is
DoCmd.RunSQL "SELECT * FROM Employee WHERE [Name] = '" & "My Name" & "';"
"My Name" could equally well be a variable
Sometimes Access in awkward and you might need to replace [Name] by Employee.Name
Good Luck
|
|
Reply By:
|
SerranoG
|
Reply Date:
|
1/9/2006 10:17:26 AM
|
If you want to hard-code it then it would be something like
DoCmd.RunSQL "SELECT * FROM Employee WHERE [Name] = 'Smith';"
If you want to specify a specific field in a form, then it would be something like
DoCmd.RunSQL "SELECT * FROM Employee WHERE [Name] = '" & Me.txtName & "';"
Where txtName is the textbox that is bound to the name field. You would use YOUR textbox name in there instead of txtName in my example.
Greg Serrano Michigan Dept. of Environmental Quality, Air Quality Division
|
|
Reply By:
|
mmcdonal
|
Reply Date:
|
1/9/2006 3:06:02 PM
|
Alternatively, you can put the parameter in the criteria of the query when it is designed, and have the criteria refer to your textbox on your form:
Criteria: [Forms]![frmYourForm].[txtYourTextBox]
Then just open the form or report and it will automatically grab the parameter without passing anything in code.
The downside of this solution is that the query is not reusable except when the form "frmYourForm" is open.
HTH
mmcdonal
|
|
Reply By:
|
russell64
|
Reply Date:
|
11/16/2007 11:20:11 AM
|
In response to mmcdonal's comment:
This works sometime, but I had a very complex query that wouldn't recognize something like [Forms]![frmMain]![txtBox] as a valid field name or expression (which is the exact error message I was receiving). To correct this issue, I switched the query into SQL view and added a PARAMETERS line, which could also be done in design view I guess. I added this line: PARAMETERS [Forms]![frmMain]![txtBox] Short;
Of course, substitue your own datatype and form information, but this surprisingly worked. I haven't seen this info posted anywhere else, so I thought I would post it. Hope this helps someone.
|
|
Reply By:
|
mmcdonal
|
Reply Date:
|
11/16/2007 11:25:30 AM
|
There are many posts on the site about adding a criteria to the query to take the parameter from a control on a form. The downside to this is that the query only works when the form is open. The proper bagn dot notation for a control on a form is:
[Forms]![frmMain].[txtBox]
To make the query reusable, you can pass the parameter as a WHERE clause on the DoCmd.OpenReport line. This method allows you to check for valid data in the control before passing the parameter, and allows you to re-use the query for other processes.
HTH
mmcdonal
Look it up at: http://wrox.books24x7.com
|
|
Reply By:
|
mmcdonal
|
Reply Date:
|
11/16/2007 11:26:11 AM
|
or "bang dot"
mmcdonal
Look it up at: http://wrox.books24x7.com
|