user3066263
user3066263

Reputation: 11

how to write datatype independent functions in vba (access)

does someone knows how to avoid writing multiple functions which are essentielly the same but having a different datatype as Parameters?

concrete:

Public Function compareDataInt(ByVal value1 As Integer, ByVal value2 As Integer)
Select Case operator
Case 1
  If value1 < value2 Then
    compareDataInt = False
    Exit Function
  End If
 Case 2
   If value1 > value2 Then
    compareDataInt = False
    Exit Function
   End If
  Case 3
    If value1 = value2 Then
     compareDataInt = True
     Exit Function
    Else
        compareDataInt = False
        Exit Function
    End If
end function 

and

Public Function compareData(ByVal value1 As Date, ByVal value2 As Date)
Select Case operator
Case 1
  If value1 < value2 Then
    compareData = False
    Exit Function
  End If
 Case 2
   If value1 > value2 Then
    compareData = False
    Exit Function
   End If
  Case 3
    If value1 = value2 Then
     compareData = True
     Exit Function
    Else
        compareData = False
        Exit Function
    End If

I tried out function compareData(of T)(byval value1 as T ...) but it wouldnt work that way

(I know the coding wont compile, due to simplicity, I shortend the code to the basic issue)

Upvotes: 0

Views: 76

Answers (1)

HansUp
HansUp

Reputation: 97101

Declare the parameters as Variant, as Barranka suggested. Then you can examine the data type of the parameters within the procedure and do whatever is appropriate.

Here is a simple subroutine example.

Public Sub foo(ByVal pValue As Variant)
    Select Case VarType(pValue)
    Case vbInteger, vbDate
        Debug.Print "compare data here"
    Case Else
        Debug.Print "wrong data type"
    End Select
End Sub

Upvotes: 2

Related Questions