Request for critique of my IsInt function

michael - what exactly are you trying to do with this function - just confirm you have a integer?

in which case this (untested) ought to work. I think. I will test it now!
(tested and added an extra line)

note that an out of range integer will fail this test. I can't see that you can get round this. Access/VBA does not handle numbers too large to fit into a given variable type. Once you get above max longint, it is very tricky.


Code:
function isint(v as variant) as boolean
dim i as integer 'or use long, if that is what you want
 
on error goto fail
i = v
isint = (i = v)  'just confirm that there was no rounding!
exit function
 
fail:
isint=false
end function
 
Last edited:
michael - what exactly are you trying to do with this function - just confirm you have a integer?

Yup. I finally circled back to one of my original requests from when I began this project... the need to validate user input data. This week I had come across TypeName() which seemed very useful.

I like your simplistic / brute-force solution. :cool: Thank you.

I had just come to the thought of making the error handler simply return false, and see you have an equally simple error handler.

Validation ala KISS... I like it! ;)
 
And here is my simplified solution based on gemma-the-husky's suggestion:

Code:
Public Function datatypevalidation_IsInt(ByVal varExpression As Variant) As Boolean
  On Error GoTo Err_datatypevalidation_IsInt

  Dim intTest As Integer

  intTest = varExpression
  datatypevalidation_IsInt = (intTest = varExpression)

Exit_datatypevalidation_IsInt:
  Exit Function

Err_datatypevalidation_IsInt:
  'Bad Return Code
  datatypevalidation_IsInt = False

  Resume Exit_datatypevalidation_IsInt

End Function
 
One possible further optimization thought occurred to me...

Is there any savings to be gained / problem with receiving the variable in the function ByRef rather than ByVal?

I do not perceive the code making adjustments to the original variable. ByRef would save VBA having to make a copy of the value to pass to the function, which the test function itself will attempt to make yet an additional copy of when it populates the test variable.

Does anyone see a potential issue with changing the receiving of varExpression to ByRef then?
 
All right, I propagated validation functions for all data types. I ended up seeing some strange behavior in the Single / Double / Decimal datatypes, validating between themselves. That caused me to put back in calls to CStr() as follows for most of the validators, OTHER than the String validator.

Code:
Public Function datatypevalidation_IsInt(ByRef vntExpression As Variant) As Boolean
  On Error GoTo Err_datatypevalidation_IsInt

  Dim intTest As Integer

  intTest = vntExpression
  datatypevalidation_IsInt = (CStr(intTest) = CStr(vntExpression))

Exit_datatypevalidation_IsInt:
  Exit Function

Err_datatypevalidation_IsInt:
  'Bad Return Code
  datatypevalidation_IsInt = False

  Resume Exit_datatypevalidation_IsInt

End Function
Decimal I needed to validate this way:

Code:
Public Function datatypevalidation_IsDec(ByRef vntExpression As Variant) As Boolean
  On Error GoTo Err_datatypevalidation_IsDec

  datatypevalidation_IsDec = ("Decimal" = TypeName(vntExpression))

Exit_datatypevalidation_IsDec:
  Exit Function

Err_datatypevalidation_IsDec:
  'Bad Return Code
  datatypevalidation_IsDec = False

  Resume Exit_datatypevalidation_IsDec

End Function
And String I simply perform this way:

Code:
Public Function datatypevalidation_IsStr(ByRef vntExpression As Variant) As Boolean
  On Error GoTo Err_datatypevalidation_IsStr

  Dim strTest As String

  strTest = vntExpression
  datatypevalidation_IsStr = (strTest = vntExpression)

Exit_datatypevalidation_IsStr:
  Exit Function

Err_datatypevalidation_IsStr:
  'Bad Return Code
  datatypevalidation_IsStr = False

  Resume Exit_datatypevalidation_IsStr

End Function
I have switched to receiving the input arg ByRef.

Feedback please.
 

Users who are viewing this thread

Back
Top Bottom