Like FindFirst Syntax

G

Guest

Need help with the correct syntax for using "Like with FindFirst"

My current code

With Me.RecordsetClone
.FindFirst "CompanyName = '" & MyName & "'"

Like ??
 
T

Tim Ferguson

Need help with the correct syntax for using "Like with FindFirst"

My current code

With Me.RecordsetClone
.FindFirst "CompanyName = '" & MyName & "'"

Like ??

jetCriterion = "CompanyName LIKE """ & userInput & """"

rst.FindFirst jetCriterion
If Not rst.NoMatch Then
' ... etc



HTH


Tim F
 
G

Guest

Hi Tim

I am getting an error "Object required"
This is my code:
With Me.RecordsetClone

MyString = "CompanyName LIKE """ & MyName & """"
rst.FindFirst MyString
If .NoMatch Then
MsgBox "Company Name Not Found"
Else
Me.Bookmark = .Bookmark
End If
End With
 
T

Tim Ferguson

I am getting an error "Object required"
This is my code:

Check your references: you may be using the ADO library, which is the
default from Access 2000 upwards, rather than DAO which is better<grin>.
The point is that an ADODB.Recordset does not have the .FindFirst and
..NoMatch methods -- it has the .Find method instead, but I don't know a
lot about it because I always do filtering and finding on the server
rather than in memory.

You could either: add a reference to Data Access Objects 3.61, and
declare your recordset object unambiguously vis:

Dim rst AS DAO.Recordset

' ...

Set rst = me.RecordsetClone ' could be ado or dao unless you specify
rst.FindFirst etc


or else you can do the whole thing in ADO

Dim rst As ADODB.Recordset

' ...

Set rst = Me.RecordSetClone
rst.Find etc, etc. ' look up help for using the ADODB .Find method


Hope that helps

Tim F
 
G

Guest

Thanks Tim

Your last post corrected the "object required" error.
Dim rst As DAO.Recordset
Set rst = Me.RecordsetClone

But for the record the "Like" syntax was not totally correct.
It was missing "*"

The correct syntax
Should be

jetCriterion = "CompanyName LIKE """ & MyName & "*"""
rst.FindFirst jetCriterion

For anyone that is checking this post for future reference.

Thanks again.

Ileana
 
T

Tim Ferguson

But for the record the "Like" syntax was not totally correct.
It was missing "*"

For anyone that is checking this post for future reference.
People checking in the future may want to know how to find a field ending
with the given text:

"... LIKE ""*" & userInput & """ ..."

or text containing the given text anywhere

"... LIKE ""*" & userInput & "*"" ..."

and don't forget that the wildcards and delimiters are different in ADO
and any other ISO dialect of SQL:

"... LIKE '%" & userInput & "%' ..."

etc etc. I also did not include any code looking for embedded quote
marks. And I did not include any code to test the userInput for malicious
attacks. It would not be good to allow someone to put something like this
in:

Eric' GO DROP TABLE Products GO //

without parsing it first.

All the best


Tim F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top