Solved

Access VBA SQL select via ADO with parameters

Posted on 2012-03-26
6
524 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
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now