Search and display record, and msgbox if record not found

G

Guest

Hi, I have created a search form that lets a user enter a company name to
search for. A command button then opens a form and goes directly to
that company's record. If left blank, it just goes to the first record, but
if the company name is not a record, then it opens a new record to be added.
How can I code this so that if the company name is not recognized, a message
box opens saying that no record was found? Here is the code I have:

Private Sub cmdSearchButton_Enter()
If Not IsNull(txtCompanyNameSearch) Then
DoCmd.OpenForm "frmCompanyInformation", WhereCondition:="CompanyName =
Forms!frmSearchByCompanyName!txtCompanyNameSearch"
Else
DoCmd.OpenForm "frmCompanyInformation"
End If
End Sub
 
D

Damon Heron

I see a problem with your method. If the user mistakenly enters
"CompanyAPC" and the company's name is "CompanyABC", then a new record would
be created with a invalid name. You would then have duplicates. I think a
better solution is a combobox tied to the companyID, companyName (set to no
dups) in your table and the user can type the first few letters in the
combobox and retrieve the company. Since they would be picking it out of an
existing list, there would be no spelling errors. If the company doesn't
exist, check out the not in list event and the example in Help.

Damon
 

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

Top