Solved

Building an SQL statement using access forms and VB.

Posted on 2006-11-27
4
267 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 500 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

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!

Question has a verified solution.

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

Suggested Solutions

This is about my first experience with programming Arduino.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?

680 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