Solved

Access VBA SQL select via ADO with parameters

Posted on 2012-03-26
6
526 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

832 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