Article ID: 137121
Article Last Modified on 1/19/2007
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.
Option Explicit
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
Form: Test1
-----------------------------
Caption: TestForm
ControlSource: <unbound>
Command Button:
Name: btnTest
Caption: My Test
OnClick: [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
Additional query words: loop
Keywords: kbinfo kbprogramming KB137121