Solved

Building an SQL statement using access forms and VB.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

919 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

12 Experts available now in Live!

Get 1:1 Help Now