Microsoft KB Archive/137121

= ACC: Do Not Jump into WITH Statement Blocks =

Article ID: 137121

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q137121





SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.

The With statement enables you to perform a series of references to a specified object without having to repeat the name of the object. For example, if you have a number of different properties to change on a single object, it is more convenient to place the property assignment statements inside the With control structure. In this manner, you have to refer to the object only once instead of having to refer to it with each property assignment.

The following example illustrates the use of the With statement to assign values to several properties of the same object: With MyLabel .Height = 2000 .Width = 2000 .Caption = "This is MyLabel" End With The With statement behaves differently from other block structures in the Basic programming language. The With statement does not allow you to use a GoTo statement to enter the With/End With statement block. Other block structures like "If/End If" and "Select Case/End Select" in the Basic programming language support the GoTo statement.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.



MORE INFORMATION
To see the behavior of the With statement, follow these steps.

CAUTION: Following the steps in this example will modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file or perform these steps on a copy of the Northwind database.

 Open the sample database Northwind.mdb. Create a module and type the following line in the Declarations section:

Option Explicit

  Type the following procedure: Sub TestWith(MyControl As Control, Optional Action As Variant) On Local Error GoTo Tester_Err Dim MyAction As Integer Const JumpIn = 1 Const JumpOut = 2 Const JumpIntoIf = 3 Const JumpIntoSelect = 4 MyAction = CInt(Action)

If MyAction = JumpIn Then ' Jump to the WITH block. GoTo Tester_Jump1 ElseIf MyAction = JumpIntoIf Then ' Jump to the IF block. GoTo Tester_Jump2 ElseIf MyAction = JumpIntoSelect Then ' Jump to SELECT block. GoTo Tester_Jump3 End If

' Start With block. With MyControl .Caption = "Now Testing" Tester_Jump1: If MyAction = JumpOut Then ' Jump out of a WITH block GoTo Tester_End End If            .ForeColor = 255 End With

' If block test. If MyAction Then Tester_Jump2: End If

' Select case block test. Select Case MyAction Case JumpIntoSelect Tester_Jump3: End Select Tester_End: Exit Sub Tester_Err: MsgBox Error$ Resume Tester_End End Sub  Save the module and close the Module window.  Create the following form with a command button, and then save it as Test1:       Form: Test1 -      Caption: TestForm ControlSource:

Command Button: Name: btnTest Caption: My Test OnClick: [Event Procedure]   Set the OnClick property of btnTest command button to the following event procedure: Private Sub btnTest_Click Const JumpIn = 1 Const JumpOut = 2 Const JumpIntoIf = 3 Const JumpIntoSelect = 4

Call TestWith(ActiveControl) MsgBox "Normal Use of [WITH-END WITH] block" Call TestWith(ActiveControl, JumpIn) MsgBox "Jumped into [WITH-END WITH] block" Call TestWith(ActiveControl, JumpOut) MsgBox "Jumped out of [WITH-END WITH] block" Call TestWith(ActiveControl, JumpIntoIf) MsgBox "Jumped into a [IF-END IF] block" Call TestWith(ActiveControl, JumpIntoSelect) MsgBox "Jumped into a [SELECT-END SELECT] block" End Sub </li> Save the form and view the form in Form view.</li> Click the btnTest command button. Note that the following message appears:

Normal Use of [WITH-END WITH] block

This is the normal usage of the With statement. The Caption of the command button changes to "Now Testing," and the button's ForeColor property changes to red (255).</li> Click OK in the message box to continue the program. Note that the following error message appears:

Object variable or With block variable not set

This error indicates that the With statement does not support a GoTo entrance into the statement. The next message is:

Jumped into [WITH-END WITH] block

</li> Click OK in the message box to continue the program. The next message is:

Jumped out of [WITH-END WITH] block

This message indicates that you can use the GoTo statement to exit a With block.</li> Click the btnTest command button again. The next message is:

Jumped into a [IF-END IF] block

This message indicates that the If/End If block statement supports the use of the GoTo statement to enter the block.</li> Click the btnTest command button again. The next message is:

Jumped into a [SELECT-END SELECT] block

This message indicates that the Select/End Select block statement supports the use of the GoTo statement to enter the block.</li></ol>

<div class="references_section">