View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default Macro executing twice??

Correct, the TextBox6="" statement is triggering the Change event again. In
addition, that second Change event calls the MSValidate subroutine again as
well. The reason this does not degenerate into an infinite loop is because
once the TextBox is empty, setting it to the empty string does not change
it, so the looping stops after two iterations. If the OP is interested in
changes that are numerical, we could stop the looping by just checking for
the empty string directly...

Private Sub TextBox6_Change()
Dim TB As Integer
If TextBox6.Text < "" Then
TB = 6
MSValidate (TB)
End If
End Sub

--
Rick (MVP - Excel)



"JLatham" wrote in message
...
You're keying off of a change in TextBox6 which calls the MSValidate()
routine. In MSValidate() there is a condition that will then set TextBox6
=
"". That would trigger the TextBox6_Change() event again. I suggest
changing that code to:

Private Sub TextBox6_Change()
Dim TB As Integer
TB = 6
Application.EnableEvents = False
MSValidate (TB)
Application.EnableEvents = True
End Sub

A potential undesirable side effect of this is that you have a run-time
error in the MSValidate() routine, Excel won't respond to events (as a
button
click) until Application.EnableEvents is set to true again (or you close
and
reopen Excel). You can issue the Application.EnableEvents = True
statement
in the VBE Immediate window if this does happen during development.

"WLMPilot" wrote:

I am trying to test the input value of textboxes in a userform. The
correct
value to be entered is 1, 2, 3, or 4. There are four textboxes that
execute
the MSValidate routine. The routines work and the incorrect entries are
caught but the problem is that both routines (see below) execute twice
before
actually returning to the textbox to accept correct entry. I had placed
msgboxes throughout both routines to follow the flow.

Because I was not familiar with the CHANGE() event, I had inserted an
IF-THEN statement in the MSValidate routine to catch a negative number,
not
knowing that as soon as I entered the "-" part of the neg number, the
CHANGE
event executed.

If you can direct me to a better way to trap entries other than a 1-4,
please let me know.


Private Sub TextBox6_Change()
Dim TB As Integer
TB = 6
MSValidate (TB)
End Sub


Sub MSValidate(TB As Integer)
Dim MSQty, Config, Morphine, Ans, Ans1, NegNum As Integer
MSQty = Worksheets("Items").Range("Z3") ' Get MAX quantity of
Morphine
Select Case TB
Case 6
Morphine = Val(TextBox6.Value)
Case 7
Morphine = Val(TextBox7.Value)
Case 8
Morphine = Val(TextBox8.Value)
Case 9
Morphine = Val(TextBox9.Value)
End Select
NegNum = 0
'MsgBox Morphine
If Morphine <= 0 Then
Config = vbOKOnly + vbExclamation
Ans1 = MsgBox("Invalid Quantity. Please enter a 1 - " & MSQty,
Config)
NegNum = 1
GoTo Resetboxes
End If
If Morphine MSQty Then
Config = vbOKOnly + vbExclamation
Ans = MsgBox("Max quantity for Morphine is " & MSQty, Config)
If Ans = vbOK Then
GoTo Resetboxes
End If
End If
Resetboxes:
Select Case TB
Case 6
TextBox6 = ""
TextBox6.SetFocus
Case 7
TextBox7 = ""
TextBox7.SetFocus
Case 8
TextBox8 = ""
TextBox8.SetFocus
Case 9
TextBox9 = ""
TextBox9.SetFocus
End Select
End If
Morphine = 0
NegNum = 0
End Sub


Any ideas of why both execute twice?

Thanks,
Les