Solved

Access VBA SQL select via ADO with parameters

Posted on 2012-03-26
6
529 Views
Last Modified: 2012-06-21
Hi,

Can anyone tell me if it's possible to use parameters within the ADO code.

For instance:


PARAM1 = "ClientName"
PARAM2 = "="
PARAM3 = "'Acme'"



SQL = _
    "SELECT [ClientName] FROM [dbo].[Clients] ORDER BY [ClientName] WHERE PARAM1 PARAM2 PARAM3"
 
Set cn = New ADODB.Connection
Set cmd = New ADODB.Command
 
With cn
    .ConnectionString = ConnString
    .Open
End With
 
With cmd
    .CommandText = SQL
    .ActiveConnection = cn

End With
 
Set rs = cmd.Execute
0
Comment
Question by:lee_jd
[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
6 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 37766330
Try this.
SQL = _
    "SELECT [ClientName] FROM [dbo].[Clients] ORDER BY [ClientName] WHERE [" & PARAM1 & "] " & PARAM2 & " " & PARAM3

Open in new window

0
 
LVL 2

Author Comment

by:lee_jd
ID: 37767066
I know how to do this by building the string.  I was hoping there was a built-in way to do it with parameters.  I imagine it's possible.
0
 
LVL 33

Expert Comment

by:Norie
ID: 37767230
I don't think it's possible.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37767297
lee_jd,

If you believe this is possible, then what might this look like as a solution...?

I am curious as to how imnorie's syntax could be done via some simpler "built-in way"?

JeffCoachman
0
 
LVL 75
ID: 37767335
Did you try imnorie approach.  The SQL evaluates to:

SELECT [ClientName] FROM [dbo].[Clients] ORDER BY [ClientName] WHERE [ClientName] = 'Acme'

mx
0
 
LVL 2

Author Comment

by:lee_jd
ID: 37769787
The reasoning behind my logic is that if I push a query to a SQL server proc via ADO I can add parameters easily.  I was hoping I could parameterise the predicate.  Not to worry if it can't be done.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

749 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