I have a typical Access form where users enter data and press a button to save the data as a new record in an existing table. The table has two primary keys, jobId and clientId. Prior to adding the new record, I want to verify that the jobId and clientId combination don't already exist in my table. That way I can display my own error messages, rather than getting the Access integrity contraint messages. What is the best approach to doing this? I am a VBA rookie.
My thought was to do something like this:
Private Sub validateKeys()
Dim strSQL As String
Dim intJobCount As String
strSQL = "SELECT COUNT(JobID) AS [JobCount] FROM tblJob WHERE ClientID = "ClientID & " AND JobID = '" & JobID & "';"
intJobCount = DoCmd.RunSQL strSQL
If intJobCount > 0 Then
'Output error message
Else
'Add record
End Sub
You'll notice my code falls apart at the point where I try and set the result of the SQL statement to intJobCount. How can I access this value? Is there a better way of doing this?
Thanks so much!
My thought was to do something like this:
Private Sub validateKeys()
Dim strSQL As String
Dim intJobCount As String
strSQL = "SELECT COUNT(JobID) AS [JobCount] FROM tblJob WHERE ClientID = "ClientID & " AND JobID = '" & JobID & "';"
intJobCount = DoCmd.RunSQL strSQL
If intJobCount > 0 Then
'Output error message
Else
'Add record
End Sub
You'll notice my code falls apart at the point where I try and set the result of the SQL statement to intJobCount. How can I access this value? Is there a better way of doing this?
Thanks so much!