Microsoft KB Archive/210230

= ACC2000: Jumping into WITH Statement Blocks Is Not Allowed =

Article ID: 210230

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q210230



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

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).



SUMMARY
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.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To see the behavior of the With statement, follow these steps:  Start Microsoft Access and open any database or project. 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  Close the module window. Create a new form not based on any table or query with a command button, as follows:

Form

Caption: TestForm

Command Button

Name: btnTest

Caption: My Test

  Set the On Click property of the 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> 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 represents 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 in the message box:

Object variable or With block variable not set.

This error message indicates that the With statement does not support a GoTo entrance into the statement. Click OK. 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 OK 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 OK 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>

Additional query words: loop

Keywords: kbhowto kbinfo kbprogramming KB210230

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.