views:

142

answers:

1

Hey I wrote a programme that would add, delete, save and search through records in a database (recordset) however I was doing it in a team. My task was to add the search function to the programme, which I have however I am having problems with adding an error message for when somebody types a word/anything that isn't in the database/recordset.

So for example in the textbox(txtFindBox.Text) if they type "ashbndash" it would come up with an error message, i've commented out my own error message boxs but tell me where i'm going wrong please :(

Here's the code for the find button.

Private Sub cmdFindDB_Click()
adoCustomer.Recordset.MoveFirst
If optLastName.Value = True Then
adoCustomer.Recordset.Find "LastName='" & txtFindBox.Text & "'"
'Else
'MsgBox ("NO RECORD FOUND")
End If
If OptFirstName.Value = True Then
adoCustomer.Recordset.Find "FirstName='" & txtFindBox.Text & "'"
'Else
'MsgBox ("NO RECORD FOUND")
End If
End Sub

edit: Just like to say the problem is that every time I hit the button 'find' it comes up with the message "NO RECORD FOUND" in a msgbox even though it finds the answer, it also comes up with that msgbox if you type in gibberish too.

Thanks for your time

Regards Haroon

A: 

Here is an example of how to do what you are trying to do: METHOD: Recordset::Find

Example for your code:

adoCustomer.Recordset.MoveFirst
adoCustomer.Recordset.Find "LastName='" & txtFindBox.Text & "'"
If (adoCustomer.Recordset.BOF = True) OR (adoCustomer.Recordset.EOF = True) Then
   MsgBox "Record not found"
End If

Instead of checking that the Value property is true, you need to check EOF and BOF. These stand for End Of File and Beginning Of File. So if either is true, then you are not "inside" the recordset, meaning you did not find anything.

magnifico
Thankyou so much! <3
Haroon Ghazni