[RESOLVED] Value of a fixed string
I am trying to determine if there is any value in a fixed length string.
Given the following:
Dim Data As String * 25
Dim NewData As String
If I try the following 2 statements I get a type mismatch error
If Data = vbNull Then Debug.Print "Y" Else Debug.Print "N"
If Data = vbEmpty Then Debug.Print "Y" Else Debug.Print "N"
I have tried all of the following without success.
If Data = "" Then Debug.Print "Y" Else Debug.Print "N"
If Data = " " Then Debug.Print "Y" Else Debug.Print "N"
If Data = Space(25) Then Debug.Print "Y" Else Debug.Print "N"
If Trim(Data) = "" Then Debug.Print "Y" Else Debug.Print "N"
If Len(Trim(Data)) = 0 Then Debug.Print "Y" Else Debug.Print "N"
If Data = Null Then Debug.Print "Y" Else Debug.Print "N"
If Data = Empty Then Debug.Print "Y" Else Debug.Print "N"
NewData = Trim(Data)
If NewData = Empty Then Debug.Print "Y" Else Debug.Print "N"
If Left(NewData, 1) = " " Then Debug.Print "Y" Else Debug.Print "N"
If NewData = Space(25) Then Debug.Print "Y" Else Debug.Print "N"
What am I obviously missing??
Re: Value of a fixed string
of the lines there... these are the ones I'd expect to work:
If Data = Space(25) Then Debug.Print "Y" Else Debug.Print "N"
If Trim(Data) = "" Then Debug.Print "Y" Else Debug.Print "N"
If Len(Trim(Data)) = 0 Then Debug.Print "Y" Else Debug.Print "N"
Can't compare it to vbNull because strings in VB can't be null.
Can't compare it to vbNullString either because of the fixed width.
Can't compare it to vbEmtpyString because it isn't going to be empty.
I'm not sure why If Data = Space(25) doesn't work... as far as I can tell... that should be right...
Is there code between the dim and the if statements? I'm wondering if it doesn't actually exist because it never gets set to anything....
What happens if you add
Data = Space(25) before running the If statements? I'd be willing to bet that it starts working...
-tg
Re: Value of a fixed string
Quote:
Originally Posted by
techgnome
I'm not sure why If Data = Space(25) doesn't work... as far as I can tell... that should be right...
Won't work if "Data" has never been used/assigned a value.
However, once "Data" is assigned any value, it will return true each time if one were to erase "Data" later, i.e., Data = ""
One possible solution is after declaring Data, simply: Data = ""
Now it will compare to Space(25) in all scenarios
Re: Value of a fixed string
Ok, so that was confirming what I suspected was going on...
-tg
Re: Value of a fixed string
And to add more to this inconsistency.... ;)
Code:
Dim f as String *25
Debug.Print (f = Space(25)), (f = String(25, vbNullChar))
f = ""
Debug.Print (f = Space(25)), (f = String(25, vbNullChar))
Re: Value of a fixed string
There is no inconsistency, it works the same way every time. Fixed-length strings are initially zero. Assigning any value to a fixed-length string is the same as using LSet, which fills the string with spaces beyond the length of the assigned value.
Anyway, it may be sufficient to test the value of Asc(FixedLengthString).
Re: Value of a fixed string
Quote:
Originally Posted by
Logophobic
There is no inconsistency, it works the same way every time. Fixed-length strings are initially zero. Assigning any value to a fixed-length string is the same as using LSet, which fills the string with spaces beyond the length of the assigned value.
Anyway, it may be sufficient to test the value of Asc(FixedLengthString).
When compared to a dynamic length string; then I'd say there is inconsistency....
Code:
Dim f As String
Debug.Print (f = vbNullString), StrPtr(f)
f = ""
Debug.Print (f = vbNullString), StrPtr(f)
With all things VB; it's just a matter of knowing what to expect and being aware of all its 'features'
Re: Value of a fixed string
It also helps to use the String-typed functions instead of the Variant ones: Space$() not Space(), etc. This adds up both in speed and code size because you avoid all those implicit type conversions you have when Variants are used in expressions.
No variants please
Re: Value of a fixed string
If I set the field to any value, including "", then the compare does work. The problem surfaces when I a UDT record and when I create then some of the fields do not have a value, so for this to always work I would have to assingn "" to any field that does not receive some other value. That might not be a bad idea however.
If I set the Data field to "" before the statements then the following work. The others do not.
If Data = Space(25) Then Debug.Print "Y" Else Debug.Print "N"
If Trim(Data) = "" Then Debug.Print "Y" Else Debug.Print "N"
If Len(Trim(Data)) = 0 Then Debug.Print "Y" Else Debug.Print "N"
NewData = Trim(Data)
If NewData = Empty Then Debug.Print "Y" Else Debug.Print "N"
Thanks to all for the input.