Solved

Building an SQL statement using access forms and VB.

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

759 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

20 Experts available now in Live!

Get 1:1 Help Now