WIDTH # Statement Raises Error in Visual Basic 4.0

Last reviewed: February 8, 1996
Article ID: Q145678
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, version 4.0, for Windows

SUMMARY

The WIDTH # statement is used to assign an output-line width to a file opened using the Open statement. However, the sample code given in Visual Basic 4.0 does not work in a form.

STEPS TO REPRODUCE

  1. Use Visual Basic 4.0 to start a new project and place the following code in Form1's Form_Click Event.

       Private Sub Form_Click()
         Open "TESTFILE" For Output As #1  ' Open file for output.
         Width #1, 5 ' Set output-line width to 5.
         For I = 0 To 9  ' Loop 10 times.
             Print #1, Chr(48 + I); ' Prints 5 characters per line.
         Next I
         Close #1    ' Close file.
       End Sub
    
    

  2. Run the program and then click the form. An "Invalid use of property" error is generated.

MORE INFORMATION

The problem is that Visual Basic 4.0 interprets the "Width" statement as a property of the Form when the code is run from a Form module. When run from a Code module or a Class module, the statement functions as documented because there is no default object on which to apply the Width property. Visual Basic 4.0 can be forced to use the correct Width statement in this case by specifying the call using "Typelib.Method" syntax. The Width statement is actually a "Visual Basic for Applications" (VBA) method, so the following syntax must be used:

   VBA.Width filenum, width

   Private Sub Command1_Click()

      Open "TESTFILE" For Output As #1
      VBA.Width 1, 5 ' Note:  "#" symbol omitted
      For I = 0 To 9
         Print #1, Chr(48 + I);
      Next I
      Close #1

   End Sub

NOTE: The compiler will not accept this statement unless the "#" symbol is removed.


Additional reference words: 4.00 vb4win vb4all
KBCategory: kbusage
KBSubcategory: IAPVBA


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 8, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.