Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Building an SQL statement using access forms and VB.

Posted on 2006-11-27
4
Medium Priority
?
289 Views
Last Modified: 2010-04-17
Hi Experts!

I have an access form that has serach critera fileds taken from the user.  I want to then efficently build an SQL string in VB.  

How does one intelligently build an SQL statement from the fields they provide?  To be more concrete about what I am asking; If I have say 10 possible criteria that I want to use in the SQL statement and I am collecting this from the user, I could have potentially 2^10 possible combinations of searches.  How do I build the statement programmatically without writing out a separate SQL statement for every possible combination of search criteria that the user could enter?

Many Thanks!

0
Comment
Question by:travishaberman
  • 2
4 Comments
 
LVL 4

Expert Comment

by:hclgroup
ID: 18020752
where
(form.field1 = NULL OR table.column1 = form.field1) AND
(form.field2 = NULL OR table.column2 = form.field2) AND
etc.

0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 1500 total points
ID: 18021026
in code, build the where clause like this:

Dim strWhere as String

If txtField1.Text <> "" then
    strWhere = " Where Field1 = '" & txtField1.Text & "'"
End If

If txtField2.Text <> "" then
   If strWhere = "" then
       strWhere = " Where Field2 = '" & txtField2.Text & "'"
   Else
       strWhere = " AND Field2 = '" & txtField2.Text & "'"
   End If
End If

If txtField3.Text <> "" then
   If strWhere = "" then
       strWhere = " Where Field3 = '" & txtField3.Text & "'"
   Else
       strWhere = " AND Field3 = '" & txtField3.Text & "'"
   End If
End If

and so on.  You should be able to see tha pattern.

AW
0
 

Author Comment

by:travishaberman
ID: 18022479
Many thanks!
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 18028919
glad to be of assistance.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Six Sigma Control Plans
Loops Section Overview

926 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