views:

3336

answers:

4

Working with some VBA code in Access that when a very specific set of conditions are met it will pop up an InputBox asking for a integer. So far so good.

Private Sub Command10_Click()

    If Not IsNull(mrf) Then
        If min <> max Then
            If qty <= min Then
                mrf = GetParamValue
            End If
        End If
    End If
    End Sub

The problem is that the Not IsNull seems to be ignored. I would like it to ask for a value to be entered unless a value is already present. This keeps firing off the InputBox as long as the min <> max and qty <= min conditions are met. What am I missing here?

+2  A: 

No, the Not IsNull is working perfectly.

Remember, IsNull is a function which returns TRUE if the parameter passed to it is null, and false otherwise.

Your "If Not IsNull(mrf) Then" statement translates into english as "If mrf is not null then"

what that means is when mrf has a value, then you're processing the code inside the if statement. If you're wanting the inner code to fire when mrf IS null, then you need to remove the NOT from the statement.

Stephen Wrighton
LOL I had a moment. Thanks.
Terry
+1  A: 

My guess is that mrf isn't null, even if it's empty or something else. It could also be Nothing, which is different from null in VBA land (I think). Try running the code in the debugger and looking at the value of mrf. Depending on what mrf is, you can do a different test (like check len(mrf) or not isNothing(mrf) or if it's an integer, and it's init to zero, then mrf <> 0.... you get the idea. Hope that helps!

Dan Coates
+1 tomorrow as well, thanks!
Terry
+4  A: 

If mrf is Variant, then it's initially Empty, not Null. You therefore have to use IsEmpty() function.

GSerg
Awesome this fixed it! +1 tomorrow when I have more votes
Terry
Another option would be to initialize mrf to null. I noticed he isn't declaring any of his variables (min, max, mrf) which is in general a bad idea. Turn on "Option Explicit" and you'll avoid a lot of logic bugs like this.
JohnFx
+1  A: 

You might be interested in this piece on VB6/VBA null/nothing/empty:

http://blogs.msdn.com/ericlippert/archive/2003/09/30/53120.aspx

Lunatik