?
Solved

Access VBA SQL select via ADO with parameters

Posted on 2012-03-26
6
Medium Priority
?
540 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 34

Accepted Solution

by:
Norie earned 2000 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 34

Expert Comment

by:Norie
ID: 37767230
I don't think it's possible.
0
Independent Software Vendors: 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

Technology Partners: 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!

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

741 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