home *** CD-ROM | disk | FTP | other *** search
- Attribute VB_Name = "Utilities"
- Option Explicit
-
- '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- ' Name: SafeCDate
- ' Description:
- ' 'Safely' converts a variant to a date. i.e handles null values properly
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
-
- Public Function SafeCDate(value As Variant) As Date
- If IsEmpty(value) Or IsNull(value) Or value = "" Then
- SafeCDate = #12:00:00 AM#
- Else
- SafeCDate = CDate(value)
- End If
- End Function
-
-
- '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- ' Name: ValidatePrimitiveTypes
- ' Description:
- ' Tests the param 'Data' against the valid values of 'DataType'.
- ' If the data is valid then returns 'True'. If the data is not valid
- ' then presents a MsgBox with vail ranges. e.g if 'DataType' = "Byte" then
- ' the MsgBox is 'Identifier' & " must be a number between 0 and 255".
- ' If 'Identifier' = "" Then 'Identifier' = "Value".
- ' Returns:
- ' True:= 'Data' is valid , False: 'Data' is not valid
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
-
- Public Function ValidatePrimitiveTypes(DataType As String, Data As Variant, Identifier As String) As Boolean
- On Error GoTo ValidatePrimitiveTypes_Err:
- Dim l As Long
- Dim d As Double
- If Identifier = "" Then
- Identifier = "Value"
- Else
- Identifier = """" & Identifier & """"
- End If
-
- Select Case DataType
- Case Is = "Boolean"
- 'True/False or 1/0
- If IsNumeric(Data) Then
- If Data = 1 Or Data = 0 Then
- ValidatePrimitiveTypes = True
- Else
- ValidatePrimitiveTypes = False
- MsgBox Identifier & " must be either the text ""True"" or ""False"" or the numbers -1(true) or 0(false)"
- End If
- ElseIf Data = "True" Or Data = "False" Then
- ValidatePrimitiveTypes = True
- Else
- ValidatePrimitiveTypes = False
- MsgBox Identifier & " must be either the text ""True"" or ""False"" or the numbers 1 or 0"
- End If
- Case Is = "Byte"
- '0-255
- If IsNumeric(Data) Then
- l = Data
- If l >= 0 And l <= 255 Then
- ValidatePrimitiveTypes = True
- Else
- ValidatePrimitiveTypes = False
- MsgBox Identifier & " must be a number between 0 and 255"
- End If
- Else
- ValidatePrimitiveTypes = False
- MsgBox Identifier & " must be a number between 0 and 255"
- End If
- Case Is = "Integer"
- '-32,768 to -32,768
- If IsNumeric(Data) Then
- l = Data
- If l >= -32768 And l <= 32768 Then
- ValidatePrimitiveTypes = True
- Else
- ValidatePrimitiveTypes = False
- MsgBox Identifier & " must be a number between -32,768 and 32,768"
- End If
- Else
- ValidatePrimitiveTypes = False
- MsgBox Identifier & " must be a number between -32,768 and 32,768"
- End If
- Case Is = "Long"
- '-2,147,483,648 to 2,147,483,647
- If IsNumeric(Data) Then
- l = Data
- If l >= -2147483648# And l <= 2147483647 Then
- ValidatePrimitiveTypes = True
- Else
- ValidatePrimitiveTypes = False
- MsgBox Identifier & " must be a number between -2,147,483,648 and 2,147,483,647"
- End If
- Else
- ValidatePrimitiveTypes = False
- MsgBox Identifier & " must be a number between -2,147,483,648 and 2,147,483,647"
- End If
- Case Is = "Currency"
- '-922,337,203,685,477.5808 to 922,337,203,685,477.5807
- If IsNumeric(Data) Then
- d = Data
- If d >= -9.22337203685478E+18 And d <= 9.22337203685478E+18 Then
- Dim c As Currency
- c = d
- ValidatePrimitiveTypes = True
- Else
- ValidatePrimitiveTypes = False
- MsgBox Identifier & " must be a ammount between -922,337,203,685,477.5808 and 922,337,203,685,477.5807"
- End If
- ValidatePrimitiveTypes = True
- Else
- ValidatePrimitiveTypes = False
- MsgBox Identifier & " must be an ammount between -922,337,203,685,477.5808 and 922,337,203,685,477.5807"
- End If
- Case Is = "Date"
- 'January 1, 100 A.D. through December 31, 9999 A.D.
- If IsDate(Data) Then
- ValidatePrimitiveTypes = True
- Else
- ValidatePrimitiveTypes = False
- MsgBox Identifier & " must be a date between January 1, 100 A.D. and December 31, 9999 A.D."
- End If
- Case Is = "Single"
- '-3.402823E38 To 3.402823E38
- If IsNumeric(Data) Then
- d = Data
- If d >= -3.402823E+38 And l <= 3.402823E+38 Then
- ValidatePrimitiveTypes = True
- Else
- ValidatePrimitiveTypes = False
- MsgBox Identifier & " must be a number between -3.402823E+38 and 3.402823E+38"
- End If
- Else
- ValidatePrimitiveTypes = False
- End If
- Case Is = "Double"
- '-1.79769313486232E308 To 1.79769313486232E308
- If IsNumeric(Data) Then
- ValidatePrimitiveTypes = True
- Else
- ValidatePrimitiveTypes = False
- MsgBox Identifier & " must be a number between -1.79769313486232E+308 and 1.79769313486232E+308"
- End If
- Case Is = "String"
- ValidatePrimitiveTypes = True
- Case Is = "Variant"
- ValidatePrimitiveTypes = True
- End Select
- Exit Function
-
- ValidatePrimitiveTypes_Err:
- Debug.Print Err.Number & " " & Err.Description
- MsgBox Identifier & " is too large or to small a number"
- ValidatePrimitiveTypes = False
- End Function
-
-