Full Version: Text appears as "variant/integer"
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
pondog
I'm trying to carry the contents of a text box from one record to the next as they're entered. I've been able to figure out how to do the combo boxes (Huge thanks to the forum for help in that) but I can't get the text box to do the same. I always end up with a "Type mismatch" error. I've done everything I can think of. My code look like this:

Me.Course.DefaultValue = Nz(Me.Course.Column(0))
Me.CriteriaNumber.DefaultValue = Nz(Me.CriteriaNumber.Column(0))
Me.ObjecNum.Value = Str(Me.ObjecNum)

When I set up a watch on the last line of code and then enter letters into the text box I find that Str(Me.ObjecNum) is shown as a "variant/integer" and I get the "Type mismatch"error. If I enter numbers only in the textbox then Str(Me.ObjecNum) shows "variant/string" and passes the value. What am I doing incorrectly?

Thanks in advance,
This forum is the best resource I've found,
Tony
balaji
Maybe you are trying to use the cstr() function and mistyped it as str(). str() returns a variant while cstr() returns a string.
datAdrenaline
Maybe this ...
QUOTE
Me.ObjecNum.Value = Str(Me.ObjecNum)

Should Be this ...
CODE
Me.ObjecNum.[color="red"]Default[/color]Value = Me.ObjecNum.Value
pondog
I'm not sure what happened to my response. I responded earlier but I don't see it so I'll respond again. Me.ObjecNum.DefaultValue = CStr(Me.ObjecNum.Value) finally ended up working. That is, it returns a string to the default value. My text box shows #NAME? even though the Default Value listed in the Properties shows the string. I've tried everything I can to get the default value to show in the text box but I always get the #NAME? error. I'm sure there's something simple I'm overlooking but I can't figure it out. I'm hoping you might have some suggestions.

Thanks for all the help on the string. I've tried so many combinations of the code your suggested, just never with the .Value object. Thanks again!
datAdrenaline
Sorry ... I gave you bad advice ... In setting a default value, you need to set it to a string value that represents an expression ... Access will attempt to put a virtual (or real) '=' sign in front of it for you but so your text defaults worked great. But with numbers, it seems Access has a little bit of trouble. So with all that your other two DefaultValue statements work but should actually be written as follows:

(Note the quotation marks ... the goal is to create a string the represents an expression .. so the result will be something like --> ='MyString' or =MyNumber )
CODE
Me.Course.DefaultValue = "=[color="blue"]'[/color]" & Nz(Me.Course,"") & "[color="blue"]'[/color]"
Me.CriteriaNumber.DefaultValue = "=[color="blue"]'[/color]" & Nz(Me.CriteriaNumber,"") & "[color="blue"]'[/color]"


Note: The .Column() property will ALWAYS return text, UNLESS the user has not made a selection in the combo. That is important to know, because sometimes a selection is made in the combo and programmers will test for a NULL in another column, and it is never NULL ... the NULLs are converted to a ZLS (zero length string). Also .Colomn(0) is usually the bound column .. so it is really not needed, but it does not harm anything, as long as you know about the handling of the NULLs --> ZLS

SO .... With all that here is how to set the defaultvalue for the ObjectNum control

If ObjectNum is a numeric value (stored in the table as a number of some sort) then do this ..

CODE
Me.ObjecNum.DefaultValue = "=" & Me.ObjecNum.Value
Note: .Value is the 'Default' property and could be left off

This will set the defaultvalue STRING to =1 or =2 or whatever the number will be

If ObjectNum is a TEXT value in the table it resides in then do this:
CODE
Me.ObjecNum.DefaultValue = "=[color="blue"]'[/color]" & Me.ObjecNum.Value & "[color="blue"]'[/color]"



I hope that helps ... sorry for the misleading original post ... I guess I had brain freeze!!
pondog
It Works!! WOW!! I've been trying to get this for the past two weeks (as shown by my posts). I never would have figured that the string value needed to be presented as an expression. After having seen the results in the Properties box I now understand why I've seen the "=" sign on other forms.

So I've learned the following:
1. Default value strings need to be presented as expressions.
2. The use of Quotation Marks in building strings (though I'll have to research the use of the apostrophe " ' ")
3. The use of the Ampersand "&" to build a string
4. The Column() ALWAYS returns Text UNLESS.....
5. Column(0) is usually the bound column so is not needed.
6. The .Value is the default value and can be left off.


I appreciate all this info. This has indeed been a very informative post. Thanks!!
datAdrenaline
Glad to be of assistance ...

One thing ...

QUOTE
6. The .Value is the default value and can be left off.


.Value is the default Property of a text box control not the default value

What that means is that if you refer to a text box control in your code the value of the .Value property is returned.

Most, if not all, control types that display data have a default property, MOST of the time it is a logical choice ... but sometimes it can bite you ... for example with a combo box on the form you may see the value from Column(1), which is the second column in the SQL/Query] however, the default property of the combo box returns the value from the BOUND column (Column(0)) ... So in code if you reference the combo WITH a specific column, the value of the BOUND column is returned, not the first VISIBLE column.. The same is true for a list box.

Hope that all helps ...
pondog
I see. I was confusing Value for Property. Thanks for setting me straight on that. I'm sure that will help to alleviate possible future headaches.

Thanks again,
Tony
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.