PRB: WIDTH # Statement Raises Error in Visual Basic 4.0 (145678)



The information in this article applies to:

  • Microsoft Visual Basic Learning Edition for Windows 5.0
  • Microsoft Visual Basic Learning Edition for Windows 6.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Professional Edition, 16-bit, for Windows 4.0
  • Microsoft Visual Basic Professional Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Enterprise Edition, 16-bit, for Windows 4.0
  • Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic for Applications 5.0

This article was previously published under Q145678

SYMPTOMS

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 the Visual Basic Help file does not work in a form.

STATUS

Microsoft has confirmed this to be an issue in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

The problem is that Visual Basic 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.

Steps to Reproduce

  1. Start a new project in Visual Basic. Form1 is created by default. 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.

Modification Type:MajorLast Reviewed:5/7/2003
Keywords:kbprb KB145678