A
Andy
Hi;
Need code in "Before Update" that would warn a user that a product has
already been selected in the current record.
Have read other post replies concerning this issue, and although they seem
to work for others the answers I have found are not working in this
database.
Have Tables:
tblCustomer MainTblID (AutoNumber)
tblProductBought ProductID (AutoNumber) & MainTblID (Number) & ProductName
(Text)
Relation is one to many MainTblID / MainTblID
Forms:
frmCustomer (it contains a bound control to MainTblID from tblCustomer.)
sfrmProductBought (it also contains a bound control to MainTblID from
tblProductBought.)
Note: Added MainTblID to both for testing purposes.
Tried modifying Microsoft's example:
' SAMPLE FOR NUMERIC LOOKUP
' =DLookUp("[LastName]", "Employees", _
"[EmployeeID] = Form![EmployeeID]")
Private Sub cbxProductName_BeforeUpdate(Cancel As Integer)
Dim X As Variant
X = DLookup("[ProductName]", "tblProductBought ", _
"[MainTblID ]= Form![MainTblID]")
If Not IsNull(X) Then
Beep
Msg = MsgBox("That ''Product'' has already been selected.",
vbOKOnly, "Our Company Name")
Cancel = True
Response = acDataErrDisplay
End If
The above code does detect that the product was already added to the current
record but it ALSO shows the MsgBox if the same product was selected in a
different Record. Meaning Record 1 bought a hat, when a user selects a hat
for Record 2 the MsgBox is displayed. Record 2 does not contain a hat as
yet.
I know that means the code is returning the selection from a different
Record.
Have tried this modification to refer to the MainTblId on the frmCustomer
X = DLookup("[ProductName]", "tblProductBought ", _
"[MainTableID] = Forms![frmCustomer].Form![MainTblID]")
' and tried "[MainTableID] = Forms![frmCustomer]!Form![MainTblID]")
And this modification to refer to the control on the sfrmProductBought.
X = DLookup("[ProductName]", "tblProductBought ", _
"[MainTblID] = Forms![frmCustomer]![sfrmProductBought].Form![MainTblID]")
' also tried !
In all cases it shows that the product was selected in the Current Record if
that product had been selected in another Record.
Any suggestions.
Thank You for taking the time to read this post.
Andy
Need code in "Before Update" that would warn a user that a product has
already been selected in the current record.
Have read other post replies concerning this issue, and although they seem
to work for others the answers I have found are not working in this
database.
Have Tables:
tblCustomer MainTblID (AutoNumber)
tblProductBought ProductID (AutoNumber) & MainTblID (Number) & ProductName
(Text)
Relation is one to many MainTblID / MainTblID
Forms:
frmCustomer (it contains a bound control to MainTblID from tblCustomer.)
sfrmProductBought (it also contains a bound control to MainTblID from
tblProductBought.)
Note: Added MainTblID to both for testing purposes.
Tried modifying Microsoft's example:
' SAMPLE FOR NUMERIC LOOKUP
' =DLookUp("[LastName]", "Employees", _
"[EmployeeID] = Form![EmployeeID]")
Private Sub cbxProductName_BeforeUpdate(Cancel As Integer)
Dim X As Variant
X = DLookup("[ProductName]", "tblProductBought ", _
"[MainTblID ]= Form![MainTblID]")
If Not IsNull(X) Then
Beep
Msg = MsgBox("That ''Product'' has already been selected.",
vbOKOnly, "Our Company Name")
Cancel = True
Response = acDataErrDisplay
End If
The above code does detect that the product was already added to the current
record but it ALSO shows the MsgBox if the same product was selected in a
different Record. Meaning Record 1 bought a hat, when a user selects a hat
for Record 2 the MsgBox is displayed. Record 2 does not contain a hat as
yet.
I know that means the code is returning the selection from a different
Record.
Have tried this modification to refer to the MainTblId on the frmCustomer
X = DLookup("[ProductName]", "tblProductBought ", _
"[MainTableID] = Forms![frmCustomer].Form![MainTblID]")
' and tried "[MainTableID] = Forms![frmCustomer]!Form![MainTblID]")
And this modification to refer to the control on the sfrmProductBought.
X = DLookup("[ProductName]", "tblProductBought ", _
"[MainTblID] = Forms![frmCustomer]![sfrmProductBought].Form![MainTblID]")
' also tried !
In all cases it shows that the product was selected in the Current Record if
that product had been selected in another Record.
Any suggestions.
Thank You for taking the time to read this post.
Andy