I wrote a program 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 program, 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 boxes 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
Here is an example of how to do what you are trying to do: METHOD: Recordset::Find
Example for your code:
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.