All my data entry options in my forms are working well..but now I'm to trying to limit duplicate entries in an access form though DLookup() but it doesn't seem like its recognizing "AND."
A little background: TripID is a foreign key in tblBeltSurvey. Within each trip (TripID) there can be up to four transects (one-to-many relationship) that are stored in tblBeltSurvey as TransectNum. I am running the code below on update of cmbTransectNum to check and make sure that specific transect number hasn't already been used for that specific tripID. I can't make TransectNum a pk because Transect #'s 1-4 can be repeated for separate trips. It works well to limit duplicate transects if there is only one TripID record, but when I go to add another 'trip' with transect 1 I get my MsgBox..even though there isn't a transect 1 for the new tripID record yet. If TransectNum X (or whatever) wasn't created for a previous trip then I don't get my message when creating transect #X under the newer trip. It seems like it isn't checking to see if the TransectNum AND TripID are records before it sends my message. Any thoughts to simplify this, or fix my code so I only get the message if a duplicate transect within a specific tripID is trying to be created?
If Me.TransectNum = DLookup("[TransectNum]", "tbl_BeltSurvey", "[TransectNum] = 1") And DLookup("TripID", "tbl_BeltSurvey", "TripID = " & Me.TripID.Value) Then
MsgBox "Transect 1 has already been created." & vbCr & "Please check transect number and try again.", vbInformation, "Duplicate Transect Number"
Me.Undo
.... repeated with TransectNum = 2...4
End if
-------------------
I also was thinking of creating a lookup function that checks to see if all four transects were already entered when the button is clicked to open the belt survey form so the user doesn't have to look through records to confirm they entered all the information for that trip. Something like...on form open...
If DLookup("[TransectNum]","tbl_BeltSurvey","[TransectNum] = "1" AND [TransectNum] = "2" AND [TransectNum] = "3" AND [TransectNum] = "4"") AND DLookup("TripID", _
"tbl_BeltSurvey", "TripID = " & Me.TripID.Value) Then
MsgBox "All belt survey transects have been entered for this trip." ......
End if
Is there an easier way to create a string criteria so I don't have TransectNum = 1 AND .....etc in my lookup? Or should I just keep my lookup criteria lengthy?