user form fundamentals

P

paradox

I have two books and cannot find a simple example of a user form and how to
get the data from a user form into the macro that calls the form. Will
someone please give me an example? My suggested example is a user form with
two check boxes (call them check1 and check2), a run button and a cancel
button. I need the VBA code that will open the form and when the form is
closed, the calling VBA code has the values of the four buttons available in
the code for use. It does not have to do anything, just be able to see the
values in the locals window.



Thanks for your time
 
P

Peter Faulhaber

Hi,

to open a form use:
<nameform>.show

to hide a form:
<nameform>.hide

to unload a form:
unload <nameform>

Difference hide will remember the values of the fields, and unload won't.

declare two variables for eaxh checkbox
strVar1 = check1.value
strVar2=check2.value

Public strVar1 as string
Public strVar2 as string

Sub startform()
<nameform>.show
end sub

In de form you will have a close button:
private sub <namebutton>_Click()
strVar1 = check1.value
strVar2=check2.value
unload <nameform>
end sub
 
P

paradox

Hello Peter,

I do not like using public and common area variables. They are to be avoided
at almost all costs.

I believe there is a way of doing this with good software techniques, just
have not yet found it. If you or anyone knows how, please post.

Thanks for your thoughts.


<snip>
 
T

Tony Jollans

One should always declare variables with the narrowest scope required, that
is true, but I think you are probably overstaing the case a little.

In the case of a Userform it has public Properties, and all the controls you
add are public - whether you like it or not. You can access these from code
outside the userform as long as the userform exists.

At its simplest the code can be ....

Set myForm = New UserForm1
myForm.Show
' Do whatever you want with the contents of the Userform here
Unload myForm

And in the Userform code module, along with whatever else you are doing ...

Private Sub RunButton_Click()
Me.Hide
End Sub

Private Sub CancelButton_Click()
Me.Hide
End Sub
 
P

paradox

Tony Jollans said:
One should always declare variables with the narrowest scope required,
that
is true, but I think you are probably overstaing the case a little.

You are right about that and I do overstate the case a bit. Programmers
writing for themselves and one or two others can safely use publics/commons
more than others. When you work in a medium or large group, the
communication effort goes up exponentially. The people that use and modify
your code will probably never speak with you have have nothing but your
documentation to go on. As you know, few people read the documentation at
all, much less carefully. Not using any public or common variables is far
safer than using them. When comparing the programming effort to eliminate
publics and commons as compared to the effort to troubleshoot a defect
(bug), and the lost reputation when the customer discoveres the effect of a
defect, you are far ahead not using publics. If you follow this process in
your personal life for personal projects, it is far eaiser to do it in your
professional life.

Back to my problem, here are a few lines from my code.

CBS_AZ_EL_AUTO_AGC_Button = Chart_Build_Selector.AZ_EL_AUTO_AGC_Button

CBS_All_AGCs_Button = Chart_Build_Selector.All_AGCs_Button

CBS_AZ_EL_CMD_and_Modes_Button =
Chart_Build_Selector.AZ_EL_CMD_and_Modes_Button

CBS_Cancel_Button = Chart_Build_Selector.Cancel_Button

CBS_Build_Button = Chart_Build_Selector.Build_Button



In this section I do nothing but get the values of the buttons from the user
form so I can see them all with little effort. This is not the final code,
just a development tool. The first three lines get the values from the
check boxes. All three check boxes are independant and I get the true/false
values as they are checked in the user form. The second two lines are for
the control buttons. They tell me if the user want to build some charts or
exit and do nothing. These values are always false. All of the functions
for the check boxes and control buttons are emptly functions with nothing
but a call and and exit. What do I need to do to get the values of the
control buttons?



Thanks again for your time.
 
T

Tony Jollans

Hi paradox,

The Value of a button on a Userform is always False. If you want to know
what button has been pressed you must save the information somewhere within
the button's click event.
 

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