ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Declare Function for global use then call on change event (https://www.excelbanter.com/excel-programming/401229-declare-function-global-use-then-call-change-event.html)

[email protected]

Declare Function for global use then call on change event
 
Say i wanted to have a numbersonly function

I would Declare this in a new module, but when i call it

Function MyFunction(NewText as string)
On Error GoTo ErrorHandler
If IsNumeric(Chr(strTempAscii)) Or Chr(strTempAscii) = "-" Or _
Chr(strTempAscii) = "/" Or Chr(strTempAscii) = "\" Then

Else
strTempAscii = ""
TxtDtoAtto2.Text = Left(TxtDtoAtto2.Text, Len(TxtDtoAtto2.Text) -
1)
End If

ErrorHandler:
End Function


When i call it Like


Private Sub TxtDtoAtto2_Change()
MyFunction( TxtDtoAtto2)
End sub
Private Sub TxtDtoAtto2_KeyPress(ByVal KeyAscii As
MSForms.ReturnInteger)
strTempAscii = ""
strTempAscii = KeyAscii
End Sub

it doesnt work, Is there something im not doing?

Jim Thomlinson

Declare Function for global use then call on change event
 
Functions are intended to return values. Sum is a function that returns the
total value of all of the numbers. Your function is not returning a value.
You are passing in a value by reference and modifying it that way. In general
functions don't modify the inputs (not always true but generally a good
practice in VB). For example sum does not change any of the numbers that are
passed in. Take a look at this...

Function MyFunction(byval NewText as string) as string
On Error GoTo ErrorHandler
MyFunction = ""
If IsNumeric(Chr(strTempAscii)) Or Chr(strTempAscii) = "-" Or _
Chr(strTempAscii) = "/" Or Chr(strTempAscii) = "\" Then

Else
strTempAscii = ""
MyFunction = Left(NewText , Len(NewText ) -1)
End If

ErrorHandler:
End Function


'your function is still changing strTempAscii which is generally considered
to be
'a side effect. You normally want to avoid side effects in functions.
Private Sub TxtDtoAtto2_Change()
TxtDtoAtto2 = MyFunction( TxtDtoAtto2.text)
End sub
Private Sub TxtDtoAtto2_KeyPress(ByVal KeyAscii As
MSForms.ReturnInteger)
strTempAscii = ""
strTempAscii = KeyAscii
End Sub

--
HTH...

Jim Thomlinson


" wrote:

Say i wanted to have a numbersonly function

I would Declare this in a new module, but when i call it

Function MyFunction(NewText as string)
On Error GoTo ErrorHandler
If IsNumeric(Chr(strTempAscii)) Or Chr(strTempAscii) = "-" Or _
Chr(strTempAscii) = "/" Or Chr(strTempAscii) = "\" Then

Else
strTempAscii = ""
TxtDtoAtto2.Text = Left(TxtDtoAtto2.Text, Len(TxtDtoAtto2.Text) -
1)
End If

ErrorHandler:
End Function


When i call it Like


Private Sub TxtDtoAtto2_Change()
MyFunction( TxtDtoAtto2)
End sub
Private Sub TxtDtoAtto2_KeyPress(ByVal KeyAscii As
MSForms.ReturnInteger)
strTempAscii = ""
strTempAscii = KeyAscii
End Sub

it doesnt work, Is there something im not doing?



All times are GMT +1. The time now is 10:36 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com