VBA passing information between differnt User Forms

C

chfa

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
 
B

Bob Phillips

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)
 
K

Keith74

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
 
J

Jon Peltier

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
 
C

chfa

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
 
C

chfa


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
 

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

Top