Consulting

Results 1 to 5 of 5

Thread: Checking for empty strings

  1. #1
    VBAX Regular
    Joined
    Apr 2010
    Posts
    28
    Location

    Checking for empty strings

    If I want to check a string variable for lack of content, I usually use:

    If Trim($var) = vbNullString...

    (There is probably a better way to do it, but I have used this in so many places .....)

    I have now hit a problem with some strings that have to be of fixed length and trimming doesn't change that. So:

    dim $var as String * 25
    If Trim($var) = vbNullString ...

    will always return "False" because if it doesn't contain anything else, $var will contain 25 spaces.

    Is there any way to check for this state other than comparing it to a string of 25 spaces?

    TIA

    Geoff

  2. #2
    VBAX Tutor
    Joined
    Mar 2014
    Posts
    210
    Location
    A trim of string does not produce NULL. It will = "" (empty set)

  3. #3
    VBAX Regular
    Joined
    Apr 2010
    Posts
    28
    Location
    You can check for an empty thread by checking for vbNullString. Not Null. vbNullString works as equal to ""

    But my question remains the same. Where the string is stuck at a certain number of spaces because it is a fixed length string, is there an easy way to check that it is just a string of spaces of that length?

    Geoff

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    If (Len(StrString) = 25) And (Trim(StrString) = vbNullString) Then ...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular
    Joined
    Apr 2010
    Posts
    28
    Location
    Many thanks

Posting Permissions

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