I'm trying to figure out a problem I'm having with DLookup, and I think I've hit the limit of my abilities.
I've created a form (frmMain) that is used to enter student records. What I'm wanting to do is to check if a student already exists in the database. Rather than wait until the whole record is entered, I want to validate that the last name and first name do not already exist for the same record. E.g. I can have multiple "Johns" in the table, and multiple "Smiths", but I can only have one record with First_Name = "John" and Last_Name = "Smith". (Yes, I realize there could be two different John Smiths at some point - I'll worry about that another time!)
I've got the following code but when I go to run it I get "Run-time error 13: Type Mismatch".
-------------
Private Sub First_Name_AfterUpdate()
Dim fName As String
Dim lName As String
Dim NameResult As String
fName = Forms!frmMain![First Name]
lName = Forms!frmMain![Last Name]
If IsNull(DLookup("[Student Number]", "[Student Info]", "[Last Name] ='" & lName & "'" And "[First Name] ='" & fName & "'")) Then
NameResult = "NEW"
Else
NameResult = "EXISTS"
End If
End Sub
---------
To confirm, the name fields are both text fields. Names are entered in Last Name, First Name order, which is why I am validating after updating the First Name field.
I've added a Watch, and I can see that the variables lName and fName are being updated correctly, but I am receiving the mismatch error. More frustrating is that if I cut down the DLookup code to validate only the First Name or Last Name, I don't receive an error - although it's not useful since I'm only confirming the existence of the First Name or Last Name, but not both.
I don't know if it's the "ISNULL" or I've got the syntax of the DLookup wrong or if there is something else going on.
Any help would be MUCH appreciated!
I've created a form (frmMain) that is used to enter student records. What I'm wanting to do is to check if a student already exists in the database. Rather than wait until the whole record is entered, I want to validate that the last name and first name do not already exist for the same record. E.g. I can have multiple "Johns" in the table, and multiple "Smiths", but I can only have one record with First_Name = "John" and Last_Name = "Smith". (Yes, I realize there could be two different John Smiths at some point - I'll worry about that another time!)
I've got the following code but when I go to run it I get "Run-time error 13: Type Mismatch".
-------------
Private Sub First_Name_AfterUpdate()
Dim fName As String
Dim lName As String
Dim NameResult As String
fName = Forms!frmMain![First Name]
lName = Forms!frmMain![Last Name]
If IsNull(DLookup("[Student Number]", "[Student Info]", "[Last Name] ='" & lName & "'" And "[First Name] ='" & fName & "'")) Then
NameResult = "NEW"
Else
NameResult = "EXISTS"
End If
End Sub
---------
To confirm, the name fields are both text fields. Names are entered in Last Name, First Name order, which is why I am validating after updating the First Name field.
I've added a Watch, and I can see that the variables lName and fName are being updated correctly, but I am receiving the mismatch error. More frustrating is that if I cut down the DLookup code to validate only the First Name or Last Name, I don't receive an error - although it's not useful since I'm only confirming the existence of the First Name or Last Name, but not both.
I don't know if it's the "ISNULL" or I've got the syntax of the DLookup wrong or if there is something else going on.
Any help would be MUCH appreciated!