View Full Version : Solved: Value exists?
ailyn
09-29-2005, 06:37 AM
How do you check if a value exists in a table on a form.
I want to check if customerId exists in order to show his info or not.
geekgirlau
09-29-2005, 06:50 PM
There's lot's of different methods: perform a Find, filter the form and check if the CustomerID field is null, create a recordset, or do a dlookup
If IsNull("[CustomerID]", "tblCustomer","[CustomerID]=" & MyVariable) then
' No customer record
Else
' Customer exists
End If
chocobochick
09-30-2005, 06:34 AM
Geekgirlau forgot to include the DLookup function. I think the corrected version looks like this:
If IsNull(DLookup("[CustomerID]", "tblCustomer","[CustomerID]=" & MyVariable)) Then
' No customer record
Else
' Customer exists
End If
Norie
10-01-2005, 09:14 AM
What about the DCount function?
geekgirlau
10-01-2005, 06:43 PM
Absolutely right Chocobochick (taking code off the top of my head is always a hit-and-miss affair!).
And yes Norie, Dcount would also work.
geekgirlau
10-03-2005, 02:35 AM
Ailyn, did this help?
ailyn
10-19-2005, 01:23 AM
Sorry for the delay!
I needed it for this:
Private Sub CustomerIdctrl_BeforeUpdate()
If IsNull(DLookup("[CustomerID]", "tblOrders", "[CustomerID]=" & Me.CustomerId)) Then
Me.CustomerIdctrl.Visible = False
Me.EditCustomers.Visible = False ' No customer record
Else
Me.CustomerIdctrl.Visible = True
Me.EditCustomers.Visible = True ' Customer exists
End If
End Sub
In frmOrders (with tblOrders) I have two fields: fldEditCustomers(link to Customers form) & CustomerIdCtrl (shows [customerId] from tblOrders). I want them to show only if [CustomerId] from tblOrders exists. But it simply hides both fields whether it exists or not. Can anybody tell me why?
This other one doesn't work either:
Private Sub CustomerIdctrl_BeforeUpdate()
If IsNull(DLookup("[CustomerID]", "tblOrders")) Then
Me.CustomerIdctrl.Visible = False
Me.EditCustomers.Visible = False ' No customer record
Else
Me.CustomerIdctrl.Visible = True
Me.EditCustomers.Visible = True ' Customer exists
End If
End Sub
geekgirlau
10-19-2005, 03:21 AM
Hi Ailyn,
I think the first thing to check is whether the event is being triggered - either of your samples should work, but it's difficult without seeing the form to tell whether they should be attached to a different event.
A quick way to test this is to put a MsgBox command in at the start of your code - that way you know that the code has actually been run.
PS. I've edited your post to include VBA tags. You can insert these by selecting your code, and clicking on the "VBA" button.
chocobochick
10-19-2005, 06:05 AM
If I understand correctly, the form frmOrders uses tblOrders as its recordset, CustomerIdctrl is the name of the control which points to the CustomerID field from this recordset, and you're attempting to hide two controls based on the existence of a value everytime you change to a new record. If that's the case, then:
1. You probably don't even need the DLookup function, because your control is already pointing to the value in the current record. Just use:
If IsNull(CustomerIdctrl) Then
2. If you were going to use the DLookup function, the right side of the expression in the last argument needs to point to your form's control, not to a table's field name. So the code probably needed to read:
If IsNull(DLookup("[CustomerID]", "tblOrders", "[CustomerID]=" & CustomerIdctrl)) Then
3. I believe the BeforeUpdate event only occurs when changes are being saved to your records. If you want to check each time new records are displayed, you're looking for the form's Current event. Try the following instead:
Private Sub Form_Current()
If IsNull(CustomerIdctrl) Then
CustomerIdctrl.Visible = False
EditCustomers.Visible = False ' No customer record
Else
CustomerIdctrl.Visible = True
EditCustomers.Visible = True ' Customer exists
End If
End Sub
I hope that helps. If it doesn't, or if you have any new questions, or if I've misunderstood your situation, then supplying a small attachment would be of great help.
ailyn
10-20-2005, 02:13 AM
O.o
I can't believe it, it was the last option. Because I only have one record in orders and I was checking if it worked on display not on changes! that was the problem!
THANKS A LOOOT!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.