ACC97: Cannot Call Class Module Methods from MS Access ObjectsID: Q170532
|
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. Microsoft 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.
There 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
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
-------------------------------------
ControlSource: Orders 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
-----------------------------
MsgBox
Message: =MultiplyByTen(5)
Beep: Yes
Type: None
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, search the Help Index for "class
modules, overview."
For more information about programming with class modules, please see the
following article in the Microsoft Knowledge Base:
Q160007 ACC97: Introduction to Stand-Alone Class Module Programming
Keywords : kbcode kbprg
Version : 97
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: April 22, 1999