User Form Button Code

  • Thread starter Thread starter Debs
  • Start date Start date
D

Debs

I set up a userform with a button that on click runs a piece of code
to reset all the fields on the form.

It reads something like this:
-------------------
Private Sub Clearform_Click()

With userform1

..Name = ""
..Address1 = ""
..Address2 = ""
..Address3 = ""
..Address4 = ""
..postcode = ""

End With

End Sub
---------------------


The button worked fine when it was first set up and then all of a
sudden decided it had had enough. Now when the button is clicked it
runs the lines of the macro in order requiring the button to be
clicked again between each one.

I have tried completely replacing the button and code, and have run
the workbook on a different system so the error must be in the
programming and not the computer set up.

What have I done? - Help please!

Thanks
 
Have you coded other events associated with the change event for the
controls that you are clearing - perhaps with error suppression. When you
clear the controls, this could be triggering their change event and an error
could cause the code to halt. I don't see anything wrong with the code you
show. You might put in a global variable at the top of the userform routine
and then have all the event code check the value of that variable before
doing anything. If the variable is set to true, you would add code to
immediately exit the event, and if false to proceed - if that routine needs
to block other events, it would set the variable to true , process, then set
it to false just before exiting. For example

Public bBlockEvents as Boolean

Private Sub Clearform_Click()

if bBlockEvents then exit sub
bBlockEvents = True
With userform1

.Name = ""
.Address1 = ""
.Address2 = ""
.Address3 = ""
.Address4 = ""
.postcode = ""
End With
bBlockEvents = False
End Sub

Private Sub Name_Change()
if bBlockEvents then Exit sub
' process change in textbox Name
' no need to block events

End Sub

as an example
 
Back
Top