Full Version: DLookup Invalid use of null
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
cpopham
I have a form that has a button that is enabled if there are notes in a related table and disabled if there are no notes in the related table. I thought that using the dlokup function wwould be the quickest and easiest way to accomplish this. If there is a value returned from dlookup then enable the button otherwise disable it.

Now, on that line of code, I get an invalid use of null error. My code looks like this:
CODE
    intOrgID = DLookup("OrgID", "OrgNote", "OrgID=" & intOrgID)
    
    If intOrgID < 0 Then
    Me.cmdViewNotes.Enabled = True
    Else
    Me.cmdViewNotes.Enabled = False
    End If


Any assistance would be greatly appreciated. Thank you,
Chester
niesz
Assuming you have a 'OrgID' control somewhere on your form, try:

Me.cmdViewNotes.Enabled = Nz(DLookup("OrgID", "OrgNote", "OrgID=" & Me.OrgID))
MiaAccess
The name of your field (intOrgID) is the same name as a field in the DLookup. That has to be wrong
cpopham
Thanks niesz, if there is a related row in the notes table, the button is now enabled, however, if it is a new record and null, I now get the error "Syntax Error (missing operator) in query expression 'OrgID='. Any ideas on that one?

MiaAccess,
I had been attempting to take the value of the field and assign it to the intOrgID variable and by placing the variable on the left and making it equal to the dlookup would normally work because the values on the right are read before the values on the left. Kind of like the code line:
intCount = intCount + 1
That will read the intCount value on the right first, then add 1 to it and then assign it to the intcount variable on the left last.

Thank you,
Chester
niesz
If it is a new record, I guess we can assume that there is no corresponding Notes in the OrgNote table, so we need to set the button to disabled.

So try:

If Nz(Me.OrgID) = "" Then
Me.cmdViewNotes.Enabled = False
Else
Me.cmdViewNotes.Enabled = Nz(DLookup("OrgID", "OrgNote", "OrgID=" & Me.OrgID))
End If
cpopham
That did it. Thank you for the assistance! I am only asked to do an Access application about once a year and it is very hard to switch gears from using Access as a back end to an application to developing an entire application in Access.

Thank you,
Chester
niesz
Glad you got it working.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.