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.
“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…
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.
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.