N
NZ VBA Developer
G'day!
I have several textboxes in a frame and have written code to validate and
format the value in each textbox on Exit. (An example of the code appears
below.) I'm working with the Exit event because I don't want the textbox to
lose focus if an invalid value is entered. And it works pretty well in most
cases. However...
When I tab out of the last textbox in the frame (or click on another control
that's not in the same frame; e.g. select another tab on the userform), the
code doesn't fire. The value in the textbox doesnt' get validated or
formatted. Apparently VBA doesn't recognise the occurence of an Exit event
for a control _inside_ a frame when focus moves _outside_ the frame.
As promised, an example:
Private Sub txtMargin_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'*** Check to see if a value has been entered in the textbox
If txtMargin.Value <> "" Then
'*** Remove the percent sign (if there is one) and check to see that what's
leftover is a number
If IsNumeric(fcnExtractNumber(txtMargin.Value)) Then
'*** If it is, carry on
Cancel = False
txtMargin.Value =
FormatPercent((fcnExtractNumber(txtMargin.Value) / 100), 2)
Else
'*** And if it's not, throw an error and retain focus
MsgBox "The Margin must be numeric.", vbCritical, "Facility
Interest Error"
Cancel = True
End If
End If
End Sub
There's a bit of mucking about to 'extract' just the numeric bit from the
value in the textbox because the FormatPercent function (and FormatCurrency
function, which I use on some other fields) adds non-numeric characters to
the value - and of course these cause the IsNumeric function to fail if the
user goes back into a field that already has a value in it and then exits
again without deleting the percent sign or dollar sign.
Any ideas on how to make this work when 'exiting' the frame? Maybe a
separate 'validate & format' procedure that I can call on the Exit event for
the frame itself?
Cheers!
I have several textboxes in a frame and have written code to validate and
format the value in each textbox on Exit. (An example of the code appears
below.) I'm working with the Exit event because I don't want the textbox to
lose focus if an invalid value is entered. And it works pretty well in most
cases. However...
When I tab out of the last textbox in the frame (or click on another control
that's not in the same frame; e.g. select another tab on the userform), the
code doesn't fire. The value in the textbox doesnt' get validated or
formatted. Apparently VBA doesn't recognise the occurence of an Exit event
for a control _inside_ a frame when focus moves _outside_ the frame.
As promised, an example:
Private Sub txtMargin_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'*** Check to see if a value has been entered in the textbox
If txtMargin.Value <> "" Then
'*** Remove the percent sign (if there is one) and check to see that what's
leftover is a number
If IsNumeric(fcnExtractNumber(txtMargin.Value)) Then
'*** If it is, carry on
Cancel = False
txtMargin.Value =
FormatPercent((fcnExtractNumber(txtMargin.Value) / 100), 2)
Else
'*** And if it's not, throw an error and retain focus
MsgBox "The Margin must be numeric.", vbCritical, "Facility
Interest Error"
Cancel = True
End If
End If
End Sub
There's a bit of mucking about to 'extract' just the numeric bit from the
value in the textbox because the FormatPercent function (and FormatCurrency
function, which I use on some other fields) adds non-numeric characters to
the value - and of course these cause the IsNumeric function to fail if the
user goes back into a field that already has a value in it and then exits
again without deleting the percent sign or dollar sign.
Any ideas on how to make this work when 'exiting' the frame? Maybe a
separate 'validate & format' procedure that I can call on the Exit event for
the frame itself?
Cheers!