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!

Find + Like Question????

Status
Not open for further replies.

Gti

Programmer
Jul 23, 2001
99
PT
Hi

What i need is make a search to a field in a database
I have this code to search but this search is only true if both are equal. What i want is make a search by one or more occurrences of any character
I know there are a function "Like" but i can't do it

Code:
-----------------------------------------------------------
With Adodc1.Recordset
.MoveFirst

.Find "Name='" & Txt_Text & "'"
If .EOF Then
MsgBox Txt_Text & " not Found."
Else

Dtl_Material.Text = Txt_Text
Dtl_Material.SetFocus

End If
End With
-----------------------------------------------------------

Tks any help
;)
Gti
 
You could use a Like operator with asterisks on each end. Depends on how you want the search to behave.

Code:
  .Find "Name LIKE '*" & Txt_Text & "*'"

This has the asterisk wildcard on each end of the text string. I'm not sure if this is what you're looking for, let me know.
 
Try using this code:

With Adodc1.Recordset
.MoveFirst

.Find "Name Like '%" & Txt_Text & "%'"
If .EOF Then
MsgBox Txt_Text & " not Found."
Else

Dtl_Material.Text = Txt_Text
Dtl_Material.SetFocus

End If
End With

This means there may be characters before and after the Txt_Text.

The % sign means there are more characters before the Txt_Text and after the Txt_Text. If you know there are no characters before the Txt_Text then leave the % sign off of the front. Like this:

.Find "Name Like '" & Txt_Text & "%'"

If you know the Txt_Text contains no characters behind the Txt_Text then omit the % sign from the back. Like this:

.Find "Name Like '%" & Txt_Text & "

I am used to SQL Server 7.0. The above code is correct for SQL but I'm almost sure it will work with access. Anything else I'm not sure about. Since you didn't specify your database type I took for granted it was access by your code.

Hope this helps ya. Good luck.



Rob
Just my $.02.
 
Apparently I was typing this at the same time as Sypher. He is correct on the * if it is Access I think. Just try it both ways. Rob
Just my $.02.
 
Works Perfectly ;)

Tkx for the help
 
Another Question!!!

Imagine!!! I have a TextBox and ListBox.
The ListBox are connected to a database, and have some strings
The TextBox is when i fill it with something i make a search in the ListBox and if is something equal or similiar
setfocus the string in the listbox

I something like the a search in the windows

Any Help Tks

;)
 
I guess I would loop through the items in your listbox to search for the text in your textbox then select the item in the listbox if found. . .

Code:
For i = 0 to Listbox.ListCount - 1
     If "*" & Textbox.Text & "*" = Listbox.List(i) Then
          Listbox.ListIndex = i
     End If
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top