Page 101 - Open Soource Technologies 304.indd
P. 101

Unit 5: VB String and Operators



                5.1.2 Fixed-Length Strings
                Fixed length strings are automatically filled with spaces to pad them to their fixed-length. When
                working with fixed-length strings, you can use RTrim$ to trim the extra spaces.
                If a fixed-length string is declared, it will be filled with Null characters until it is used. The Visual
                Basic trim functions (RTrim$, LTrim$, and Mid$) will not trim Null characters, so be sure to
                assign a fixed-length string to an empty string immediately.
                Din Mystring As String* 10
                My String = “Chilkat” ‘Prints “Chikat”
                Print RTrin$ (My String) ‘Prints “Chilkat”
                Din Another String As String * 10
                Print Len (RTrim$ (Another String)) ‘Prints 10, because the string Contains Null characters.
                5.1.3 Working With Fixed Length Strings

                Strings in VBA are by their nature resizable. All of VBA’s string functions are used to set or
                retrieve portions of strings of variable length. You may find, however, that you need strings of
                a specific length, such as when you are writing text-based files that will be parsed by another
                application, or simply for alignment in list boxes.
                You can declare a string to be a fixed length with a declaration like
                Dim S As String * 20
                This declares a string of 20 characters. The disadvantage of this method is that the length of
                the string is always fixed at 20. You cannot resize the string “on the fly” at run time. You
                can neither lengthen nor shorten the string. Moreover, you must know the length of the
                string at design-time. You can’t create fixed length strings at run time. The SizeString
                function on this page can be used to create pseudo-fixed length strings. The strings are
                normal, sizable strings, by are sized a specified length, containing the specified text value on
                either the left or right region of the string.
                The following are a few VBA procedures for working with strings. These functions will work
                in any version of Office and any application. They will also work in Visual Basic 6.  There is
                nothing in the code that is specific to Excel.
                You can download a bas module containing these functions here.

                5.1.4 Testing For Fixed Length Strings
                At times, you may need to determine whether a string variable is a fixed length string or a
                sizable string. You cannot do this with a function procedure because VBA will convert fixed
                length strings to sizable strings when passing the string variable to a procedure. Instead, you
                must write code within the procedure in which the string is declared (unless it is a module-level
                or global variable) to test whether a string is fixed or sizable. The following code will do this.
                Dim S As String ‘ normal, sizable string
                Dim FS As String * 10 ‘ fixed length string, 10 chars
                Dim OrigLen As Long ‘ original length of string
                Dim OrigVal As String ‘ original value of string
                ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’



                                      LOVELY PROFESSIONAL UNIVERSITY                         95
   96   97   98   99   100   101   102   103   104   105   106