MS Access - simple problem on a form

I have a database that has 1 form. The form has 1 button and 1 text box.

When the button is clicked, it should check to see if the text box is empty and return the message “Enter Something” if it is.

If the text box is not empty it should return a message with the contents of the text box.

I’m using the following code for the button;


Private Sub Command2_Click()

Dim MyTemp As Variant
Dim MyText As Variant
Dim db As DAO.Database

Set db = CurrentDb()

MyTemp = Me![Text0]

If MyTemp = Null Then
MyText = "Enter Something"
Else
MyText = MyTemp
End If

MsgBox MyText

End Sub

However I’m getting an error if the text box is empty and I can’t figure out why.
It doesn’t seem to be evaluating the “If” statement and assigning the value of “Enter Something” to MyText.

I get an “invalid use of null” error on the MsgBox line.

Any thoughts?
I could e-mail anyone a copy if it would help.

Can anyone explain this to me?

“if [whatever] = null then” does not work intuitively in VBA or any species of visual basic, in fact it does nothing at all useful. Null does not equal null.

Try “If MyTemp is Null Then” That should get you the results you want i believe.

If chrisk’s advice doesn’t work, try something along the lines of

If Trim(MyTemp) = “” Then

Textboxes and the like don’t usually return NULL, just an empty string. If you use trim, you can also catch if a person has just entered a space. Of course, I don’t use Access a lot…

I tried that and now I get the error:

Run time error ‘424’ object required

Have you seen this before?

This works:

If IsNull(Me.Text0) Then
MsgBox “empty”
End If

Yeah, hmmm… doesn’t like comparing a variant to null huh? Well, we could try ishmael’s advice, which seems good. I’d try rewriting the code something like this:


Private Sub Command2_Click()

Dim MyTemp As string
Dim MyText As string
Dim db As DAO.Database

Set db = CurrentDb()

MyTemp = Me![Text0].text

If trim(MyTemp) = "" Then
MyText = "Enter Something"
Else
MyText = MyTemp
End If

MsgBox MyText

End Sub

Let me know if you still get any null errors (or any error messages at all) with that. Using string variables instead of variants and calling the .text property explicitly will tighten up your code and may help catch out what the problem is here.

OK - I tried that and I still get the original error.

:frowning:

I just tried this myself and it looks like we’re missing a step. Before checking the textbox value, add in a line saying “Me![Text0].setfocus”

Access doesn’t like reading controls that don’t have the focus. That might be where the nulls were coming from.

Now I get:

Run-time error ‘2185’:
You can’t reference a property or method for a control unless the control has the focus.

Just use if IsNull(Me.Text0) then… fercryinoutloud!

Are you sure that will actually accomplish the goal?? (Not just testing for null… actually reading the textbox value.)

Zoid, if you haven’t noticed yet, post 8 contains the solution to the error message in post 9.

Can’t you just :

Private Sub Command2_Click()

If IsNull(Me.Text0) Then
MsgBox “Empty”
Else
MsgBox Me.Text0.Value
End If
End Sub

Works for me…

LOL - Sorry missed your first post :smiley:

Works just fine!
Thanks!

Sorry - I’m a little slow today - I just saw that.

Well, it’s supposed to work, also, I tested it and it did work, if that’s what you mean…

Hmmm… yes, that works too. Strange.

Despite what the error message 2185 says, you can access the value property for a textbox that doesn’t have the focus… but not the text property. (sigh.)

This is why I like VB6 better, hehe.

Glad you got it working zoid. Hope I didn’t just confuse the issue.

Sorry, guess I was being a little snippy. There are multiple ways to accomplish any goal, and I don’t know everything…
[sub]yet![/sub] :smiley:

I’ve seen that before; Access is a bit funny about that, I’m not sure why, but I’d love to know, if there’s anyone watching that knows the answer.

Sorry, I didn’t mean the reply to read quite as curtly as it turned out…

…and zoid instigates an Access THROWDOWN! :smiley:
Seroiusly, thank you ALL for your help and let me just say yet once again…

There is NOTHING you guys don’t know!