Something I didn't know - default property & Null
Sorry if this should be posted in a different forum; mods can move it if they want.
Lots of times you can leave off a control's default property when referring to the control, i.e. strFoo = txtBar instead of strFoo = txtBar.Text. Supposedly, this is a little faster, according to my VB5 textbook years ago.
But I didn't know until today that if, say, the textbox is empty, that method throws error 94, Invalid Use of Null. If I use txtBar.Text, it works fine, returning an empty string.
I know I could use Nz() to check the value, but it's easier to just add .Text.
Re: Something I didn't know - default property & Null
Which app did you experience this with?
Re: Something I didn't know - default property & Null
Sorry, Access 2K.
Maybe using the default property makes Access treat the property as a variant, rather than text or a string?
Re: Something I didn't know - default property & Null
Or could it be that your setting a field = to the textbox? Is the textbox a bould control?
Re: Something I didn't know - default property & Null
Nope. Unbound controls. The type of code that throws the error is strAns2 = txtAns2, if txtAns2 is empty. txtAns2.Text works, empty or not.
Maybe it's me. I get weird stuff sometimes, like a project called "utility" that once in a while appears as a 2nd project in the Project Explorer window. That has two forms, autodial and zoomform, & 4 modules, none of which are viewable. next time, the project may have disappeared.
Re: Something I didn't know - default property & Null
Nope, it is a recreatable error. I did this and I got error 94. I guess there is no default property if the textbox
doesnt have the focus. If it has focus then the text property should return a nullstring instead of a Null.
VB Code:
Dim str As String
'str = Forms![Form1]![txtTextBox]
str = Null '<same error as the line above
MsgBox str
Re: Something I didn't know - default property & Null
It happens even when the focus is set to the textbox. If it's empty, and no .Text, error 94 pops up. It's not a bug, it's an undocumented feature...
Re: Something I didn't know - default property & Null
Yes, a undocumented feature. :D
But if you try to do this you obviously understand the error - "str = Null" - Invalid use of Null.
So if the textbox is empty and you dont include the property then consider it as a Null. :D