Set object variable in class module

A

alex

Set object variable in class module

Hello,

Is it possible to define an object—once—in a class module?

E.g.,
Option Compare Database
Option Explicit

Dim strTable As String
strTable = "tblMyTable" ‘throws error, Invalid outside procedure

I have a table that I’d like to reference throughout code in a class
module… Is there a place within the module where I can define it only
once, instead of in every Sub?

Thanks,
alex
 
D

Dirk Goldgar

From the sound of it, you could just use a private constant:

Private Const conTable As String = "tblMyTable"

.... defined at the module level.

You can't do that if you're going to be modifying the value of the variable,
though. In that case, you can define a module level variable, and then use
the class module's Initialize event to set the initial value of the
variable.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)


Set object variable in class module

Hello,

Is it possible to define an object—once—in a class module?

E.g.,
Option Compare Database
Option Explicit

Dim strTable As String
strTable = "tblMyTable" ‘throws error, Invalid outside procedure

I have a table that I’d like to reference throughout code in a class
module… Is there a place within the module where I can define it only
once, instead of in every Sub?

Thanks,
alex
 
A

alex

From the sound of it, you could just use a private constant:

    Private Const conTable As String = "tblMyTable"

... defined at the module level.

You can't do that if you're going to be modifying the value of the variable,
though.  In that case, you can define a module level variable, and thenuse
the class module's Initialize event to set the initial value of the
variable.

--
Dirk Goldgar, MS Access MVP
Access tips:www.datagnostics.com/tips.html

(please reply to the newsgroup)


Set object variable in class module

Hello,

Is it possible to define an object—once—in a class module?

E.g.,
Option Compare Database
Option Explicit

Dim strTable As String
    strTable = "tblMyTable" ‘throws error, Invalid outside procedure

I have a table that I’d like to reference throughout code in a class
module…  Is there a place within the module where I can define it only
once, instead of in every Sub?

Thanks,
alex

Thanks Dirk; I never thought of that!
Can I do the same for:
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim tbl As TableDef
Dim fld As Field
....as constants as well?
 
D

Dirk Goldgar

alex said:
Can I do the same for:
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim tbl As TableDef
Dim fld As Field
...as constants as well?

No, object variables can't be constants, as they are really just pointers to
objects that have to be dynamically created. For object variables, you'd
need to use the class's Initialize event to create the objects, and the
class's Terminate event to destroy them. In the case of objects that should
be explicitly closed when you're through with them, such as Recordset
objects (and Database objects that you explicitly open), you should close
them in the Terminate event, before destroying them.
 
A

alex

No, object variables can't be constants, as they are really just pointersto
objects that have to be dynamically created.  For object variables, you'd
need to use the class's Initialize event to create the objects, and the
class's Terminate event to destroy them.  In the case of objects that should
be explicitly closed when you're through with them, such as Recordset
objects (and Database objects that you explicitly open), you should close
them in the Terminate event, before destroying them.

--
Dirk Goldgar, MS Access MVP
Access tips:www.datagnostics.com/tips.html

(please reply to the newsgroup)

Thanks Dirk...
You're talking about (for example):
'close
rst.close
dbs.close
'terminate
set rst = nothing
set dbs = nothing
'in a class sub
 
D

Dirk Goldgar

alex said:
You're talking about (for example):
'close
rst.close
dbs.close
'terminate
set rst = nothing
set dbs = nothing
'in a class sub

Right, in the Class_Terminate() event procedure. Note, though, that if your
dbs object was set like this:

Set dbs = CurrentDb

.... or like this:

Set dbs = DBEngine.Workspaces(0)(0)

.... then you shouldn't close it because you didn't open it.

You should only issue dbs.Close if you actually opened it using something
like:

Set dbs = DBEngine.OpenDatabase("Your\Path\To\YourDb.mdb")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top