Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 20, 1999

Perform Complex Tests in the Immediate Window

If you've ever tried to type a small program into the Immediate window or the Debug window in Access, you might have found that the style of VB programming you use in most of your code didn't work there. For example, this code doesn't work when you type it into the Debug window because one of the statements takes more than a single line:

 
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset( _
	"SELECT * FROM MyTable")
If rs.RecordCount = 0 Then
	Beep
	Debug.Print "Hello"
ElseIf rs.Updatable Then
	DoThis()
	DoThat()
End If

You should almost always use Option Explicit when writing regular code, but it is neither necessary nor available in the Immediate window. You never need to declare a variable, as in the first line of the previous snippet. When you refer to a variable name for the first time, it is implicitly created. Remember that Variants of type Object are late-bound, which means you don't see the AutoSyntax box for implicitly created object variables.

Using the colon executes more than one statement on a single line:

 
Debug.Print "A" : Beep

For example, this code prints an "A" in the Immediate window and then beeps. By itself, this isn't highly valuable, but using the colon construct in the right place allows you to reformat most VB statements for the Immediate window.

To handle the multiple-line If...ElseIf...Else...End If construct in the Immediate window, use a single-line If...Else...Then construct-nested, if necessary-with the colon construct. The syntax single-line version of the If statement is simple:

 
If <condition> Then <statement> [Else <statement>]

Notice there is no End If or ElseIf clause, and the Else clause is optional. Here is the previous snippet, correctly formatted for the Immediate window with ElseIf replaced by a nested If:

 
Set rs = CurrentDb.OpenRecordset( _
	"SELECT * FROM MyTable")
If rs.RecordCount = 0 Then Beep : Debug.Print _
	"Hello" Else If rs.Updatable Then DoThis() : _
	DoThat()

(Editor's Note: Line continuation characters also aren't allowed in the Immediate window, although they're required in this example to fit the supplement's layout.)

Be careful when converting If statements from multiple-line to single-line. Unusual or complex nesting might require special logic.

The colon also allows you to perform loops in the Immediate window. VB declares the looping variable implicitly:

 
	For Each v In MyCollection : ? v.Description _
		: Next v
Chase Saunders
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date