Output Misdirected Using Print# and Debug.Print with Function

ID Number: Q84551

1.00

WINDOWS

buglist1.00

Summary:

When you print the return value of a function to a file, and in that

function you send output to the Immediate window during Visual Basic

design mode, the value sent to the file and the value sent to the

Immediate window will incorrectly be switched.

To work around this problem, assign the return value from the function

to a temporary variable and then print that variable to the file.

Microsoft has confirmed this to be a problem with Microsoft Visual

Basic programming system version 1.0 for Windows. We are researching

this problem and will post new information here as it becomes

available.

More Information:

This problem occurs only if you are sending sequential output to a

file and to the Immediate window. You can use either PRINT or WRITE

and the problem still occurs.

Steps to Reproduce Problem

--------------------------

1. Run Visual Basic, or from the File menu, choose New Project (ALT,

F, N) if Visual Basic is already running. Form1 is created by

default.

2. Enter the following code:

Sub Form_Click ()

Open "Test.Txt" for Output as #1

Print #1, MyFunct()

Close #1

End Sub

Function MyFunct () as String

Debug.Print "This goes to file (instead of to Immediate window)"

MyFunct = "This goes to Debug window (instead of to the file)"

End Function

3. Press F5 to run the program.

When you click on the form, you would expect to see the value sent by

Debug.Print in the Debug window. Instead, you get the return value

from the function. The contents of the TEST.TXT file, which should

contain the return value of the function, instead has the value that

was sent to the Immediate window.

This problem occurs even if the value sent using Debug.Print and the

value sent by Print # are of different data types.

Workaround

----------

To work around this problem, change the Form_Click event to the

following:

Sub Form_Click ()

Open "Test.Txt" for Output as #1

X$ = MyFunct ()

Print #1, X$

Close #1

End Sub

Additional reference words: 1.00