Not Being Able To Use DLookup With Dynamic Data...

Challseus

Board Regular
Joined
Feb 5, 2003
Messages
141
Okay, for anyone who hasn't been helping me these past 3 days, here's my story(sorry if this is long, but I had to make sure I was totally understood):

I have a userform with 4 different fields. One of them is called ssn(social security number). After the user types the ssn in, they can move to type in data in another field. At that point, an on_exit function is called on the field, and a query is run on another table to see if the ssn is in it. If not, it will prompt the user to store it in the database, or cancel. Here's my code:
Code:
'In Form_Keys
Option Explicit
Private Sub ssn_Exit(Cancel As Integer)
      If Not Form_Students.queryFormStudents(ssn.value) Then
            DoCmd.CancelEvent
      End If
End Sub
.
.
.In Form_Students
Public Function queryFormStudents(ssn As String) As Boolean
      queryFormStudents = True
      Dim answer As Integer

      MsgBox ssn
      MsgBox Form_Students.ssn
      If IsNull(DLookup("ssn", "Students", "[ssn] = ssn)) Then
            answer = MsgBox("SSN doesn't exist.  Create new one?", vbOKCancel)
            If answer = vbOK Then
                  DoCmd.OpenForm "Students"
            Else
                  queryFormStudents = False
            End If
      End If
End Function
Here's the problem...The DLookup is never returning null, even if the ssn the user types is definitely not in the other table. I even used MsgBox's to see what the corresponding values are, and they were in fact different as assumed. Now, for some reason, if I write it like this:
Code:
If IsNull(DLookup("ssn", "Students", "[ssn] ='123-45-6789'))
It functions as it's supposed to. So my question is, can you not use variables in DLookup, only actual constant values?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Your DLookup should look something like this:
Code:
DLookup("ssn", "Students", "[ssn] = '" & ssn & "'")
However, a better way to do what you are doing would be to make the text box that you are inputting SSN into a combo box (based on all SSN's in your other table) and set the Limit To List property to Yes, then use the NotInList event to handle SSN's that are not in your other table (you could add them via code).

HTH,

Russell
 
Upvote 0

Forum statistics

Threads
1,221,544
Messages
6,160,431
Members
451,646
Latest member
mmix803

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top