Record search code error (1 Viewer)

jp80

Registered User.
Local time
Today, 01:33
Joined
Jun 19, 2014
Messages
11
When opening my form for the first time and searching for a record, it displays the 'no record found' MsgBox as well as returning the actual record. The same code works fine in another database that I have created, so what am I doing wrong here?

Code:
 Private Sub SearchButton_Click()
  Dim rsTemp As Recordset
     If IsNull(Me.SearchField) = False Then
    Set rsTemp = Me.RecordsetClone
    rsTemp.FindFirst "[TIDRef] = '" & Me.SearchField & "'"
    If Not rsTemp.NoMatch Then
    Me.Bookmark = rsTemp.Bookmark
    Else
    MsgBox "No record found", vbOKOnly + vbInformation, "Sorry"
    End If
    Me.SearchField.Value = Null
    End If
    
    If IsNull(Me.SearchField1) = False Then
    Set rsTemp = Me.RecordsetClone
    rsTemp.FindFirst "[CompassRef] = '" & Me.SearchField1 & "'"
    If Not rsTemp.NoMatch Then
    Me.Bookmark = rsTemp.Bookmark
    Else
    MsgBox "No record found", vbOKOnly + vbInformation, "Sorry"
    End If
    Me.SearchField1.Value = Null
    End If
 End Sub
 

JHB

Have been here a while
Local time
Today, 02:33
Joined
Jun 17, 2012
Messages
7,732
You are searching for 2 different fields value in 2 different fields, with 2 search boxes in 2 if structures, maybe 1 criteria is found and the other not! Then the message box pop up + you move to the record where the criteria is found.
Maybe you should set up you code a little more easier to read.
Code:
 Private Sub SearchButton_Click()
  Dim rsTemp As Recordset
  
  If IsNull(Me.SearchField) = False Then
    Set rsTemp = Me.RecordsetClone
    rsTemp.FindFirst "[TIDRef] = '" & Me.SearchField & "'"
    If Not rsTemp.NoMatch Then
      Me.Bookmark = rsTemp.Bookmark
    Else
      MsgBox "No record found", vbOKOnly + vbInformation, "Sorry"
    End If
    Me.SearchField.Value = Null
  End If
  If IsNull(Me.SearchField1) = False Then
    Set rsTemp = Me.RecordsetClone
    rsTemp.FindFirst "[CompassRef] = '" & Me.SearchField1 & "'"
    If Not rsTemp.NoMatch Then
      Me.Bookmark = rsTemp.Bookmark
    Else
      MsgBox "No record found", vbOKOnly + vbInformation, "Sorry"
    End If
    Me.SearchField1.Value = Null
  End If
 End Sub
 

vbaInet

AWF VIP
Local time
Today, 01:33
Joined
Jan 22, 2010
Messages
26,374
Why don't you explain what the could should do? From what I see it doesn't make sense to perform a second search if the first search was successful. So explain it in more detail.
 

Users who are viewing this thread

Top Bottom