ACC2000: Conversion of Macro to Visual Basic Code Uses Unexpected Syntax with Form References (209935)
The information in this article applies to:
This article was previously published under Q209935 Advanced: Requires expert coding, interoperability, and multiuser skills.
This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).
SYMPTOMS
When you use the Convert Macros to Visual Basic command to convert a macro that contains a reference to a user-defined item on a form or report to a Visual Basic for Applications event procedure, the code syntax that is created from the conversion uses the . (dot) operator instead of the ! (exclamation point) operator for any implicit referencing in the procedure.
CAUSE
The Convert Macros to Visual Basic command does not differentiate between implicit and explicit referencing when a macro is converted to a VBA procedure.
RESOLUTION
In modules that are created from the Convert Macros to Visual Basic command, both operators are acceptable.
MORE INFORMATION
In Microsoft Access, the ! (exclamation point) operator and the . (dot) operator are used as identifiers to indicate whether a particular reference is implicit ( !) or explicit ( .). Implicit referencing usually means that you are referring to a user-defined item. Explicit referencing usually means that you are referring to an item defined by Microsoft Access. However, when you use the Convert Macros to Visual Basic command, the distinction is not made for implicit and explicit form referencing.
For example, a macro with this condition and action
Condition Action
------------------------------
[PrintLabelsFor]=1 SetValue
and these action arguments
Action Arguments
--------------------------------------
Item: [SelectCountry].[Enabled]
Expression: No
is converted to the following:
With CodeContextObject
If (.PrintLabelsFor = 1) Then
.SelectCountry.Enabled = False
End If
.
.
End With
Notice the . (dot) operator that precedes PrintLabelsFor. The ! (exclamation point) operator should be used instead of the . (dot) operator.
Steps to Reproduce BehaviorCAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database. - Start Microsoft Access and open the sample database Northwind.mdb.
- Click Macros, and then click Customer Labels Dialog.
- On the Tools menu, point to Macro, and then click Convert Macros to Visual Basic.
- In the Convert macro: Customer Labels Dialog dialog box, make sure that the Add error handling to generated functions and the Include macro
comments check boxes are selected, and then click Convert.
- When the conversion process is finished, click OK in the Conversion Finished! message box.
- Open the Converted Macro-Customer Labels Dialog module in Design view, and then in the Procedure box, select Customer_Labels_Dialog_Print.
Notice the syntax created for the implicit form references. The actual code
created contains the . (dot) operator instead of the ! (exclamation point) operator.
The following sample code shows the correct syntax for the With statement, which should be created from the conversion:
With CodeContextObject
' Attached to the Print button.
If (!PrintLabelsFor = 1) Then
' Print all records.
DoCmd.OpenReport "Customer Labels", acNormal, "", ""
End If
If (!PrintLabelsFor = 2 And IsNull(!SelectCountry)) Then
' If no country is selected, display a message...
Beep
MsgBox "To preview or print labels, you must either pick a country from the list or click All Countries.", vbOKOnly, "Pick a Country"
' ...go to the SelectCountry combo box...
DoCmd.GoToControl "SelectCountry"
' ...and stop the macro.
Exit Function
End If
If (!PrintLabelsFor = 2) Then
' Print records for selected country.
DoCmd.OpenReport "Customer Labels", acNormal, "", "[Country]=[Forms]![Customer Labels Dialog]![SelectCountry]"
End If
' Close the Customer Labels Dialog form.
DoCmd.Close acForm, "Customer Labels Dialog"
End With
REFERENCESFor more information about using the ! and . operators in expressions, click Microsoft Access Help on the Help menu, type use the ! and . (dot) operators in expressions in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
Modification Type: | Minor | Last Reviewed: | 9/28/2004 |
---|
Keywords: | kbprb kbprogramming KB209935 |
---|
|