two criteria in DLookup

S

Song Su

No error when compiling but when open form, got 'type mismatch'. Here is the
code:

Private Sub Form_Load()

If DLookup("[userid]", "users", "Trim([userid]) = '" &
Left(fOSUserName(), 8) & "' AND [EmailAll] = " & -1) Then
cmdMassEmail.Enabled = True

ElseIf DLookup("[userid]", "users", "Trim([userid]) = '" &
Left(fOSUserName(), 8) & "' AND [Grade] = " & -1) Then
cmdGrade.Enabled = True

End If
End Sub

Both [EmailAll] and [Grade] are logical fields. I got fOSUserName from
http://www.mvps.org/access/api/api0008.htm
 
J

John W. Vinson

No error when compiling but when open form, got 'type mismatch'. Here is the
code:

Private Sub Form_Load()

If DLookup("[userid]", "users", "Trim([userid]) = '" &
Left(fOSUserName(), 8) & "' AND [EmailAll] = " & -1) Then
cmdMassEmail.Enabled = True

ElseIf DLookup("[userid]", "users", "Trim([userid]) = '" &
Left(fOSUserName(), 8) & "' AND [Grade] = " & -1) Then
cmdGrade.Enabled = True

End If
End Sub

Try putting the -1 (or its synonym True) inside the quotes:

If DLookup("[userid]", "users", "Trim([userid]) = '" &
Left(fOSUserName(), 8) & "' AND [EmailAll] = -1 ") Then
cmdMassEmail.Enabled = True

You can probably leave out the Trim() as well - Access tables don't store
trailing blanks.
 
S

Song Su

John W. Vinson said:
No error when compiling but when open form, got 'type mismatch'. Here is
the
code:

Private Sub Form_Load()

If DLookup("[userid]", "users", "Trim([userid]) = '" &
Left(fOSUserName(), 8) & "' AND [EmailAll] = " & -1) Then
cmdMassEmail.Enabled = True

ElseIf DLookup("[userid]", "users", "Trim([userid]) = '" &
Left(fOSUserName(), 8) & "' AND [Grade] = " & -1) Then
cmdGrade.Enabled = True

End If
End Sub

Try putting the -1 (or its synonym True) inside the quotes:

If DLookup("[userid]", "users", "Trim([userid]) = '" &
Left(fOSUserName(), 8) & "' AND [EmailAll] = -1 ") Then
cmdMassEmail.Enabled = True

You can probably leave out the Trim() as well - Access tables don't store
trailing blanks.

Thanks John. Tried your code, still get type mismatch

If DLookup("[userid]", "users", "[userid] = '" & Left(fOSUserName(), 8)
& "' AND [EmailAll] = -1 ") Then
cmdMassEmail.Enabled = True
End if
 
J

John W. Vinson

Try putting the -1 (or its synonym True) inside the quotes:

If DLookup("[userid]", "users", "Trim([userid]) = '" &
Left(fOSUserName(), 8) & "' AND [EmailAll] = -1 ") Then
cmdMassEmail.Enabled = True

You can probably leave out the Trim() as well - Access tables don't store
trailing blanks.

Thanks John. Tried your code, still get type mismatch

If DLookup("[userid]", "users", "[userid] = '" & Left(fOSUserName(), 8)
& "' AND [EmailAll] = -1 ") Then
cmdMassEmail.Enabled = True
End if

Ah. Missing the forest for the trees!

Your DLookUp expression will return either a text string if the criteria
match, or NULL if it doesn't. Your If expression expects the value to be
either TRUE or FALSE. Try

If Not IsNull(DLookup("[userid]", "users", "[userid] = '" &
Left(fOSUserName(), 8) & "' AND [EmailAll] = -1 ")) Then
cmdMassEmail.Enabled = True
End if

Or leave out the IF altogether:

cmdMassEmailEnabled = Not IsNull(DLookup("[userid]", "users", "[userid] = '"
& Left(fOSUserName(), 8) & "' AND [EmailAll] = -1 "))
 
S

Song Su

John W. Vinson said:
Try putting the -1 (or its synonym True) inside the quotes:

If DLookup("[userid]", "users", "Trim([userid]) = '" &
Left(fOSUserName(), 8) & "' AND [EmailAll] = -1 ") Then
cmdMassEmail.Enabled = True

You can probably leave out the Trim() as well - Access tables don't
store
trailing blanks.

Thanks John. Tried your code, still get type mismatch

If DLookup("[userid]", "users", "[userid] = '" & Left(fOSUserName(),
8)
& "' AND [EmailAll] = -1 ") Then
cmdMassEmail.Enabled = True
End if

Ah. Missing the forest for the trees!

Your DLookUp expression will return either a text string if the criteria
match, or NULL if it doesn't. Your If expression expects the value to be
either TRUE or FALSE. Try

If Not IsNull(DLookup("[userid]", "users", "[userid] = '" &
Left(fOSUserName(), 8) & "' AND [EmailAll] = -1 ")) Then
cmdMassEmail.Enabled = True
End if

Or leave out the IF altogether:

cmdMassEmailEnabled = Not IsNull(DLookup("[userid]", "users", "[userid] =
'"
& Left(fOSUserName(), 8) & "' AND [EmailAll] = -1 "))

It works! Thank you very much, John.
 
S

Song Su

John W. Vinson said:
Try putting the -1 (or its synonym True) inside the quotes:

If DLookup("[userid]", "users", "Trim([userid]) = '" &
Left(fOSUserName(), 8) & "' AND [EmailAll] = -1 ") Then
cmdMassEmail.Enabled = True

You can probably leave out the Trim() as well - Access tables don't
store
trailing blanks.

Thanks John. Tried your code, still get type mismatch

If DLookup("[userid]", "users", "[userid] = '" & Left(fOSUserName(),
8)
& "' AND [EmailAll] = -1 ") Then
cmdMassEmail.Enabled = True
End if

Ah. Missing the forest for the trees!

Your DLookUp expression will return either a text string if the criteria
match, or NULL if it doesn't. Your If expression expects the value to be
either TRUE or FALSE. Try

If Not IsNull(DLookup("[userid]", "users", "[userid] = '" &
Left(fOSUserName(), 8) & "' AND [EmailAll] = -1 ")) Then
cmdMassEmail.Enabled = True
End if

Or leave out the IF altogether:

cmdMassEmailEnabled = Not IsNull(DLookup("[userid]", "users", "[userid] =
'"
& Left(fOSUserName(), 8) & "' AND [EmailAll] = -1 "))

Great! It Works! Thank you John.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top