Solved

SQL Searches Within VB

Posted on 2002-07-12
10
143 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
  • 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
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!

 

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

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

756 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