Public variable problem

  • Thread starter Thread starter moonhk
  • Start date Start date
M

moonhk

I have nine module 1 to module 9. Each module handle one location of
Voucher.
How to define following two public variable ? Need to define in each
module ?

Public gerr_Cnt As Long
Public gShErr As Worksheet


Module 1
Option Explicit
'~~ 2006/11/24
Public gcJap As New clsConfigJap
Public gerr_Cnt As Long
Public gShErr As Worksheet

Sub Voucher_JAP_Step_1()
.....
end

Module 2
Option Explicit

Public gcSin As New clsConfigSin
Public gerr_Cnt As Long
Public gShErr As Worksheet

Sub Voucher_SIN_Step_1()
.....
end
 
What you are trying will error, with "ambiguous name".
Public variables in modules are global to the project, so you cannot
duplicate names.
If they are different variables, give them different name.
Otherwise just declare in one module as public and they will be available to
all modules.

Depending on your structure, could you expose them through the class for
each location ?

NickHK
As a side-line, depending on the similarity of your different location
classes, would using an Interface/Implements be better ?
Then you may only need a single module, because you can :

'Declare the object as the Interface
Dim MyLocation As ILocation

'Set the object to the required class, as each Implements ILocation
Private Sub CommandButton1_Click()

Select Case UCase(InputBox("First letter of the location."))
Case "J"
Set MyLocation = New CJapan
Case "S"
Set MyLocation = New CSingapore
Case Else
Set MyLocation = Nothing
MsgBox "Invalid"
End Select

If Not MyLocation Is Nothing Then MyLocation.Step1

End Sub
 
The error message is not "ambiguous name". May be Object not set. Now,
my problem seem working ok.

In each module, I have 3 steps
1. Base on raw data file, build Account mapping
2. Build Summary base on raw data file
3. Build Upload text file, upload to ERP system

If run step 1 ,step 2 and then step 3 without error run under same
module(e.g. JAP)
Then ,select other module (eg.Singapore) may have error.

I just want define below two lines as public variable
Public gerr_Cnt As Long
Public gShErr As Worksheet

Also, how to using Interface/Implements in classes module ?
 
Martin Fishlock said:
Dear Moonhk,

You only need to declare one instance of the public variables I usually
have
a seperate module tohold 'global publics'.

You need to ensure that you do not have 'Option Private Module' is in
effect.


That doesn't matter, Option Private just means that Public
variables/procedures are not exposed outside of the project. ANy module
within the project will still see them.
 
Thank, I try using a seperate module to hold global variable. I am sure
that do not have 'Option Private Module'.
 
I neet below error "Method or Data member not found" , when put "Public
cim As New clsCIM" into Global_public module. Other two public variable
gerr_cnt and gShErr are OK.

How to handle class level public variable ?

Option Explicit
'~~ Module : Global_Public
Public gerr_Cnt As Long
Public gShErr As Worksheet
Public cim As New clsCIM
 
Not Yet. still need define "Public cim As New clsCIM" in CIM module.



'~~ Modules : Global_Public
Option Explicit
Public gerr_Cnt As Long
Public gShErr As Worksheet
Public cim As New clsCIM

'~~ Module : CIM
Option Explicit
'~~ 2006/11/23 Add SIN
'~~ 2006/11/22
'~~ 2006/11/17
'~~ 2006/11/14

Public cim As New clsCIM
 
The type of "cim" needs to match the name of the class.
So call the module "clsCIM".

Not sure what you are trying to do here :
'~~ Module : CIM
........
'This line
Public cim As New clsCIM

as you already have the same in the "~~ Modules : Global_Public".

NickHK
 
When remove the Public cim As New clsCIM in CIM module

But Excel return "Method or Data member not found"
 
Not sure what you have, bt do you mean this :

'<Standard module: Global_Public>
Public gerr_Cnt As Long
Public gShErr As Worksheet
Public cim As New clsCIM

'</Standard module: Global_Public>

'<Class module: clsCIM>
'Code for your class
'</Class module: clsCIM>

Or do you have more classes etc ?

NickHK
 
I found the problem. Due to I created Module Name "CIM" and define
"Public CIM As New clsCIM" in modules Global_public.


'~~ Modules : Global_Public
Option Explicit
Public gerr_Cnt As Long
Public gShErr As Worksheet
Public CIM As New clsCIM

In CIM module, I wrote a code CIM.init (which is clsCIM init method) ,
Which is class method. But Excel try to found init in CIM module, as a
result, No such procedure found. When changed CIM module to CIM_OPR. My
program works. Module name should not as a Object Name.

Public Sub Build_CIM(loShName As String, _
loBatch, loVONum As String, loEffDate As Date, lodate As Date)

On Error Resume Next
Dim loBook As Workbook
Dim loSheet As Worksheet
Dim loBookName As String
Dim loSheetName As String
Dim loCIM As Worksheet
Dim cnt, cimCnt, icnt, LineCnt, ActLineCnt As Long
Dim vRange, vObject, v
Dim VouchRoundAmt, VoucherTotal, MarkupAmt, BTAmt, VatAmt, Amt As
Double
Dim HeaderRow As Long
Dim CurrentDNNum, RunningVONum As String
Dim xRange As Range
Dim effDate As Date
Dim runDate As Date
Dim Supplier, SuppCurr As String

CIM.init '<--- This line

.....
 
moonhk said:
I found the problem. Due to I created Module Name "CIM" and define
"Public CIM As New clsCIM" in modules Global_public.

That illustrates the advantage of having and using a standard naming
convention for entities in the project -- it prevents errors like the one
you encountered as well as provides nice documentation of the project,
especially if the project is going to be referenced by other projects.

In medium to large sized projects I do for myself, and in all my commercial
work, I prefix all standard module names with "mod", (e.g.,
"modFileUtilities"), all my class modules begin with "C" (e.g.,
"CTheObject"), I change the worksheet code names to have a prefix of "sht"
or "cht", (e.g., "shtSummary" or "chtSummaryChart"), all forms are prefixed
with "frm" (e.g."frmDataEntry"), and I prefix the Project Name with "proj",
(e.,g, "projMarketScan)".

I also prefix each form control with a character string that indicates the
type of control (e.g., btnOK for a button, lbxItemList for a LisrtBox, and
fraOptions for a Frame, etc.)

The only thing I don't do (and probably should, if I could make it a habit)
is use "Hungarian Notation" in which each variable is prefixed with
characters indicating its data type (e.,g "strName" for a sting, "intCount"
for an integer, etc).

You can design any naming convention you like, but the challenge is to use
it consistently. If you do so, you will find it nicely documents the project
and avoids the sort of problem you encountered.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)
 
Thank A lot.
Currently, All the Class Module using cls prefix , All the Form using
frm prefix.
Just modules, not using prefix.
 

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

Back
Top