-
Feb 1st, 2021, 03:59 PM
#1
Thread Starter
Hyperactive Member
return information from form to calling macro
I currently have a VBA macro that calls a Form with
in userform2 there is a text box (and button) which data can be entered. The user can press either enter, the 'ok' button or the escape key.
upon doing so, the _keyup function is executed (coded in the form) which checks for certain key codes and if the codes match 'KEycode' then the userform is hidden and control returns to the calling macro.
When control is returned to the calling macro, How do I determine what the keycode was in the form ?
-
Feb 1st, 2021, 07:12 PM
#2
Re: return information from form to calling macro
You could assign the keycode to a variable and then pass that value back to wherever it needs to go.
-
Feb 1st, 2021, 07:45 PM
#3
Thread Starter
Hyperactive Member
Re: return information from form to calling macro
Thanks, That's what I thought, but how do I pass a variable back to the calling macro, when it has been triggered by a keyup event on the form ?
-
Feb 2nd, 2021, 02:19 AM
#4
Re: return information from form to calling macro
You don't. You can only pass a value back to the calling macro, the moment control is back to the calling macro.
As jdc said: Declare a Public Variable in your Form-code, assign the keycode (or whatever value you want to return) to that Variable in your keyUp-Event, hide your Form, and then your calling macro can read that variable as any other property of the form
Code:
UserForm.Show
'...something happens in the Form, where MyKeycode gets assigned a value in KeyUp
Debug.Print UserForm.MyKeycode
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Feb 2nd, 2021, 03:38 AM
#5
Re: return information from form to calling macro
As jdc said: Declare a Public Variable in your Form-code, assign the keycode
i believe it would be better to use a public variable in a standard module, a variable in the form module would go out of scope when the form is closed, so would not be recoverable to the caller when the form is closed
a variable could be in the same module as the calling procedure, but dimensioned at the top in the general section, or in a separate empty module, or any other module in the project, also at the top in the general section
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 2nd, 2021, 04:17 AM
#6
Re: return information from form to calling macro
Pete, correct, that's one of the "obvious" ways,
but that would be the child updating the parent, which is considered "bad practice"
And i did say "hiding" the form, not closing/unloading.
IMO, the correct way would be assigning the "returned" value to a local variable, then close/unload the form, and then go from there.
Aircode
Code:
Dim MyValue As SomeType 'Long, String, whatever
UserForm.Show
'....DoSomething in the Form, assign Value to Public Variable in UserForm
'...Hide userForm
MyValue=UserForm.MyVariable
UserForm.Close
Debug.Print MyValue
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|