ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA passing information between differnt User Forms (https://www.excelbanter.com/excel-programming/386062-vba-passing-information-between-differnt-user-forms.html)

chfa

VBA passing information between differnt User Forms
 
Hi u,

the task is as following :

When User Form1 (UF1) is called, it checks for a value in a Sheet. If
the value isn't set UF1 croaks and sets a flag to be passed to a
UserForm (UF2) where the value is to be set.

Searching 4 the solution I found Public Properties the most suitable
way solving my task. But i didn't got the string yet. In UF2 the
flagRogue keeps empty.

Due to what I found I tried following:

in: UF1
'---------------
private flagRogue as string
publicproperty get flagStatus() as String
flagStatus = flagCroak
end property
publicproperty let flagStatus(byval flagVal as String)
flagStatus = flagRogue
end property
'--------------
' Initialize:
dim wannaSthg as new UF2
...
if <no Value Then
<croak
flagRogue = 1
wannaSthg.flagRogue = Me.flagRogue
wannaSthg.Show
end if


in UF2
'-------------
private flagRogue as string
publicproperty get flagStatus() as String
flagStatus = flagCroak
end property
publicproperty let flagStatus(byval flagVal as String)
flagStatus = flagRogue
end property
'--------------
' Initialize:
dim wannaSthg as new UF2
...
if flagRogue Then
<do something
else
<do something else
end if

Question:
Where I'm wrong ???

thanx 4 helping
VBR


Bob Phillips

VBA passing information between differnt User Forms
 
It would be far simpler to create the flag variable as a Public variable in
a general code module, you will then be able to load and read it from either
form.

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"chfa" wrote in message
ups.com...
Hi u,

the task is as following :

When User Form1 (UF1) is called, it checks for a value in a Sheet. If
the value isn't set UF1 croaks and sets a flag to be passed to a
UserForm (UF2) where the value is to be set.

Searching 4 the solution I found Public Properties the most suitable
way solving my task. But i didn't got the string yet. In UF2 the
flagRogue keeps empty.

Due to what I found I tried following:

in: UF1
'---------------
private flagRogue as string
publicproperty get flagStatus() as String
flagStatus = flagCroak
end property
publicproperty let flagStatus(byval flagVal as String)
flagStatus = flagRogue
end property
'--------------
' Initialize:
dim wannaSthg as new UF2
..
if <no Value Then
<croak
flagRogue = 1
wannaSthg.flagRogue = Me.flagRogue
wannaSthg.Show
end if


in UF2
'-------------
private flagRogue as string
publicproperty get flagStatus() as String
flagStatus = flagCroak
end property
publicproperty let flagStatus(byval flagVal as String)
flagStatus = flagRogue
end property
'--------------
' Initialize:
dim wannaSthg as new UF2
..
if flagRogue Then
<do something
else
<do something else
end if

Question:
Where I'm wrong ???

thanx 4 helping
VBR




Keith74

VBA passing information between differnt User Forms
 
Not sure i understand exactly what you're doing but it looks like your
variable isn't being passed between the forms try in UF2 if you
haven't unloaded UF1

UF2_Initialize()

UF2.flagRogue.value = UF1.flagRogue.value

end sub

Personally i prefer using global variable as you're not dependant on
userform life.

HTH


Jon Peltier

VBA passing information between differnt User Forms
 
I'd check for the value in the code that calls UF1. If no value, skip UF1
and call UF2.

Despite what my colleagues say, I find it safer to use properties than
globals. However, I don't think you're using the property correctly. This is
not clear and probably wrong:

'---------------
private flagRogue as string
publicproperty get flagStatus() as String
flagStatus = flagCroak
end property
publicproperty let flagStatus(byval flagVal as String)
flagStatus = flagRogue
end property
'--------------

I suppose you want to do this:

'---------------
' module level variable
private m_flagRogue as string

public property get flagStatus() as String
' return value stored in module level variable
flagStatus = m_flagRogue
end property

public property let flagStatus(byval flagVal as String)
' assign input value to module level variable
m_flagRogue = flagVal
end property
'--------------

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"chfa" wrote in message
ups.com...
Hi u,

the task is as following :

When User Form1 (UF1) is called, it checks for a value in a Sheet. If
the value isn't set UF1 croaks and sets a flag to be passed to a
UserForm (UF2) where the value is to be set.

Searching 4 the solution I found Public Properties the most suitable
way solving my task. But i didn't got the string yet. In UF2 the
flagRogue keeps empty.

Due to what I found I tried following:

in: UF1
'---------------
private flagRogue as string
publicproperty get flagStatus() as String
flagStatus = flagCroak
end property
publicproperty let flagStatus(byval flagVal as String)
flagStatus = flagRogue
end property
'--------------
' Initialize:
dim wannaSthg as new UF2
..
if <no Value Then
<croak
flagRogue = 1
wannaSthg.flagRogue = Me.flagRogue
wannaSthg.Show
end if


in UF2
'-------------
private flagRogue as string
publicproperty get flagStatus() as String
flagStatus = flagCroak
end property
publicproperty let flagStatus(byval flagVal as String)
flagStatus = flagRogue
end property
'--------------
' Initialize:
dim wannaSthg as new UF2
..
if flagRogue Then
<do something
else
<do something else
end if

Question:
Where I'm wrong ???

thanx 4 helping
VBR




chfa

VBA passing information between differnt User Forms
 

Hi John,

thank u 4 your answer. Of course theres no point
in using global variables. That's a kind of dirty hacking ;-).

Your hint signing the variables with "m_" seems a
valuable practice worth adopting.

But last not least. The knack of the problem is covered
in Keith75's comment. Using UF<Nbr_Intialize
runs into a deadlock, as each module tries to initialize
the variables .... Using UF<nbr_Activate works.

And by the way. Why String? Ok. That's a historical
try. I changed to boolean, the most suitable Type for
this kind passing states.

Thanx a lot, happy easter, so long, vbr

chfa





Bob Phillips

VBA passing information between differnt User Forms
 


"chfa" wrote in message
oups.com...


thank u 4 your answer. Of course theres no point
in using global variables. That's a kind of dirty hacking ;-).



Why?



chfa

VBA passing information between differnt User Forms
 

.... Of course theres no point
in using global variables. That's a kind of dirty hacking ;-).


Why?


at first of all VBA is object orientated.
at second, far more important, it's far easier for maintenance
and understanding the structure of the code if global variables
are not used. So there are only the interfaces as the gates
for the datas beeing handed over.

and atleast: backdoors, as i regard globals, are something
suspicious ;-))

(:
ChFa



All times are GMT +1. The time now is 02:01 PM.

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