ACC2000: Cannot Call Class Module Methods from MS Access ObjectsID: Q208202
|
Advanced: Requires expert coding, interoperability, and multiuser skills.
When you call a class module method directly from a query, form, report, or
macro, you receive an error message.
In order to call a class module procedure, the calling object must initialize an instance of the class. Access objects, such as queries, forms, reports, and macros, cannot automatically initialize new instances of a user-defined class. Only a Visual Basic for Applications procedure can initialize a new instance of a user-defined class.
The following are two possible workarounds:
Option Explicit
Function MultiplyByTen(clsVar As Variant) As Variant
MultiplyByTen = clsVar * 10
End Function
Option Explicit
Function CallMultiplyByTen(stdVar As Variant) As Variant
Dim clsMultiply As New MultiplyClass
CallMultiplyByTen = clsMultiply.MultiplyByTen(stdVar)
End Function
?CallMultiplyByTen(5)
Note that the procedure returns the number 50 to the Immediate window.
Query: Query1
-----------------------------------------
Type: Select Query
Field: OrderID
Table: Orders
Field: Freight
Table: Orders
Field: EXPR1: CallMultiplyByTen([Freight])
Option Explicit
Function MultiplyByTen(clsVar As Variant) As Variant
MultiplyByTen = clsVar * 10
End Function
Query: ClassTestQuery
--------------------------------------
Type: Select Query
Field: Freight
Table: Orders
Field: Expr1: MultiplyByTen([Freight])
Undefined function 'MultiplyByTen' in expression.
Form: ClassTestForm
-------------------------
Caption: TestForm
ControlSource: Test Table
Text box:
---------------------------
Name: Freight
Caption: Freight
ControlSource: Freight
Text box:
------------------------------
Name: Text1
Caption: Text1
ControlSource: =MultiplyByTen([Freight])
Report: ClassTestReport
---------------------------------------
ControlSource: Orders Table
Text box:
---------------------------------------
Name: Freight
Caption: Freight
ControlSource: Freight
Text box:
---------------------------------------
Name: Text1
Caption: Text1
ControlSource: =MultiplyByTen([Freight])
Macro Name Action
--------------------------------
ClassTestMacro MsgBox
ClassTestMacro Action Arguments
--------------------------------
MsgBox
Message: =MultiplyByTen(5)
Beep: Yes
Type: None
You may also receive the following error message when you call a class module method directly from a macro:The expression you entered has a function name that Microsoft Access can't find.
Microsoft Access can't find the name <class name> you entered in the expression.
For more information about class modules, in the Visual Basic Editor, click
Microsoft Visual Basic Help on the Help menu, type "class modules" in
the Office Assistant or the Answer Wizard, and then click Search to
view the topic.
For additional information about programming with class modules, please see the following article in the Microsoft Knowledge Base:
Q160007 Introduction to Stand-Alone Class Module Programming
Additional query words: prb
Keywords : kbcode kbprg kbdta
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: May 13, 1999