Lost Focus Event Need Help

H

Holly H.

I have a field that I want to have check another table upon
the loss of focus.

I have it working and it finds out if there is a record
matching the open record in another table and appends a
field in the matching record.

If it doesn't find a matching record, it pops up the screen
saying it will append 0 records and asks if it is OK.

What I need for it to do is not run the append query if
there is no matching record.

Here is the Event code as it currently is:

Private Sub REPORT_DATE_LostFocus()

Dim db As [Form_Open Job DB]
Dim strSQL As String

strSQL = "UPDATE [Sample Log] " & " SET [REPORT DATE] = #"
& Me![REPORT DATE] _
& "# WHERE [REFERENCE NUMBER] = " & Me![REFERENCE
NUMBER]

DoCmd.RunSQL strSQL, dbFailOnError ' db.RecordsAffected
has the number of updated records
Set db = Nothing

End Sub

Please let me know how I might change this code to ignore
the update command if there is no matching file.
 
K

Ken Snell [MVP]

If DCount("*", "Sample Log", "[REFERENCE NUMBER] = " & Me![REFERENCE
NUMBER]) > 0 Then
strSQL = "UPDATE [Sample Log] " & " SET [REPORT DATE] = #" & Me![REPORT
DATE] _
& "# WHERE [REFERENCE NUMBER] = " & Me![REFERENCE NUMBER]
DoCmd.RunSQL strSQL, dbFailOnError ' db.RecordsAffected
End If
 

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