Solved

Building an SQL statement using access forms and VB.

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java  and programming certification ? 4 95
Arduino EDI - Programming language 3 95
C# code editing and collaboration 3 115
sp_configure 'external scripts enabled', 1; 7 52
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

810 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