Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How get value in control (not as simple as it sounds)? 1

Status
Not open for further replies.

gazolba

Programmer
Dec 4, 2001
167
US
I'm in a tab of a tabbed control in a subform. There is a command button that opens a new form (with docmd.openform)and certain logic builds a string and populates a global variable.
I need to get this global variable in a textbox (locked) in the tab above the command button. I do not want to load this from the form I open but after I return (this is very important!).
How to do it? Logic that I put after the openform does not work, I also tried the form activate event (does not trigger). I cant use focus on the textbox as it is locked.
 
enable the the textbox, then set focus, then disable all in one go from vba?
 
If you have a control that is locked, you need to unlock it before you can add data to it. That could be done in vba code behind the form. You can create a property in VBA behind the form that is opened and in your main form, instanciate the other form. Then in your main form, you can the read the property from the other form and populate your text box.

I have given you a high level explanation of what you can do because I don't know what your skills are. Ask if you need more info.
 
Thanks for the suggestions.
I dont want anything in the code for the form that builds the global variable because it is a general purpose routine.
The code to load the textbox must be outside the form that builds the global variable.
What event can I use where I can populate the textbox?
I have tried making the textbox both enabled and unlocked but I stll can't find a way to get the global variable in there.
 
gazolba . . . .

In the [blue]Unload Event[/blue] of the called form, add the following:
Code:
[blue]   Dim frm As Form, ctl As Control
   
   Set frm = Forms![purple]CallingFormName[/purple]
   Set ctl = frm![purple]CallingFormTextBoxName[/purple]
   
   ctl.Locked = False
   ctl = [purple]GlobalVariableName[/purple]
   ctl.Locked = True
   
   Set ctl = Nothing
   Set frm = Nothing[/blue]
I'm not sure if this is what you want, but when you close the called form, the textbox is updated.


cal.gif
See Ya! . . . . . .
 
Thanks for the suggestion but I already said I can't put anything in the called form. It MUST be in the form with the command button.
 
gazolba . . . .

Yes your right! You did say that . . . (my mistake)

Then could you be a little more specific about the following:
gazolba said:
[blue] . . . . certain logic builds a string and populates a global variable.[/blue]
Hmmmmmmmmmmmmmmm . . . if you build this string with the code on the calling form (which it appears you do), then opening the second form is academic, unless the second form is changing the variable. If your building the variable in the called form, then your logic of picking it up after the called form is closed, escapes me.

I could be wrong, but it seems like there's something your not telling us.

In any case, I'm not sure if you can do what you want so directly. You somehow have to monitor the called form for when it closes, and at present, I can only think of setting a timer in the calling form. I'll research this and see what else I can come up with . . .


cal.gif
See Ya! . . . . . .
 
Sorry for the confusion, I thought I had explained it clearly in my original post but.....

There is a text box and a command button on a form. The textbox is empty. Pressing the button opens up a new form which allows a string to be built. Upon exit from this form the string is stored in a global variable. How to get this global variable in the textbox *without adding any code to the form which builds the stringm*?

The best I can think of so far, is to have the control source be an expression that returns the global variable value.
 
gazolba - I think you may be missing the point. The issue is not how to get the value of the global variable into the textbox - that's easy. The question is when to put the value in the textbox. Without the ability to make changes to the second form, there is no way for the first form, via an event, to know that the second form has closed.

One approach is the one indicated by TheAceMan1 to use a timer to periodically check the Application's Forms collection, and once you detect that the form no longer exists, populate the textbox with a simple assignment. But that is not very efficient.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hay CajunCenturion . . . . Welcome aboard . . .

I'm still researching this for [blue]gazolba [/blue], but for the life of me, I can't see the big deal in updating the textbox only after the called form is close versus updating on close?. Can you?

Some security reasons pass thru my mind, but this doesn't appear to be the case here.

And yes he could do it with a timer, but I would consider that a clumsy way of doing it (not good programming). So far my research has not turned up anything worthy . . .


cal.gif
See Ya! . . . . . .
 
The reasons that the second form may not be modified are what they are and if that is not an option then so be it. Although it may be reasonable for us to ask why that is the policy, it may not reasonable to expect a change in that policy.

Another approach is to get the Window Handle of the second form after it's opened, then subclass that second window from within the first form, monitor and pass through all windows messages back to the second form, and when the close message is detected, populate the textbox. That's not an approach I would suggest or recommend unless you really know what you're doing. The timer approach is much easier (and safer).

gazolba - You said you tried the Activate event and it did not fire. How did you test that? If form1 opens form2 and form2 does something and then closes, and form1 returns to being the active window, then the Activate event for form1 does fire. That's not the case in a subform, and a third form will also thrown a monkey wrench which is why I didn't bring it up earlier.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Can form2 be modal?

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
If modal is acceptable:

[tt]DoCmd.OpenForm "form2",,,,,acDialog
me!txtSomeText.Value = gYourPublic[/tt]

Open the form in dialog mode, then the code in the calling form halts until the "form2" form is closed, and you can retrieve the public.

The locked property of controls specifies whether the the control is editable in Form View. I don't think there's a need to toggle this property prior to assigning a value programaticly.

Btw - nice to see you back in these fora, CajunCenturion!

Roy-Vidar
 
Way ta go [blue]Roy![/blue]

I've never had a need to use [purple]acDialog[/purple]. I remember it in classes along time ago. Gonna have to flag it now.


cal.gif
See Ya! . . . . . .
 
Roy Vidar's ingenious idea seems like it should work I will try it.

The reason the Activate event did not tigger is because the original form is a subform, at least that's my guess. I have not have the time or inclination to investigate the vagaries of when and why events trigger.

Thanks for everyones help on this matter! I did not expect it to generate such enthusiastic strivings.
 
Thanks Roy! It worked like a charm. Who'd have thought it would be so simple.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top