Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 7th, 2006, 01:21 PM
Authorized User
 
Join Date: Sep 2003
Location: , , .
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

Reply With Quote
  #2 (permalink)  
Old January 8th, 2006, 12:29 PM
Authorized User
 
Join Date: Jul 2004
Location: clapton-in-gordano, n.somerset, United Kingdom.
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

Reply With Quote
  #3 (permalink)  
Old January 9th, 2006, 11:17 AM
Friend of Wrox
Points: 4,007, Level: 26
Points: 4,007, Level: 26 Points: 4,007, Level: 26 Points: 4,007, Level: 26
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Lansing, Michigan, USA.
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
Reply With Quote
  #4 (permalink)  
Old January 9th, 2006, 04:06 PM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
  #5 (permalink)  
Old November 16th, 2007, 12:20 PM
Registered User
 
Join Date: Nov 2007
Location: , , .
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.
Reply With Quote
  #6 (permalink)  
Old November 16th, 2007, 12:25 PM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
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
Reply With Quote
  #7 (permalink)  
Old November 16th, 2007, 12:26 PM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

or "bang dot"

mmcdonal

Look it up at: http://wrox.books24x7.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:41 PM.


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