I do not understand how to get modules to recognize variables that have been declared Public in the frmMain section. When I try to use a variable in a module, and the variable has already been declared as Public in the frmMain section, VB shows an error that says the variable name has not been declared.

From everything I've read so far, what I'm doing should work, but it doesn't. So, I'm not sure what I'm doing wrong. I'm using VB5 Express. Any help would be greatly appreciated.

Thanks, Audrey

Recommended Answers

All 4 Replies

Code would help, but I sounds like you are declaring a variable in one Sub or Function and attempting to use it in another....

You can try one of these options:

1.) Declare the variable in the prologue section (outside of all Sub's and Function's)

'Top of VB File
Option Explicit
Public <variable Name> as <variable type>

2.) Pass the variable to the Sub's or Function's:

Private Sub frmMain_Load(...)
   Dim <variable name1> as <variable type>
   .......
   TestForU(<variable name1>)
End Sub

Sub TestForU( Byval <variable name2> as <varable type>)
    ...
    Debug.Print <variable name2>
    ...
End Sub

Thanks for your quick reply. Neither of those options fixed the problem. I'll just keep working at it.

Hi,

Please use the variable as formname.variablename instead of using the variable name directly..

For Example
Form Code
Public Var1 as integer
Call Func1()
Module Code
Sub Func1()
msgbox form1.var1
END SUB

maheshsayani has it...

For variables delclared publically within a form the variable must be qualified by the form name it was declared in...

Form1.MyForm1Var = SomeValue
SomeVariable = Form2.MyForm2Variable

Good Luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.