Recordset.FindFirst

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to make a field search these fields and one more called
"Company". I orginally had the second and third FindFirst lines the same as
the first, but I got a type mismatch error. The problem is that nothing
happens. Is this the correct way or should I do something else, and why
won't it do anything?
-------------start code---------------
With Me
.Recordset.FindFirst "CustomerInfo.MailboxNo. = " & !SearchField
.Recordset.FindFirst FirstName = !SearchField
.Recordset.FindFirst LastName = !SearchField
If .Recordset.NoMatch Then
MsgBox "Record can't be found.", , "PackageLog 2005"
.Recordset.MoveFirst
Else
End If
!SearchField.SetFocus
End With
 
James said:
I am trying to make a field search these fields and one more called
"Company". I orginally had the second and third FindFirst lines the same as
the first, but I got a type mismatch error. The problem is that nothing
happens. Is this the correct way or should I do something else, and why
won't it do anything?
-------------start code---------------
With Me
.Recordset.FindFirst "CustomerInfo.MailboxNo. = " & !SearchField
.Recordset.FindFirst FirstName = !SearchField
.Recordset.FindFirst LastName = !SearchField
If .Recordset.NoMatch Then
MsgBox "Record can't be found.", , "PackageLog 2005"
.Recordset.MoveFirst
Else
End If
!SearchField.SetFocus
End With


You need to do the search with a single FindFirst.

If I understand what you're trying to do with this, I think
you might want to use:

.Recordset.FindFirst "[MailboxNo.] = " _
& !SearchField _
& " OR FirstName = """ & !SearchField & """" _
& " OR LastName = """ & !SearchField & """"

Note the additional quotes that are needed for Text type
fields. I can't tell if the MailBoxNo field is a numeric
type or a text field, so I don't know if you need the quotes
there too.

Also note that you either have an extra period at the end of
the MailboxNo field or you need to enclose the name in sqare
brackets.
 
Thank you for your help. The first part works. If I put in a mailbox number
(yes, it is a number datatype) it finds it with no problem, and if it doesn't
exist then it displays the msgbox. When I type in a name it gives me
run-time error 3070: the microsoft jet database does not recognize '(text I
type in)' as a valid field name or expression. Then the code you gave me is
highlighted yellow. Any ideas?

Marshall Barton said:
James said:
I am trying to make a field search these fields and one more called
"Company". I orginally had the second and third FindFirst lines the same as
the first, but I got a type mismatch error. The problem is that nothing
happens. Is this the correct way or should I do something else, and why
won't it do anything?
-------------start code---------------
With Me
.Recordset.FindFirst "CustomerInfo.MailboxNo. = " & !SearchField
.Recordset.FindFirst FirstName = !SearchField
.Recordset.FindFirst LastName = !SearchField
If .Recordset.NoMatch Then
MsgBox "Record can't be found.", , "PackageLog 2005"
.Recordset.MoveFirst
Else
End If
!SearchField.SetFocus
End With


You need to do the search with a single FindFirst.

If I understand what you're trying to do with this, I think
you might want to use:

.Recordset.FindFirst "[MailboxNo.] = " _
& !SearchField _
& " OR FirstName = """ & !SearchField & """" _
& " OR LastName = """ & !SearchField & """"

Note the additional quotes that are needed for Text type
fields. I can't tell if the MailBoxNo field is a numeric
type or a text field, so I don't know if you need the quotes
there too.

Also note that you either have an extra period at the end of
the MailboxNo field or you need to enclose the name in sqare
brackets.
 
James said:
Thank you for your help. The first part works. If I put in a mailbox number
(yes, it is a number datatype) it finds it with no problem, and if it doesn't
exist then it displays the msgbox. When I type in a name it gives me
run-time error 3070: the microsoft jet database does not recognize '(text I
type in)' as a valid field name or expression. Then the code you gave me is
highlighted yellow.

Let's back up a little here. Since you're using the same
text box for all the search fields, I think we're always
going to have this problem. If you enter a number in the
SearchField text box, the search string will end up looking
like:

[MailboxNo.] = 123 OR Lname = "123" OR ...

which will work ok.

However, if you enter a name in the SearchField text box,
the search string will end up like this:

[MailboxNo.] = Smith OR Lname = "Smith" OR ...

and you'll get a syntax error because Smith is not a valid
term in an SQL statement.

All that adds up to a fundamental conflict caused by the
ambiguity of using one text box for several fields with
different types. If you'd be willing to use separate text
boxes for each possible criteria, this would be easy to
avoid.

Create 3 text boxes named txtMb, txtLN and txtFN. The users
would then indicate what they wanted to search for by typing
something in one (or more) of the text boxes and the code
behind your search button would be along the lines of this
air code:

Dim strWhere As String
If Not IsNull(txtMb) Then
If IsNumeric(txtMb) Then
strWhere = strWhere & " AND [MailboxNo.]=" & txtMb
Else
MsgBox "invalid mailbox number"
End If
End If
If Not IsNull(txtLN) Then
strWhere = strWhere & " AND [LastName]=""" & txtLN & """"
End If
If Not IsNull(txtFN) Then
strWhere = strWhere & "AND [FirstName]=""" & txtFN & """"
End If
Me.Recordset.FindFirst Mid$(strWhere. 6)
If Me.Recordset.NoMatch Then
. . .

This has the added advantage(?) that users could search for
records that matched multiple criteria such as Smith and
Jack.
--
Marsh
MVP [MS Access]


Marshall Barton said:
You need to do the search with a single FindFirst.

If I understand what you're trying to do with this, I think
you might want to use:

.Recordset.FindFirst "[MailboxNo.] = " _
& !SearchField _
& " OR FirstName = """ & !SearchField & """" _
& " OR LastName = """ & !SearchField & """"

Note the additional quotes that are needed for Text type
fields. I can't tell if the MailBoxNo field is a numeric
type or a text field, so I don't know if you need the quotes
there too.

Also note that you either have an extra period at the end of
the MailboxNo field or you need to enclose the name in sqare
brackets.
 
Thank you, I will do it that way. One more thing--How would you use
FindNext, so that if the found record is not the correct one but the next one
or two is?

Marshall Barton said:
James said:
Thank you for your help. The first part works. If I put in a mailbox number
(yes, it is a number datatype) it finds it with no problem, and if it doesn't
exist then it displays the msgbox. When I type in a name it gives me
run-time error 3070: the microsoft jet database does not recognize '(text I
type in)' as a valid field name or expression. Then the code you gave me is
highlighted yellow.

Let's back up a little here. Since you're using the same
text box for all the search fields, I think we're always
going to have this problem. If you enter a number in the
SearchField text box, the search string will end up looking
like:

[MailboxNo.] = 123 OR Lname = "123" OR ...

which will work ok.

However, if you enter a name in the SearchField text box,
the search string will end up like this:

[MailboxNo.] = Smith OR Lname = "Smith" OR ...

and you'll get a syntax error because Smith is not a valid
term in an SQL statement.

All that adds up to a fundamental conflict caused by the
ambiguity of using one text box for several fields with
different types. If you'd be willing to use separate text
boxes for each possible criteria, this would be easy to
avoid.

Create 3 text boxes named txtMb, txtLN and txtFN. The users
would then indicate what they wanted to search for by typing
something in one (or more) of the text boxes and the code
behind your search button would be along the lines of this
air code:

Dim strWhere As String
If Not IsNull(txtMb) Then
If IsNumeric(txtMb) Then
strWhere = strWhere & " AND [MailboxNo.]=" & txtMb
Else
MsgBox "invalid mailbox number"
End If
End If
If Not IsNull(txtLN) Then
strWhere = strWhere & " AND [LastName]=""" & txtLN & """"
End If
If Not IsNull(txtFN) Then
strWhere = strWhere & "AND [FirstName]=""" & txtFN & """"
End If
Me.Recordset.FindFirst Mid$(strWhere. 6)
If Me.Recordset.NoMatch Then
. . .

This has the added advantage(?) that users could search for
records that matched multiple criteria such as Smith and
Jack.
--
Marsh
MVP [MS Access]


James wrote:
I am trying to make a field search these fields and one more called
"Company". I orginally had the second and third FindFirst lines the same as
the first, but I got a type mismatch error. The problem is that nothing
happens. Is this the correct way or should I do something else, and why
won't it do anything?
-------------start code---------------
With Me
.Recordset.FindFirst "CustomerInfo.MailboxNo. = " & !SearchField
.Recordset.FindFirst FirstName = !SearchField
.Recordset.FindFirst LastName = !SearchField
If .Recordset.NoMatch Then
MsgBox "Record can't be found.", , "PackageLog 2005"
.Recordset.MoveFirst
Else
End If
!SearchField.SetFocus
End With
Marshall Barton said:
You need to do the search with a single FindFirst.

If I understand what you're trying to do with this, I think
you might want to use:

.Recordset.FindFirst "[MailboxNo.] = " _
& !SearchField _
& " OR FirstName = """ & !SearchField & """" _
& " OR LastName = """ & !SearchField & """"

Note the additional quotes that are needed for Text type
fields. I can't tell if the MailBoxNo field is a numeric
type or a text field, so I don't know if you need the quotes
there too.

Also note that you either have an extra period at the end of
the MailboxNo field or you need to enclose the name in sqare
brackets.
 
James said:
Thank you, I will do it that way. One more thing--How would you use
FindNext, so that if the found record is not the correct one but the next one
or two is?

Do exactly the same thing except use FindNext instead of
FindFirst.

You may want to create a separate function to construct the
Where string so that you don't have duplicated logic in
multiple procedures.
--
Marsh
MVP [MS Access]


Marshall Barton said:
Let's back up a little here. Since you're using the same
text box for all the search fields, I think we're always
going to have this problem. If you enter a number in the
SearchField text box, the search string will end up looking
like:

[MailboxNo.] = 123 OR Lname = "123" OR ...

which will work ok.

However, if you enter a name in the SearchField text box,
the search string will end up like this:

[MailboxNo.] = Smith OR Lname = "Smith" OR ...

and you'll get a syntax error because Smith is not a valid
term in an SQL statement.

All that adds up to a fundamental conflict caused by the
ambiguity of using one text box for several fields with
different types. If you'd be willing to use separate text
boxes for each possible criteria, this would be easy to
avoid.

Create 3 text boxes named txtMb, txtLN and txtFN. The users
would then indicate what they wanted to search for by typing
something in one (or more) of the text boxes and the code
behind your search button would be along the lines of this
air code:

Dim strWhere As String
If Not IsNull(txtMb) Then
If IsNumeric(txtMb) Then
strWhere = strWhere & " AND [MailboxNo.]=" & txtMb
Else
MsgBox "invalid mailbox number"
End If
End If
If Not IsNull(txtLN) Then
strWhere = strWhere & " AND [LastName]=""" & txtLN & """"
End If
If Not IsNull(txtFN) Then
strWhere = strWhere & "AND [FirstName]=""" & txtFN & """"
End If
Me.Recordset.FindFirst Mid$(strWhere. 6)
If Me.Recordset.NoMatch Then
. . .

This has the added advantage(?) that users could search for
records that matched multiple criteria such as Smith and
Jack.

James wrote:
I am trying to make a field search these fields and one more called
"Company". I orginally had the second and third FindFirst lines the same as
the first, but I got a type mismatch error. The problem is that nothing
happens. Is this the correct way or should I do something else, and why
won't it do anything?
-------------start code---------------
With Me
.Recordset.FindFirst "CustomerInfo.MailboxNo. = " & !SearchField
.Recordset.FindFirst FirstName = !SearchField
.Recordset.FindFirst LastName = !SearchField
If .Recordset.NoMatch Then
MsgBox "Record can't be found.", , "PackageLog 2005"
.Recordset.MoveFirst
Else
End If
!SearchField.SetFocus
End With


:
You need to do the search with a single FindFirst.

If I understand what you're trying to do with this, I think
you might want to use:

.Recordset.FindFirst "[MailboxNo.] = " _
& !SearchField _
& " OR FirstName = """ & !SearchField & """" _
& " OR LastName = """ & !SearchField & """"

Note the additional quotes that are needed for Text type
fields. I can't tell if the MailBoxNo field is a numeric
type or a text field, so I don't know if you need the quotes
there too.

Also note that you either have an extra period at the end of
the MailboxNo field or you need to enclose the name in sqare
brackets.
 
I can cycle through the records with this code, but if you are on the last
record then it will not find the item. I tried to put .movefirst but it
would only find the first record. I think is has something to do with
..bookmark, but I don't understand what to do.

With Me.RecordsetClone
.FindNext "CustomerInfo.[MailboxNo] = " & Me!txtfindnumber
If .NoMatch = True Then
MsgBox "Record can't be found.", , "PackageLog 2005"
.MoveFirst
Else
End If
Me!txtfindnumber.SetFocus
End With

Marshall Barton said:
James said:
Thank you, I will do it that way. One more thing--How would you use
FindNext, so that if the found record is not the correct one but the next one
or two is?

Do exactly the same thing except use FindNext instead of
FindFirst.

You may want to create a separate function to construct the
Where string so that you don't have duplicated logic in
multiple procedures.
--
Marsh
MVP [MS Access]


James wrote:
Thank you for your help. The first part works. If I put in a mailbox number
(yes, it is a number datatype) it finds it with no problem, and if it doesn't
exist then it displays the msgbox. When I type in a name it gives me
run-time error 3070: the microsoft jet database does not recognize '(text I
type in)' as a valid field name or expression. Then the code you gave me is
highlighted yellow.
Marshall Barton said:
Let's back up a little here. Since you're using the same
text box for all the search fields, I think we're always
going to have this problem. If you enter a number in the
SearchField text box, the search string will end up looking
like:

[MailboxNo.] = 123 OR Lname = "123" OR ...

which will work ok.

However, if you enter a name in the SearchField text box,
the search string will end up like this:

[MailboxNo.] = Smith OR Lname = "Smith" OR ...

and you'll get a syntax error because Smith is not a valid
term in an SQL statement.

All that adds up to a fundamental conflict caused by the
ambiguity of using one text box for several fields with
different types. If you'd be willing to use separate text
boxes for each possible criteria, this would be easy to
avoid.

Create 3 text boxes named txtMb, txtLN and txtFN. The users
would then indicate what they wanted to search for by typing
something in one (or more) of the text boxes and the code
behind your search button would be along the lines of this
air code:

Dim strWhere As String
If Not IsNull(txtMb) Then
If IsNumeric(txtMb) Then
strWhere = strWhere & " AND [MailboxNo.]=" & txtMb
Else
MsgBox "invalid mailbox number"
End If
End If
If Not IsNull(txtLN) Then
strWhere = strWhere & " AND [LastName]=""" & txtLN & """"
End If
If Not IsNull(txtFN) Then
strWhere = strWhere & "AND [FirstName]=""" & txtFN & """"
End If
Me.Recordset.FindFirst Mid$(strWhere. 6)
If Me.Recordset.NoMatch Then
. . .

This has the added advantage(?) that users could search for
records that matched multiple criteria such as Smith and
Jack.


James wrote:
I am trying to make a field search these fields and one more called
"Company". I orginally had the second and third FindFirst lines the same as
the first, but I got a type mismatch error. The problem is that nothing
happens. Is this the correct way or should I do something else, and why
won't it do anything?
-------------start code---------------
With Me
.Recordset.FindFirst "CustomerInfo.MailboxNo. = " & !SearchField
.Recordset.FindFirst FirstName = !SearchField
.Recordset.FindFirst LastName = !SearchField
If .Recordset.NoMatch Then
MsgBox "Record can't be found.", , "PackageLog 2005"
.Recordset.MoveFirst
Else
End If
!SearchField.SetFocus
End With


:
You need to do the search with a single FindFirst.

If I understand what you're trying to do with this, I think
you might want to use:

.Recordset.FindFirst "[MailboxNo.] = " _
& !SearchField _
& " OR FirstName = """ & !SearchField & """" _
& " OR LastName = """ & !SearchField & """"

Note the additional quotes that are needed for Text type
fields. I can't tell if the MailBoxNo field is a numeric
type or a text field, so I don't know if you need the quotes
there too.

Also note that you either have an extra period at the end of
the MailboxNo field or you need to enclose the name in sqare
brackets.
 
James said:
I can cycle through the records with this code, but if you are on the last
record then it will not find the item. I tried to put .movefirst but it
would only find the first record. I think is has something to do with
.bookmark, but I don't understand what to do.

With Me.RecordsetClone
.FindNext "CustomerInfo.[MailboxNo] = " & Me!txtfindnumber
If .NoMatch = True Then
MsgBox "Record can't be found.", , "PackageLog 2005"
.MoveFirst
Else
End If
Me!txtfindnumber.SetFocus
End With


Not sure what you want here. If you click your find next
match button after you get the not found message, it should
find the first match. Are you saying you don't want to have
to click the button a second time? What happened to the
FindFirst button?

Maybe you should put the FindFirst logic in where you have
the MoveFirst???
 

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

FindFirst Method syntax 3
Recordset Find in 2000 3
Access MS Access VBA - 3022 error 3
DAO - apostrophe in primary key 4
Use a form's control value as a table name 2
use of me 1
Syntax (Missing Operator) Error 2
What wrong is it? 8

Back
Top