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

ReEntry is a simple boolean value and can have only 2 values: True or False.
The "Not ReEntry" is pretty much same as If ReEntry=False
When the code is initially called, ReEntry is false and it will then set
that flag to True, and it stays True until after the MSValidate() routine is
called, so if this routine is called again by the change in MSValidate() to
TextBox6, the code within the IF Then End IF block won't be executed again.

Take a look at the post by Rick Rothstein earlier - it may offer an even
cleaner solution, which like the other two may need to be set up for the
other TextBox processes that may get triggered during MSValidate() processing.


"WLMPilot" wrote:

Could you explain how the ReEntry works?

Les

"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



.