Using the WithEnd With statements

If you want your code to change several properties of a control, you can use a With...End With block of code to make your code easier to read. For example, if you want your code to change several properties of a control named myControl on a form named myForm (and the code isn't in a class

module), you could include that lengthy identifier on every line of code, as shown here:

Forms!myForm.myControl.BackStyle = 1 Forms!myForm.myControl.BackColor = vbWhite Forms!myForm.myControl.ForeColor = vbRed Forms!myForm.myControl.SpecialEffect = acEffectNormal Forms!myForm.myControl.FontBold = True

Or, you can use a With...End With block of code:

With Forms!myForm.myControl .BackStyle = 1 .BackColor = vbWhite .ForeColor = vbRed .SpecialEffect = acEffectNormal .FontBold = True End With

Most programmers prefer to write using the latter format because the code is easier to read. When executing the code, VBA understands that With Forms!myForm!myControl means that all the property settings to follow (up to the End With statement) are to be applied to the object named

Forms!myForm.myControl.

0 0

Post a comment

  • Receive news updates via email from this site