Defining a Globally available Variable

Defining a Globally available Variable

Anonymous
Not applicable
407 Views
5 Replies
Message 1 of 6

Defining a Globally available Variable

Anonymous
Not applicable
Hi,

I do not understand what is so different between VBA and VB 6, but I was
able to, in VB 6 to define a global variable by:

Public 'Variable' As 'Type'

This variable was defined like this in a Module (to keep things cleaner), if
I do the same thing with VBA, the variable does not transfer between the
dialogs.

I define this variable in the class as above (it is an integer), in one
dialog, I set its value from a text box in one dialog (call this one dlg2)
(this dialog is started via a button in another dialog - ill call it dlg1).
After filling out the text box and pressing the ok button on dlg2 (okay sets
the variable) a message box pops up displaying the value in the variable,
the dlg2 closes. Back in dlg1, another message box appears, the value for
the variable is now 0.

Just wondering if somebody out there could tell me what I am doing wrong in
defining a variable that has the ability to pass between dialogs?

Thank you
0 Likes
408 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
it's the programming Gods telling you not to use global variables 🙂
If you must, place it in ThisDrawing. Better to create a Class Module.
"bcbii" wrote in message
news:4914327@discussion.autodesk.com...
Hi,

I do not understand what is so different between VBA and VB 6, but I was
able to, in VB 6 to define a global variable by:

Public 'Variable' As 'Type'

This variable was defined like this in a Module (to keep things cleaner), if
I do the same thing with VBA, the variable does not transfer between the
dialogs.

I define this variable in the class as above (it is an integer), in one
dialog, I set its value from a text box in one dialog (call this one dlg2)
(this dialog is started via a button in another dialog - ill call it dlg1).
After filling out the text box and pressing the ok button on dlg2 (okay sets
the variable) a message box pops up displaying the value in the variable,
the dlg2 closes. Back in dlg1, another message box appears, the value for
the variable is now 0.

Just wondering if somebody out there could tell me what I am doing wrong in
defining a variable that has the ability to pass between dialogs?

Thank you
0 Likes
Message 3 of 6

Anonymous
Not applicable
Hi,

Textboxes hold data as text. Although you can write numbers to a text box,
I have found it useful when querying them from the textbox to use
Variable = Val(Textbox)

You could use CInt or CDbl but these create errors if there are any
non-numeric characters in the textbox.

"I define this variable in the class as above" Do you mean module? Public
variables in VBA are defined the same way as VB

Public 'Variable' As 'Type'


--

Laurie Comerford
CADApps
www.cadapps.com.au
"bcbii" wrote in message
news:4914327@discussion.autodesk.com...
Hi,

I do not understand what is so different between VBA and VB 6, but I was
able to, in VB 6 to define a global variable by:

Public 'Variable' As 'Type'

This variable was defined like this in a Module (to keep things cleaner), if
I do the same thing with VBA, the variable does not transfer between the
dialogs.

I define this variable in the class as above (it is an integer), in one
dialog, I set its value from a text box in one dialog (call this one dlg2)
(this dialog is started via a button in another dialog - ill call it dlg1).
After filling out the text box and pressing the ok button on dlg2 (okay sets
the variable) a message box pops up displaying the value in the variable,
the dlg2 closes. Back in dlg1, another message box appears, the value for
the variable is now 0.

Just wondering if somebody out there could tell me what I am doing wrong in
defining a variable that has the ability to pass between dialogs?

Thank you
0 Likes
Message 4 of 6

Anonymous
Not applicable
Hmm,

Okay so I tried the following and still something isn't transferring over, i
have a text box on dlg1 called txtInc, I make my changes in dlg2 in the text
box called txtIncrement. Now when I push okay, and return to dlg1, I have
programmed the following code:

Private sub cmdIncrement_click()

dlg2.Show vbModal

txtInc.text = dlg2.txtIncrement.Text

End Sub

From what I have done in other vb6 apps, this works, teh value from
txtIncrement will be put into txtInc, but not in this app???


"Paul Richardson" wrote in message
news:4914361@discussion.autodesk.com...
it's the programming Gods telling you not to use global variables 🙂
If you must, place it in ThisDrawing. Better to create a Class Module.
"bcbii" wrote in message
news:4914327@discussion.autodesk.com...
Hi,

I do not understand what is so different between VBA and VB 6, but I was
able to, in VB 6 to define a global variable by:

Public 'Variable' As 'Type'

This variable was defined like this in a Module (to keep things cleaner), if
I do the same thing with VBA, the variable does not transfer between the
dialogs.

I define this variable in the class as above (it is an integer), in one
dialog, I set its value from a text box in one dialog (call this one dlg2)
(this dialog is started via a button in another dialog - ill call it dlg1).
After filling out the text box and pressing the ok button on dlg2 (okay sets
the variable) a message box pops up displaying the value in the variable,
the dlg2 closes. Back in dlg1, another message box appears, the value for
the variable is now 0.

Just wondering if somebody out there could tell me what I am doing wrong in
defining a variable that has the ability to pass between dialogs?

Thank you
0 Likes
Message 5 of 6

Anonymous
Not applicable
I would set the form to Modal at design time if
needed, which is the default Modal is Readonly
at runtime inVBA I believe.

Create two forms each with a textbox1.
Add following code to each see if it helps.


'userform1
Private Sub UserForm_Click()
UserForm2.TextBox1.Text = _
Me.TextBox1.Text
Me.Hide
UserForm2.Show
End Sub

'userform2
Private Sub UserForm_Click()
Me.Hide
UserForm1.Show
End Sub


"bcbii" wrote in message
news:4914353@discussion.autodesk.com...
Hmm,

Okay so I tried the following and still something isn't transferring over, i
have a text box on dlg1 called txtInc, I make my changes in dlg2 in the text
box called txtIncrement. Now when I push okay, and return to dlg1, I have
programmed the following code:

Private sub cmdIncrement_click()

dlg2.Show vbModal

txtInc.text = dlg2.txtIncrement.Text

End Sub

From what I have done in other vb6 apps, this works, teh value from
txtIncrement will be put into txtInc, but not in this app???


"Paul Richardson" wrote in message
news:4914361@discussion.autodesk.com...
it's the programming Gods telling you not to use global variables 🙂
If you must, place it in ThisDrawing. Better to create a Class Module.
"bcbii" wrote in message
news:4914327@discussion.autodesk.com...
Hi,

I do not understand what is so different between VBA and VB 6, but I was
able to, in VB 6 to define a global variable by:

Public 'Variable' As 'Type'

This variable was defined like this in a Module (to keep things cleaner), if
I do the same thing with VBA, the variable does not transfer between the
dialogs.

I define this variable in the class as above (it is an integer), in one
dialog, I set its value from a text box in one dialog (call this one dlg2)
(this dialog is started via a button in another dialog - ill call it dlg1).
After filling out the text box and pressing the ok button on dlg2 (okay sets
the variable) a message box pops up displaying the value in the variable,
the dlg2 closes. Back in dlg1, another message box appears, the value for
the variable is now 0.

Just wondering if somebody out there could tell me what I am doing wrong in
defining a variable that has the ability to pass between dialogs?

Thank you
0 Likes
Message 6 of 6

Anonymous
Not applicable
The difference in this case is not between VBA and VB6. The
difference is in the code you have in the two. You say that
your global variable is declared as public in a module in VB6.
That gives it application scope and lifetime. In VBA you have
it declared as public in a class. That gives it application scope
but class lifetime - when the object goes away, so does the
variable that is a part of it. If dlg2 is a UserForm whose hidden
global "As New" variable is "dlg2" and when you say it gets
closed you mean it gets unloaded then you need to extend
the lifetime of the object until you can retrieve the value.
Try this based on the code in your second post:
[code]
Private Sub cmdIncrement_Click()
'dlg2.Show vbModal
Dim d As dlg2
Set d = New dlg2
d.Show vbModal
'txtInc.Text = dlg2.txtIncrement.Text
txtInc.Text = d.txtIncrement.Text
Set d = Nothing
End Sub
[/code]
0 Likes