UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> IsMixedNumber    

This function is intended for use with EvalMixedNum so that the input may be verified before evaluation. This function does not tell us whether or not the string is actually a mixed number, but only whether it can be evaluated accurately through the EvalMixedNum() function (for example, IsMixedNumber("1.75") returns True because it is valid input for EvalMixedNum, though it is technically not a mixed number).


' NAME: IsMixedNumber
' Code courtesy of UtterAccess Wiki
' http://www.utteraccess.com/wiki/index.php/Category:FunctionLibrary
' You are free to use it in any application,
' provided this copyright notice is left unchanged.
' PURPOSE: Determines if a number is able to be evaluated by the
'          EvalMixedNum() function.
' DEPENDANCIES: Office 2000 or later (or custom Split and Replace functions)
'               IsFraction() function, which can be found at:
'                   http://www.utteraccess.com/wiki/index.php/IsFraction
' RETURNS: True on sucessfull, false otherwise
'  R01   2010/08/20    INITIAL
'  R02   2010/08/20    MINOR     Forgot to add a redirect to exit when the
'                                Ubound is 0 and the number is numeric
'                                Was getting Err9 on further array evals
'  R03   2010/08/21    MINOR     Changed comparisons using CDbl to CDec
'                                to avoid float issues
'  R04   2010/12/10    MAJOR     Restructured function, removed some function-
'                                ality and placed in IsFraction() function
'                                instead.  Fixed bug where function returned
'                                false when a fraction alone (1/2) was passed
'                                Backward compatibility maintained only when
'                                IsFraction() function is added to application
Public Function IsMixedNumber(ByVal sInput As String) As Boolean
On Error GoTo Error_Proc
Dim Ret As Boolean
 Dim v As Variant 'array to hold portions of the input for evaluation

 'clean up the string from ugly user input

   'remove leading/trailing spaces
   sInput = Trim(sInput)
   'make sure there's something there
   If Len(sInput) = 0 Then
     GoTo Exit_Proc
   End If
   'if there's double spaces replace them with a single space
   sInput = Replace(sInput, "  ", " ")
 'remove the sign if there is one
 If Left(sInput, 1) = "-" Then sInput = Right(sInput, Len(sInput) - 1)
 'at this point, if there's a space seperator and a dash in the
 'expression, the syntax is no good
 If (InStr(1, sInput, " ") <> 0) And (InStr(1, sInput, "-") <> 0) Then
   GoTo Exit_Proc
 End If

 'Split the input into sections to start looking at them
 If InStr(1, sInput, " ") <> 0 Then
   'if there's a space split the string
   v = Split(sInput, " ")
 ElseIf InStr(1, sInput, "-") <> 0 Then
   'if there's a dash split the string
   v = Split(sInput, "-")
   'there's no seperator, send the entire string to v
   v = Split(sInput)
 End If
 If UBound(v) > 1 Then
   'theres more than two elements, no good
   GoTo Exit_Proc
 End If
 If UBound(v) = 0 Then
   'there's only one element, make sure it's numeric
   If IsNumeric(v(0)) = False Then
      'if it's not numeric, see if it's a fraction
      If IsFraction(CStr(v(0))) Then
        'it's a fraction, exit ok
        Ret = True
        GoTo Exit_Proc
        'not a fraction or numeric, no good
        GoTo Exit_Proc
      End If
     'it's  valid number, return true and exit
     Ret = True
     GoTo Exit_Proc
   End If
 End If
 'theres two elements... verify them
 If (IsNumeric(v(0)) = False) Or (InStr(1, v(1), "/") = 0) Then
   'the first element isn't a number or the second one isn't a fraction
   'no good
   GoTo Exit_Proc
 End If
 'make sure the first element is a whole number
 If Int(CDec(v(0))) <> CDec(v(0)) Then
   GoTo Exit_Proc
 End If
 'we've verified the first part of the expression, now we'll
 'verify the second part of the expression
 If IsFraction(CStr(v(1))) Then Ret = True

IsMixedNumber = Ret
Exit Function
Select Case Err.Number
  Case Else
    MsgBox "Error: " & Trim(Str(Err.Number)) & vbCrLf & _
      "Desc: " & Err.Description & vbCrLf & vbCrLf & _
      "Module: lmthNumberEvals, Procedure: IsMixedNumber" _
      , vbCritical, "Error!"
End Select
Resume Exit_Proc
End Function

Creative Commons License
IsMixedNumber by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search

Thank you for your support!
This page has been accessed 6,937 times.  This page was last modified 09:37, 6 April 2011 by Jack Leach.   Disclaimers