Results 1 to 9 of 9

Thread: [RESOLVED] Value of a fixed string

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    71

    Resolved [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??

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Value of a fixed string

    Quote Originally Posted by techgnome View Post
    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Value of a fixed string

    Ok, so that was confirming what I suspected was going on...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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))
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    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).

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Value of a fixed string

    Quote Originally Posted by Logophobic View Post
    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'
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    71

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width