Solved

SQL Searches Within VB

Posted on 2002-07-12
10
144 Views
Last Modified: 2012-05-04
I have a form with 5 search fields on it.  I also have a search button to search on 1 or multiple fields.

The problem is, how do I create one SQL search that can accomodate this without having to write a routine for every possible search combination?

Any input would be appreciated.

Thanks in Advance.
0
Comment
Question by:Baddogi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
ID: 7149014
Build a statement something like this:

Dim strSQL As String
strSQL = Iif(Text1.Text <> "","Field1 = '" & Text1.Text & "'")
strSQL = Iif(Text1.Text <> ""," AND ","") & Iif(Text2.Text <> "","Field2 = '" & Text2.Text & "'")
strSQL = Iif(Text1.Text <> "" And Text2.Text <> ""," AND ","") & Iif(Text3.Text <> "","Field3 = '" & Text3.Text & "'")

And so on until you get to five.

Then your statement will be correctly built for any combination of fields.
0
 
LVL 2

Expert Comment

by:yongyih
ID: 7149051
You have to use SQL statement.
e.g.

statement like "select * from table1, table2, table3 where searchfield = '" & txtsearchfield1.text & "'"
something like this..

u use ADO or DAO?
0
 

Author Comment

by:Baddogi
ID: 7149052
Thanks Much.  I'll give that a try....
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Baddogi
ID: 7149315
2 Problems -  

I got the statement built but there are 2 issues now.

1.  I get a argument not optional on
    strsql=IIF(********

2.  Am I Correct by writing the SQL as:
    Select strsql * from DB


0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7149342
You would use it like:

"Select * From MyTable Where " & strSQL

as for the missing argument, I would need to see the line to determine which bit is missing.
0
 

Author Comment

by:Baddogi
ID: 7149356
Thanks - The SQL statement should work ok - Here is the block of code for the other:  Do you see anything wrong?

choice = IIf(txtsearchNDC.Text <> "", "[ndc_code]='" & txtsearchNDC.Text & "")
choice = IIf(txtsearchNDC.Text <> "", " AND ", "") & IIf(txtPrevNDCSearch.Text <> "", "[prev_ndc_code] = '" & txtPrevNDCSearch.Text & "")
choice = IIf(txtsearchNDC.Text <> "" And txtPrevNDCSearch.Text <> "", " AND ", "") & IIf(txtmfgsearch.Text <> "", "[mfg_name] = '" & txtmfgsearch.Text & "")
choice = IIf(txtsearchNDC.Text <> "" And txtPrevNDCSearch.Text <> "" And txtmfgsearch.Text <> "", "AND", "") & IIf(txtprodnamesearch.Text <> "", "[prod_name]='" & txtprodnamesearch.Text & "")
choice = IIf(txtsearchNDC.Text <> "" And txtPrevNDCSearch.Text <> "" And txtmfgsearch.Text <> "" And txtgenericsearch.Text <> "", "And", "") & IIf(txtgenericsearch.Text <> "", "[gcr_name] = '" & txtgenericsearch.Text & "")
0
 

Author Comment

by:Baddogi
ID: 7149366
Thanks - The SQL statement should work ok - Here is the block of code for the other:  Do you see anything wrong?

choice = IIf(txtsearchNDC.Text <> "", "[ndc_code]='" & txtsearchNDC.Text & "")
choice = IIf(txtsearchNDC.Text <> "", " AND ", "") & IIf(txtPrevNDCSearch.Text <> "", "[prev_ndc_code] = '" & txtPrevNDCSearch.Text & "")
choice = IIf(txtsearchNDC.Text <> "" And txtPrevNDCSearch.Text <> "", " AND ", "") & IIf(txtmfgsearch.Text <> "", "[mfg_name] = '" & txtmfgsearch.Text & "")
choice = IIf(txtsearchNDC.Text <> "" And txtPrevNDCSearch.Text <> "" And txtmfgsearch.Text <> "", "AND", "") & IIf(txtprodnamesearch.Text <> "", "[prod_name]='" & txtprodnamesearch.Text & "")
choice = IIf(txtsearchNDC.Text <> "" And txtPrevNDCSearch.Text <> "" And txtmfgsearch.Text <> "" And txtgenericsearch.Text <> "", "And", "") & IIf(txtgenericsearch.Text <> "", "[gcr_name] = '" & txtgenericsearch.Text & "")
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7149382
I think I missed it out when I typed it in to start with:

choice = IIf(txtsearchNDC.Text <> "", "[ndc_code]='" & txtsearchNDC.Text & "","")
choice = IIf(txtsearchNDC.Text <> "", " AND ", "") & IIf(txtPrevNDCSearch.Text <> "", "[prev_ndc_code] = '" & txtPrevNDCSearch.Text & "","")
choice = IIf(txtsearchNDC.Text <> "" And txtPrevNDCSearch.Text <> "", " AND ", "") & IIf(txtmfgsearch.Text <> "", "[mfg_name] = '" & txtmfgsearch.Text & "","")
choice = IIf(txtsearchNDC.Text <> "" And txtPrevNDCSearch.Text <> "" And txtmfgsearch.Text <> "", "AND", "") & IIf(txtprodnamesearch.Text <> "", "[prod_name]='" & txtprodnamesearch.Text & "","")
choice = IIf(txtsearchNDC.Text <> "" And txtPrevNDCSearch.Text <> "" And txtmfgsearch.Text <> "" And txtgenericsearch.Text <> "", "And", "") & IIf(txtgenericsearch.Text <> "", "[gcr_name] = '" & txtgenericsearch.Text & "","")


The IIF function has three arguments: Iif(TestCondition,TrueValue,FalseValue)

I missed out the false value in the second Iif in each case.
0
 

Author Comment

by:Baddogi
ID: 7149435
The argument error went away but now the Select statement isn't working.  Incorrect near "where".  Don't I have to specify a field where [field] = ????
0
 

Author Comment

by:Baddogi
ID: 7149513
I figured it out.....  The correct SQL should be:

"SELECT * from [Database] Where" & _
sqlStr & "'"


Thanks Again For Everyone's Help On This.

0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

751 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