Solved

SQL Searches Within VB

Posted on 2002-07-12
10
140 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks Much.  I'll give that a try....
0
 

Author Comment

by:Baddogi
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Baddogi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

728 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

13 Experts available now in Live!

Get 1:1 Help Now