Want msg "no record found"

M

Maha

From my main form, the user inputs a value to search on
and the form opens with the matching record. The problem
is if there are no records, I'd like a message to pop up
before the form opens (which it does to a new record), so
I can prevent the form from opening at all. The current
code I have seems too inefficient, because it opens the
form, checks to see if it's on a new record, closes it if
it is, then the message pops up. How can I check for
matching records before opening the form? Here's my code:

stDocName = "frmComplaint"
stLinkCriteria = "[complaintID]=" & Me![txtIncidentNum]
DoCmd.OpenForm stDocName, , , stLinkCriteria
If Forms!frmComplaint.NewRecord = True Then
DoCmd.Close
MsgBox ("Incident not found.")
End If

I'd appreciate any ideas/solutions you may have.

TIA!!
 
B

Brian

Here is a sample of code from one of my applications that
does what I think your tring to do. This code checks to
see if there is any data, and if so, opens the form. If
not, sends back message to the user and keeps them on the
current form. I put this in the OnClick event for the
button that opens the form. Hope this helps.

Dim Criteria As String

Set MyDB = DBEngine.Workspaces(0).Databases(0)
Set myset = MyDB.OpenRecordset("FSE Customer Orders",
DB_OPEN_DYNASET)

Criteria = "([Customer Number] = " & Forms![FSE
Customer Maintenance]![Customer Number].Value & ")"

myset.FindFirst Criteria ' Locate first
occurrence.

If Not myset.NoMatch Then ' Matching record found.
stDocName = "FSE Customer Order (Associated)"
DoCmd.OpenForm stDocName, , , stLinkCriteria
Else 'No Matching Record found
'Me![Open Associated Orders Form].Enabled =
False
MsgBox "There are currently NO FSE Orders for
this Customer.", 48, "FSE Warning Message"
Exit Sub
End If

myset.Close
MyDB.Close
 
T

tina

try this:

If DCount("PrimaryKeyFieldName","TableName","ComplaintID="
& Me!txtIncidentNum) > 0 Then
DoCmd.OpenForm stDocName, , , stLinkCriteria
Else
MsgBox "Incident not found."
End If

hth
 

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