How to make variables public

  • Thread starter Thread starter ericb
  • Start date Start date
E

ericb

On the main form are several control buttons that open different forms that
then open other forms and reports.
All this works fine.

But I would like to declare an array of string that would be available to
all forms and report alike. The reason is if I change the name of a product I
don't have to go through all of the code to change strings all over the place
and for sure I will forget one.

To do that I added a class module and put this in it :
Option Compare Database

Const nb_prod As Long = 6 ' Nb de produit disponible

Dim lst_produit(6) As String ' Les produits

lst_produit(0) = "OP"
lst_produit(1) = "BM"
lst_produit(2) = "CP"
lst_produit(3) = "CM"
lst_produit(4) = "SP"
lst_produit(5) = "OPL"

' Les index
Const prod_OP As Long = 0
Const prod_BM As Long = 1
Const prod_CP As Long = 2
Const prod_COM As Long = 3
Const prod_SP As Long = 4
Const prod_OPL As Long = 5

and nothing works.

1- I am not initializing lst_produit properly
2- From the main form I am not able to see (or use) lst_produit

Where do I put and how do I make such an array ans const

Thank you for the HELP

Eric
 
Eric -

I have used global functions to make the constants available to code,
queries, and reports alike. These go in your class module. For example:

Public Function GetProd_OP() As Long
GetProd_OP = prod_OP
End Function

You can now use this GetProd_OP function in your queries or other code. I
have not done this with arrays, but I imagine the same would work, something
like this:

Public Function GetProduit_OP(IntIndex as Integer) As Long
GetProduit_OP = lst_produit(IntIndex)
End Function

I hope this helps!
 
I would agree with Ken here.

For cases like this, I usually do build what I like to think of
"interface" to the data, though that isn't technically accurate
description. What it means is that I'd store the data in a table, then
write a public function or property procedure that returns the value
based on the argument given. So all over in the application, I just call
this function, thus hiding the logic needed to extract the data from the
table and making it a one-liner affair.

One advantage of this approach is that any changes made to the function
will be a breaking change and thus the compiler can pull you to all
places where the old calls would be invalidated so you can updated and
thus rest in assurance that you've updated your code for the new
requirement. Sure, you can just use a Find & Replace but why leave it to
chance?
 
Ken,

That would be the way of doing it. Why do I work at complicating my life ?

But how can I make the Const values available to all the forms and reports.

A Const value would be an index in the table and would to point to the right
string.

Where and how do I declare those Const values ?

Thank you
 
The problem is that Const is a compile-time operation. You're basically
hard-coding the value at the time of compiling.

The table of values will need to be looked up and you can't look it up
at the compile time- only at the run time. Thus, you can't use Const for
this kind of thing.

Const is great for something that's really immutable. Perhaps for example:

Const Pi As Double = 3.14159...

I sincerely doubt that we'll find that we've been doing it all wrong and
Pi should be some other value so it's a good candidate for Const.

As I suggested, write a Public Function property that does the lookup
with desired parameter and you'll get what you want.

HTH.
 
I understand how to use the table.

My problem is still, How and where to declare the public values to be used
as the passing parameter to the function.

In the main form, on the top of the code is :
Option Compare Database

Public Const prod_OP As Long = 0
Public Const prod_BM As Long = 1


I always get the following msg:

Constants, fixed length strings, ... statements not allowed as Public
members of an object module.

I think if I put them at that place only the functions and procedures of
that module will see them. So where to place them so all the modules will see
them ?

Thank you
--
eric


KenSheridan via AccessMonster.com said:
Eric:

The values 0 to 5 would be in the prod_ID column of the table and each would
thus correspond with a text value in the produit column. You can declare
constants as Public in the 'declarations' area of any standard module:

' Les index
Public Const prod_OP As Long = 0
Public Const prod_BM As Long = 1
Public Const prod_CP As Long = 2
Public Const prod_COM As Long = 3
Public Const prod_SP As Long = 4
Public Const prod_OPL As Long = 5

You can then find the corresponding produit value anywhere in the database, e.
g. by calling the DLookup function:

DLookup("produit", "Produits", "prod_ID = " & prod_CP)

would return whatever value is in the produit column of the table where the
prod_ID value is 2.

Or you could include a function in the same standard module:

Function GetProduit (lngIndex As Long) As String

GetProduit = DLookup("produit", "Produits", "prod_ID = " & lngIndex)

End Function

and call it anywhere, passing a constant into it as its argument to get the
produit value:

GetProduit (prod_CP)

One thing to remember, however, is that you cannot reference a constant in a
query; you'd have to use its numeric value if calling the function there.
But in query you'd probably be more likely to join the Produits table to
another table on prod_ID, which would be a far more efficient way of
returning the produit value.

Ken Sheridan
Stafford, England
Ken,

That would be the way of doing it. Why do I work at complicating my life ?

But how can I make the Const values available to all the forms and reports.

A Const value would be an index in the table and would to point to the right
string.

Where and how do I declare those Const values ?

Thank you
[quoted text clipped - 60 lines]
 
ericb said:
I understand how to use the table.

My problem is still, How and where to declare the public values to be used
as the passing parameter to the function.

In the main form, on the top of the code is :
Option Compare Database

Public Const prod_OP As Long = 0
Public Const prod_BM As Long = 1


I always get the following msg:

Constants, fixed length strings, ... statements not allowed as Public
members of an object module.

I think if I put them at that place only the functions and procedures of
that module will see them. So where to place them so all the modules will
see
them ?

Please reread what Ken said:
A form module is not a standard module, it's a special kind of class module.
A standard module is a non-class module that is shown on the Modules tab of
the database window. If you declare a Public constant or variable in a
*standard* module, all other modules (standard or class modules) will be
able to see it.
 
On the main form are several control buttons that open different
forms that then open other forms and reports.
All this works fine.

But I would like to declare an array of string that would be
available to all forms and report alike. The reason is if I change
the name of a product I don't have to go through all of the code
to change strings all over the place and for sure I will forget
one.

The data belongs in a table, but you could easily build a class
module around it that is your lookup structure for it, e.g., load
the data into an array in the class module's Initialize event and
then use a property Get to get the data out of the array. For the
data you list in your example (an index number and a value), you
could use a custom collection, and just make it a public member of
the class module.

Any time you start trying to put things in public constants, it
raises a read flag that you're probably doing something wrong.
 
Back
Top