Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textbox in sql query 3

Status
Not open for further replies.

obulldog27

Programmer
Apr 26, 2004
169
US
what is the syntax if I want to substitute ChemicalName in the query for a textbox so that user can define his own search field.

Code:
Private Sub Command1_Click()
SearchCriteria = find_box.Text

If SearchCriteria = "" Then
  MsgBox "Enter a Criteria"
Else
With compchartado
.RecordSource = "SELECT * FROM Comp_Chart WHERE ChemicalName like '" & SearchCriteria & "%'"
.Refresh
End With
End If
End Sub
 
.RecordSource = "SELECT * FROM Comp_Chart WHERE " & txtSearchField.Text & " like '" & SearchCriteria & "%'"

Also, you should use a replace statment on the search criteria so....

Change the first line to...
SearchCriteria = Replace(find_box.Text, "'", "''")

If someone entered a word with an apostrophe, your query would fail.


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Well suppose you have a textbox named txtSearchField ....

.RecordSource = "SELECT * FROM Comp_Chart WHERE " & txtSearchField.Text & " like '" & SearchCriteria & "%'"

Or did I not understand the question?
 
Basically, do it the same way you do the criteria:

Code:
Private Sub Command1_Click()
SearchCriteria = find_box.Text
DearchField = field_box.Text

If SearchCriteria = "" Then
  MsgBox "Enter a Criteria"
Else
With compchartado
.RecordSource = "SELECT * FROM Comp_Chart WHERE " & SearchField & " like '" & SearchCriteria & "%'"
.Refresh
End With
End If
End Sub

I would suggest a listbox or combobox where the user can just select a field name, rather than a text box where the user might (probably will...no, DEFINITELY will) enter a non-existent field name. Also, you need to have some sort of decision code (if, select, etc.) to configure the SQL command properly for different data types. The code above will only work if any field that can be selected is a character or text field. If a numeric or date field is supplied, the SQL code will fail as it is currently written.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
woah!

Your post wasnt up yet when I mashed the reply button but somehow we both decided to name the control txtSearchField

spooky
 
Great minds think alike, right?

If that were true, there would only be about 50 patents in the U.S. patent office. [bigsmile]

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Great minds think alike, right?

If that were true, there would only be about 50 patents in the U.S. patent office


Looks like somebody read today's Dilbert!


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Every morning. Not much gets past you jebenson.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top