Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 7th, 2006, 01:21 PM
Authorized User
 
Join Date: Sep 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default passing a parameter to an Access query!

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

 
Old January 8th, 2006, 12:29 PM
Authorized User
 
Join Date: Jul 2004
Posts: 46
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

 
Old January 9th, 2006, 11:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

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
 
Old January 9th, 2006, 04:06 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

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
 
Old November 16th, 2007, 12:20 PM
Registered User
 
Join Date: Nov 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old November 16th, 2007, 12:25 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

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
 
Old November 16th, 2007, 12:26 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

or "bang dot"

mmcdonal

Look it up at: http://wrox.books24x7.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing parameters to Access query pcassistnw Access 0 March 2nd, 2007 11:07 PM
Passing function parameter to query kar22 Access 1 February 27th, 2007 08:28 AM
Passing an Input Field to a Parameter Query stcraig BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 4 November 6th, 2006 06:06 PM
passing parameter to query vladimir Access 6 August 30th, 2006 05:39 AM
Passing parameter to Access query eapsokha Classic ASP Databases 2 September 16th, 2004 02:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.