Hi Ofer,
Many thanks that works fine - how do I combine this with a message box?
I am trying
If DCount("*", "qrybom", "partno = '" & partno & "'") = 0 Then
MsgBox "This is a New Part - Do You Wish To Add?", vbYesNo, "Project
Costing Database"
If vbYesNo = vbNo Then Exit Sub
Else: DoCmd.RunSQL "INSERT INTO tblnewparts ( partno ) Values ('" & partno
& "')"
End If
Again this doesn't work (your code without the msgbox does) Also, how to I
clear the "new part" data from the lookup field?
Thanks for all your help - I'll never get the hang of this VBA!!
Dave
End Sub
Ofer Cohen said:
In that case, if the partno is a text type field, try
If DCount("*", "qrybom", "partno = '" & partno & "'")=0 Then
' if it doesn't exist enter a new value to the table
DoCmd.RunSQL "INSERT INTO tblnewparts ( partno ) Values ('" & partno & "')"
End If
--
Good Luck
BS"D
midiman69 said:
Hi Ofer
That is exactly what I am trying to do - No Error message with the first
code but you last generates "Data type mismatch in Criteria Expression"
partno is text.
Dave
:
Do you get an error message? and if so, what is it?
I think you are looking for something like
' Check if record exist using the dcount
If DCount("*", "qrybom", "partno = " & partno)=0 Then
' if it doesn't exist enter a new value to the table
DoCmd.RunSQL "INSERT INTO tblnewparts ( partno ) Values (" & partno & ")"
End If
--
Good Luck
BS"D
:
Hi Ofer,
Thanks for the reply.
The DlookUp is to check if a matching record exists, if not add the "new"
part number to tblnewparts - I have tried both of your suggestions - still
not working.
I have tried using the Before Update and After Update events.
I am very inexperienced in VBA so I hope you will forgive me if this is some
thing easy.
Dave
:
Why do you need this DLookUp, what are you trying to return?
If (DLookup("[partno]", "qrybom", IsNull([partno]) = True)) Then
When do you assign value to the partno, that you are using in the Insert?
If the partno is a value that suppose to return using the dlookup, then use
DoCmd.RunSQL "INSERT INTO tblnewparts ( partno ) Values (" & partno & ")"
If the partno is string then use
DoCmd.RunSQL "INSERT INTO tblnewparts ( partno ) Values ('" & partno & "')"
--
Good Luck
BS"D
:
I am trying to insert a record in to tblnewparts if a field contains no
matching records using the following
If (DLookup("[partno]", "qrybom", IsNull([partno]) = True)) Then
DoCmd.RunSQL "INSERT INTO tblnewparts ( partno ) " & "SELECT " & partno & ";"
End If
I gety no error message and the code complies but it doesn't work - can any
one please help?
Dave