|
|
Variants can be useful, even necessary under certain circumstances. However, a procedure that is receiving a variant argument may need to assure that the data actually passed is of a particular. Access VBA does not have a built-in function to determine if a value is actually boolean. This function meets the need. This function could be used, for example, in a routine which has several optional variant arguments. Variants are required because the IsMissing() function requires a variant argument. In that situation, IsBoolean() can be used to ensure that an optional argument that has been passed is, in fact, a boolean. Note: VBA does include a function VarType() which can be used to determine the data type of values assigned to Variants. So the test has been revised to If VarType(rvarValue) = vbBoolean then....
CODE Private Function IsBoolean(rvarValue As Variant) As Boolean ' Procedure: IsBoolean ' ' Description: test a variant to determine if it is boolean or not '-- Const cstrProcedure = "IsBoolean" 'Dim blnResult As Boolean On Error GoTo HandleError 'blnResult = False 'this code yields false positives ' blnResult = False ' If IsNumeric(rvarValue) Then ' If rvarValue = CLng(True) Or rvarValue = CLng(False) Then ' blnResult = True ' End If ' Else ' blnResult = False ' End If If VarType(rvarValue) = vbBoolean Then IsBoolean = True Else IsBoolean = False End If HandleExit: Exit Function HandleError: 'Insert error handling code here Resume HandleExit End Function
|
| This page was last modified 14:27, 30 June 2011. This page has been accessed 842 times. Disclaimers |