You may have seen some other posts on this form that I am working on. New question, new thread.
I have an application with a main entry form (frmHBCase) for new projects/cases. There are 3 sub forms -
Custodians (frmCaseCustodian) - people who will give us data for the project
Assets (frmCaseAsset) - data on media for the project
Asset Custodians (frmCaseAssetCustodian) - a many/many linking form to tie each asset to any custodians having data on it. (1 asset may have multiple custodian stuff on it, one custodian may have data on multiple assets)
I created code that runs on a command button from the main form. A user -
1) clicks the record selector of a single record on the custodian sub form
2) clicks the record selector of a single record on the asset sub form
3) clicks the command button to assign asset custodian relationship.
The command button will generate a new record in the asset/custodian key table and populate the sub form with the keys for the selected records from the other 2 sub forms.
Now I need to add a clause that will prevent the command button from executing the add record, if there is already a case asset assigned to that case custodian in that given case record.
This is the code I have for adding the record. The portion to catch the dup is in red:
The dlookup is to the query qryfrmCaseCustodianExists which has the following sql output:
The query is not limiting correctly. It is actually returning whatever the current record is in the frmCaseAssetCustodian sub form. I do not understand how to catch the FKCaseAsset and the FKCaseCustodian IDs that were just selected and compare them to the dlookup query, to catch if they are duplicate or not.
Am I missing something obvious?
Thanks!
misscrf
It is never too late to become what you could have been ~ George Eliot
I have an application with a main entry form (frmHBCase) for new projects/cases. There are 3 sub forms -
Custodians (frmCaseCustodian) - people who will give us data for the project
Assets (frmCaseAsset) - data on media for the project
Asset Custodians (frmCaseAssetCustodian) - a many/many linking form to tie each asset to any custodians having data on it. (1 asset may have multiple custodian stuff on it, one custodian may have data on multiple assets)
I created code that runs on a command button from the main form. A user -
1) clicks the record selector of a single record on the custodian sub form
2) clicks the record selector of a single record on the asset sub form
3) clicks the command button to assign asset custodian relationship.
The command button will generate a new record in the asset/custodian key table and populate the sub form with the keys for the selected records from the other 2 sub forms.
Now I need to add a clause that will prevent the command button from executing the add record, if there is already a case asset assigned to that case custodian in that given case record.
This is the code I have for adding the record. The portion to catch the dup is in red:
Code:
Private Sub cmdAddCaseAssetCust_Click()
Dim frm As Form
Set frm = Me.frmCaseAssetCustodian.Form
[COLOR=red][b]If DLookup("[PKHBCaseCustodianAssetKeyID]", "qryfrmCaseCustodianExists") > 0 Then
MsgBox "This custodian already exists in this case.", _
vbCritical, _
"Custodian May Only Exist Once in a Given Case"
Me.frmCaseAssetCustodian.Form.Undo[/b][/color]
Else
With frm.RecordsetClone
.AddNew
![FKCaseAsset] = [frmCaseAsset].Form![PKHBCaseAssetKeyID]
![FKHBCaseCustodian] = [frmCaseCustodian].Form![PKHBCaseCustodianKeyID]
.Update
frm.Bookmark = .LastModified
End With
Set frm = Nothing
[frmCaseAsset].Form![PKHBCaseAssetKeyID].Requery
[frmCaseCustodian].Form![PKHBCaseCustodianKeyID].Requery
End If
End Sub
The dlookup is to the query qryfrmCaseCustodianExists which has the following sql output:
Code:
SELECT tblKeyHBCaseAssetCust.PKHBCaseCustodianAssetKeyID, tblKeyHBCaseAssetCust.FKHBCaseCustodian, tblKeyHBCaseAssetCust.FKCaseAsset
FROM tblKeyHBCaseAssetCust
WHERE (((tblKeyHBCaseAssetCust.FKHBCaseCustodian)=[Forms]![frmHBCase]![frmCaseAssetCustodian].[Form]![FKHBCaseCustodian]) AND ((tblKeyHBCaseAssetCust.FKCaseAsset)=[Forms]![frmHBCase]![frmCaseAssetCustodian].[Form]![FKCaseAsset]));
The query is not limiting correctly. It is actually returning whatever the current record is in the frmCaseAssetCustodian sub form. I do not understand how to catch the FKCaseAsset and the FKCaseCustodian IDs that were just selected and compare them to the dlookup query, to catch if they are duplicate or not.
Am I missing something obvious?
Thanks!
misscrf
It is never too late to become what you could have been ~ George Eliot