Userform to save inputs, clear commandbutton to clear fields

jyablinsky

New Member
Joined
Dec 29, 2008
Messages
33
Hello,

I have a userform that has about 40 input fields and an execute button that creates a quote/enters data into a data spreadsheet/etc. Once I click the "Generate Quote" commandbutton, the 40 inputs disappear the next time I open the userform. If I make one small mistake, I have to re-enter all 40 fields. Is there an option or code that will save the data in the fields when I reopen the userform? And can I put a "Clear Fields" commandbutton on the userform to clear all the information? Any help would be GREATLY APPRECIATED. Thanks in advance.

Jason
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hello,

I have a userform that has about 40 input...the 40 inputs disappear the next time I open the userform. If I make one small mistake, I have to re-enter all 40 fields. Is there an option or code that will save the data in the fields when I reopen the userform?

And can I put a "Clear Fields" commandbutton on the userform to clear all the information?

Hi Jason,

I imagine you mean text boxes? As to saving their values, how are you dismissing the userform? If you Unload the userform, unless you have saved the vals of the textboxes in variables they will be lost. If you just hide the userform (Like: Me.Hide) you can Show the userform again and the vals will remain.

As you a "clear all" button, of course you can. You can use the controls' type, or if you have a consistent naming convention, or tags.

Mark
 
Upvote 0
Hi,
Try something like this:

for the calling macro:
Code:
Public gfrmInput As UserForm1

Sub ShowUserform()

If gfrmInput Is Nothing Then Set gfrmInput = New UserForm1

gfrmInput.Show

End Sub

Sub TidyUp()
Set gfrmInput = Nothing
End Sub

Note that this is in a module and to avoid memory leeks you need to call 'TidyUp before exitting.

for the Userform:
Code:
Private Sub btnCancel_Click()
Me.Hide
End Sub

Private Sub btnClear_Click()
Dim Ctl As Control

For Each Ctl In Me.Controls
    If TypeName(Ctl) = "TextBox" Then Ctl.Text = ""
Next Ctl
End Sub

Private Sub btnDoSomething_Click()

MsgBox "Do something here"

Me.Hide
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
        ' X was clicked
        Cancel = True
        btnCancel_Click
    End If
End Sub

The clear code assumes all the input is in textboxes.

Note the code uses 'Me.hide' not 'Unload Me', also note the QueryClose event to stop user clicking on the 'X'.
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,759
Members
449,048
Latest member
excelknuckles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top