Str() seems to add a space character, why?

mdlueck

Sr. Application Developer
Local time
Today, 07:22
Joined
Jun 23, 2011
Messages
2,648
Dealing with reading data out of unbound field controls, for some reason one field was ending up with a "Variant/Integer" value, so I thought to use Str() to insure that it was a string to successfully use in an If comparison with another field control value.

Simply calling
Code:
Str(MePointer.fldfarev.Value)
was adding a space character to the resulting string!

So I wound up adding Trim() as:

Code:
Trim(Str(MePointer.fldfaver.Value))
Why in the world would Str() be adding a space character which was clearly not there in the Integer value of the field control?
 
interesting observation, michael

i can't think i have uses str as a function directly

maybe cstr is more accurate


Sub tryit()
MsgBox (Len(Str(12))) 'returns 3
MsgBox (Len(CStr(12))) ' returns 2
End Sub

[addendum]
I just checked str for help, and it says an extra space is added/reserved for the sign - hence (+)12 = length 3.
 
Dealing with reading data out of unbound field controls, for some reason one field was ending up with a "Variant/Integer" value, so I thought to use Str() to insure that it was a string to successfully use in an If comparison with another field control value.
Yet again another enthusiastic programming approach Michael. How is it possible for your control to return Variant/Integer if you haven't coded it to do so somewhere? I would advise you find the culprit and not just band-aid fix it. Use the Locals window and step through your code - looking at the Data Types.

By the way, if you want to convert a Number to String you can do so explicitly:
Code:
CStr(MePointer.fldfarev.Value)
Mid(MePointer.fldfarev.Value, 1)
Or implicitly:
Code:
MePointer.fldfarev.Value & vbNullString
These are common programming concepts I thought you would know, especially the latter.

Yes CStr() will fail if Null is the parameter, but you already know what to do. Mid() will return Null for Null. Again, you know what to do there.
 
maybe cstr is more accurate

That was it, thanks! I forgot that C prefix. Indeed, where Str() accpets an Int arg, CStr accepts (expression).

I thought I did not remember the function to convert to the string representation requiring an Int.
 
OT: Note that Str is a lifesaver, when writing SQL-statements containing decimal numbers in non-US/UK enviroment, since it uses "." for decimal separator (as required in the SQL), irrespective of locale.
 
How is it possible for your control to return Variant/Integer if you haven't coded it to do so somewhere?

I believe that answer is due to the fact that yet again this is an unbound form with unbound controls.

The datatype will be Int if the program populated the field, and Str if it was empty and someone keyed in a value.

Unbound fields, unlike variables, are not specifically datatyped.

It is rare that I have form business logic comparing values in various fields and performing computations dynamically on form data. But on this form such is what I have.
 
I thought you were initialising your controls on the form's Load/Open event?
 
I thought you were initialising your controls on the form's Load/Open event?

Add record forms do not initialize all controls, only the read-only application context controls.

The validation class must be able to work with both the Add/Edit form universally.
 
Urm, what do you mean by "Add record forms"?

The form tied to SQL INSERT as opposed to the form tied to SQL UPDATE.

The record list forms are read-only browsing records in an FE Temp Table. Add / Edit buttons bring up the Add / Edit Record Forms. Those, unlike the record list forms, have all UNBOUND controls.

The Commit button on the Add Form validates the data and if approved issues an SQL INSERT.

The Commit button on the Edit Form validates the data and if approved issues an SQL UPDATE.

Thus the validate code needs to be safe for both Add/Edit forms.
 
Then you should have extended the validation class for those controls to be initialised.

All I'm saying is based on your comment below, if you're having inconsistent return values then there's a problem somewhere which should be found and fixed.
Dealing with reading data out of unbound field controls, for some reason one field was ending up with a "Variant/Integer" value,
 
Then you should have extended the validation class for those controls to be initialised.

In the case of fields which are going to map to a numeric column, how would I blank initialize those fields AND at the same time have them be a numeric data type?

Fields and variables are a bit different... fields may be empty, but numeric variable types can not be NULL.

In the case of optional fields which are of numeric datatype, I use a NULL indicator value (-1 usually) in VBA and use that to store blank in the DB field (along with coding a default value of NULL in the SP), then when I populate the form I map -1 --> Blank field, else Value --> field.

I do not see so simple a solution to justify your attitude towards me, vbaInet. You have been very helpful to me. Would it be to much to ask for you to cut me some slack? I am a team of one on a large project acting as architect / application developer / DBA / software distribution engineer / tester / etc...

The topic of NULLable numeric was rather complicated to work out a standard which would satisfy all three:
1) DB fields
2) VBA numeric variables
3) Access form field controls
I consider what I arrived at quite a workable arrangement

The one odd twist THIS TIME was I wanted to code a comparison between form fields, which is not done on any other form in this application.
 

Users who are viewing this thread

Back
Top Bottom