View Single Post
  #12   Report Post  
Posted to microsoft.public.excel.programming
JLatham JLatham is offline
external usenet poster
 
Posts: 2,203
Default Macro executing twice??

Rick Rothstein also came up with an interesting solution that doesn't use a
flag like Bob Phillips or the EnableEvents trick and may be best of all --
still may require changes to other TextBox#_Change event processors in
similar fashion.

Which all goes to show once again that there is definitely more than one way
to beat a dead horse with Excel.

"Joe User" wrote:

"JLatham" wrote:
Bob's solution is the better of the two: it [...] doesn't run the risk
of leaving event processing disabled in the event of an error in
downstream code.


But when it is corrected (Static ReEntry instead of Dim ReEntry), I think it
runs the risk of effectively disabing the TextBox6_Change macro -- at least,
the portion executed when ReEntry is False.

Moreover, as you noted elsewhere in this thread, it requires similar code to
be implemented in all other event macros that might be triggered (or
re-triggered) unintentionally when MSValidate is executed.

(In fact, ReEntry might need to be a global variable if you want the same
effect as disabling EnableEvents.)

I think that error recovery must be dealt with in either case.

And if that is done correctly, I think that disabling EnableEvents is the
cleaner solution -- unless the intention is to permit other events to be
triggered when MSValidate is executed.

(Not intended, based on my brief reading of the original posting.)


----- original message -----

"JLatham" wrote in message
...
MLMPilot - Bob's solution is the better of the two: it achieves the same
goal
and doesn't run the risk of leaving event processing disabled in the event
of
an error in downstream code.


"Bob Phillips" wrote:

Try this

Private Sub TextBox6_Change()
Dim ReEntry As Boolean
Dim TB As Integer

If Not ReEntry Then

ReEntry = True

TB = 6
MSValidate (TB)

ReEntry=False
End If
End Sub

--

HTH

Bob

"WLMPilot" wrote in message
...
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



.


.