ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   passing values from form to module (https://www.excelbanter.com/excel-programming/444135-passing-values-form-module.html)

c1802362[_2_]

passing values from form to module
 
Hello, all.

I have a programming issue I can’t seem to get a handle on.

I have an application that launches a form (“LaunchForm”) that gives
the user a choice of picking either Radio Button A or B. Picking
either radio button assigns a string variable (“textvariable”) with an
appropriate value (StringA or StringB).

When I try to pass textvariable from the form to the application, it
disappears. Any suggestions? Here’s the basic code:


Option Explicit

Sub RunThisApp()

LaunchForm.Show

Range(“A1”).Value = textvariable

.........more code.....

Unload LaunchForm

End Sub

Here’s the code for LaunchForm

Option Explicit
Public textvariable As String ' I added the public statement to
the form since this is the first appearance of textvariable

Private Sub YesButton_Click()

If ButtonA.Value = True Then
textvariable = "StringA"
End If

If ButtonB.Value = True Then
textvariable = "StringB”
End If

LaunchForm.hide

End Sub

Private Sub CancelButton_Click()
Unload LaunchForm
Exit Sub
End Sub


Art

Jim Cone[_2_]

passing values from form to module
 

Those "public" variables in class modules are really private.
The module behind a user form is a class module.
All or most controls on a user form and the user form have a "Tag" property (its
a string).
You can assign a value to a tag property and then access it anytime before the
form is unloaded...

textVariable = LaunchForm.Controls("Sludge").Tag
'---
Jim Cone
Portland, Oregon USA
http://www.mediafire.com/PrimitiveSoftware
(create Lottery Numbers - in the free folder)




"c1802362"
wrote in message
...
Hello, all.

I have a programming issue I can’t seem to get a handle on.

I have an application that launches a form (“LaunchForm”) that gives
the user a choice of picking either Radio Button A or B. Picking
either radio button assigns a string variable (“textvariable”) with an
appropriate value (StringA or StringB).

When I try to pass textvariable from the form to the application, it
disappears. Any suggestions? Here’s the basic code:


Option Explicit

Sub RunThisApp()

LaunchForm.Show

Range(“A1”).Value = textvariable

.........more code.....

Unload LaunchForm

End Sub

Here’s the code for LaunchForm

Option Explicit
Public textvariable As String ' I added the public statement to
the form since this is the first appearance of textvariable

Private Sub YesButton_Click()

If ButtonA.Value = True Then
textvariable = "StringA"
End If

If ButtonB.Value = True Then
textvariable = "StringB”
End If

LaunchForm.hide

End Sub

Private Sub CancelButton_Click()
Unload LaunchForm
Exit Sub
End Sub


Art



c1802362[_2_]

passing values from form to module
 
On Jan 17, 7:34*pm, "Jim Cone" wrote:
Those "public" variables in class modules are really private.
The module behind a user form is a class module.
All or most controls on a user form and the user form have a "Tag" property (its
a string).
You can assign a value to a tag property and then access it anytime before the
form is unloaded...

textVariable = LaunchForm.Controls("Sludge").Tag
'---
Jim Cone
Portland, Oregon USAhttp://www.mediafire.com/PrimitiveSoftware
(create Lottery Numbers - in the free folder)

"c1802362"
wrote in ...
Hello, all.


Jim,

made one small modification and it works perfectly.

I changed "LaunchForm.Controls("Sludge").Tag" to
"LaunchForm.Controls.Tag" when I couldn't get any variation of
"Sludge", such as "ButtonA" to work

Art

Jim Cone[_2_]

passing values from form to module
 
I think you must have left something out of your explanation.

The following two syntax forms work...
LaunchForm.CheckBox1.Tag
LaunchForm.Controls("CheckBox1").Tag

But this fails...
LaunchForm.Controls.Tag
'--
Jim Cone
Portland, Oregon USA
http://www.mediafire.com/PrimitiveSoftware
(Formats & Styles lists or removes unused styles or number formats - in the free
folder)





"c1802362"
wrote in message
...

Jim,
made one small modification and it works perfectly.

I changed "LaunchForm.Controls("Sludge").Tag" to
"LaunchForm.Controls.Tag" when I couldn't get any variation of
"Sludge", such as "ButtonA" to work

Art



c1802362[_2_]

passing values from form to module
 
On Jan 17, 10:32*pm, "Jim Cone" wrote:
I think you must have left something out of your explanation.

The following two syntax forms work...
* *LaunchForm.CheckBox1.Tag
* *LaunchForm.Controls("CheckBox1").Tag

But *this fails...
* *LaunchForm.Controls.Tag
'--
Jim Cone
Portland, Oregon USAhttp://www.mediafire.com/PrimitiveSoftware
(Formats & Styles lists or removes unused styles or number formats - in the free
folder)


Jim,

can't explain it, but if I use the
"LaunchForm.Controls("CheckBox1").Tag" syntax it fails to pass the
value from the form to the module, but if I use
"LaunchForm.Controls.Tag" it works....

I'm using VBA from Excel/Powerpoint 2003, but that shouldn't matter

Art

GS[_2_]

passing values from form to module
 
I'm thinking that if you're trying to read from a variable in a
userform class, you need to ref the class as you would any other class.

As Jim states, declaring a variable 'Public' in a userform keeps it
private to the userform because it dies when the userform is unloaded.
This is the same as what happens when a class terminates. Making the
declaration 'Public', though, does make the variable behave like a
property of the class and so should be handled as such.

You do it right by trying to access it before unloading the userform,
but because VB[A] doesn't know of it (unqualified ref) then you're SOL.
Try...

Range("A1").Value = LaunchForm.textvariable '//fully qualified ref

Alternatively, you could make the vars global by declaring them in a
standard module. This allows access to them from anywhere in your
project.

HTH

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc




All times are GMT +1. The time now is 06:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com