Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Searches Within VB

Posted on 2002-07-12
10
Medium Priority
?
149 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 400 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
Independent Software Vendors: 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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.
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

926 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