Previous  Home  Next

Immediate window

Here's the overall VB IDE screen again so you can orient yourself:

screenshot of Visual Basic editor

Press Ctrl+G to view the Immediate window if it's not already visible:

screenshot of Immediate window

The Immediate window is a handy debugging and code testing tool.

At nearly any time, you can type code into the Immediate window and press Enter to execute it.
If you need to type a short series of commands to execute at once, type a colon (:) instead of pressing Enter. For example:

MsgBox("Something"):MsgBox("Something Else")

To display the value of a variable or other expression in the Immediate window, use the ? operator. For example, to see the Answer to Life, The Universe and Everything:

? 6*7

You can also have VB display information in the Immediate window while code is running.

Sub SomeTestCode()
	Dim sComputerName as String
	Dim iUltimateAnswer as Integer
	Dim x as Integer

	sComputerName = "Deep Thought"
	iUltimateAnswer = 42

	' Now let's try several possible answers and see what Deep Thought thinks of them:
	For x = 1 to 10
		' Instead of annoying the user, just display intermediate results to the Immediate window
		' using Debug.Print
		If x * 7 = iUltimateAnswer Then
			Debug.Print "Deep Thought says INCORRECT.  Not " & cstr(x*6) & _
				" you pathetic organic lifeform."
		Else
			Debug.Print "Deep Thought says Hmmm.  You may be on to something, O Wet One"
		End If
	Next x

End Sub

Output from Debug.Print appears only in the Immediate window. Unless you're running or later viewing the results in the IDE, you won't see it. This means that you can leave debugging information in your code without the end user seeing it.

Click Next to continue

Previous  Home  Next