Knowledge Base

PRB: WIDTH # Statement Raises Error in Visual Basic 4.0

Article ID: 145678

Article Last Modified on 5/7/2003


APPLIES TO


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.

Additional query words: kbVBp400 kbVBp500 kbVBp600 kbVBp kbdsd kbDSupport kbVBA500 kbCompiler

Keywords: kbprb KB145678