VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 18

Testing and Executing VB Procedures

Although most of the uses to which you have put the Immediate window so far have required that a program be suspended in Break mode, you can do some things with the Immediate window that don’t require a program at all. If you want to execute a command or run a procedure that you have written, you can just type the command or procedure name (along with any necessary parameters) into the Immediate window, and then press the Enter key.

Assume, for example, that you want to delete a file using the Kill statement, but you aren’t sure what will happen if you put the command in your program and the file doesn’t exist. You can enter the command in the Immediate window twice in succession to see what it will do, as shown in Figure 18.19.

Deleting a file with the Kill statement.
FIGURE 18.19 Deleting a file with the Kill statement.

As you can see, nothing special happens in the Immediate window to indicate that the command succeeded. If you try to execute the Kill command again, however, you will generate a runtime error (#53: File not found) because the file was successfully deleted the first time.

Perhaps it would be nice to have a more informative way to delete a file. There is no way to modify the built-in Kill statement to provide extra information, but you can write your own file deletion function that does. Here is one that uses the Kill command to do its dirty work, but that uses the Immediate window to display its status along the way. Most important, the new function also provides a return value to indicate whether it succeeded:


Function Delete(sFilename As String) As Boolean
‘ use the return value in the calling function rather than
trap the error here
On Error Resume Next
Dim fReturn As Boolean
#Const DEBUGGING = True
‘ see if the file exists to delete
If Dir(sFilename) <> "" Then fReturn = True
Debug.Print "File exists = "; fReturn
#End If
If fReturn Then
‘ file exists, so kill it
Kill sFilename
‘ if couldn’t delete, set return value
If Dir(sFilename) <> "" Then fReturn = False
Debug.Print "File deleted = "; fReturn
#End If
End If
Delete = fReturn
End Function

If you run this function twice in succession, the Immediate window looks like Figure 18.20.

Results of the Delete function displayed in the Immediate window.
FIGURE 18.20 Results of the Delete function displayed in the Immediate window.

The Delete function is more informative than the Kill statement at debug time because it uses Debug.Print to keep you posted about its progress. Aside from the debugging output, Delete also gives you more to work with in your program. Because it provides a Boolean return value, you can test for the success of the file deletion and take appropriate measures in your program.

Because Delete is a function, it also enables you to display useful output in the Immediate window without sprinkling Debug.Print statements throughout its body. If you remove the "#Const DEBUGGING = True" line and run Delete from the Immediate window, you can use the Debug.Print statement to display its return value, as shown in Figure 18.21.

Results of the Delete function displayed in the Immediate window with the Debug.Print method.
FIGURE 18.21 Results of the Delete function displayed in the Immediate window with the Debug.Print method.

This gives you two ways to run a procedure from the Immediate window: with or without prefacing it with Debug.Print. If you just want to execute a procedure and disregard its return value (if it has one), don’t use the Print method. If you want to display the return value of a function, preface it with a leading question mark to invoke Debug.Print.

NOTE - Functions Versus Subs in the Immediate Window: You can treat a function like a sub in the Immediate window, but you can’t treat a sub like a function. If you inadvertently put a leading question mark in front of a sub that you try to run in the Immediate window, you will get this error message: Compile error: expected function or variable


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved