For Access field value input of any standard data type... is there a datatype which would loose data/precision passing through a String variable as opposed to a Variant variable?
I realize use of the Variant type would be necessary to be able to store more specialized data type, but for sake of this question all of those datatypes are out of scope.
Consider the following datatype validation code:
When I coded these validation functions, I specifically received in the data value to be inspected into a Variant variable.
My other use of a Variant is I took the original author's lead in building a collection class in VBA in that tFalse bln
False byt
True cur
True dtm
True dbl
True dec
False int
False lng
True sng
True str
True vnt
he variable to fetch out a certain object stored in the collection is a Variant so that it is safe for numeric of string collection object identifiers.
In both of these suggested cases, is there actually a valid reason to transport any of the standard VBA variable data types in a Variant rather than simply use a String to transport it? Boolean, is a String truly not capable of transporting a Boolean correctly? So on and on through the VBA core simple data types.
Does anyone have examples of when using a String would loose some precision which a Variant would preserve?
Oh, looks like per my Test() function to smoke test all my data type validator functions, that there was no suitable VBA variable for the Access Dec data type, so for that I had to use a Variant. So maybe that is one singled out example of where using a Variant for a simple data type would be required.
I am thankful,
I realize use of the Variant type would be necessary to be able to store more specialized data type, but for sake of this question all of those datatypes are out of scope.
Consider the following datatype validation code:
Code:
Public Function datatypevalidation_IsByte(ByRef vntExpression As Variant) As Boolean
On Error GoTo Err_datatypevalidation_IsByte
Dim bytTest As Byte
bytTest = vntExpression
datatypevalidation_IsByte = (CStr(bytTest) = CStr(vntExpression))
Exit_datatypevalidation_IsByte:
Exit Function
Err_datatypevalidation_IsByte:
'Bad Return Code
datatypevalidation_IsByte = False
Resume Exit_datatypevalidation_IsByte
End Function
My other use of a Variant is I took the original author's lead in building a collection class in VBA in that tFalse bln
False byt
True cur
True dtm
True dbl
True dec
False int
False lng
True sng
True str
True vnt
he variable to fetch out a certain object stored in the collection is a Variant so that it is safe for numeric of string collection object identifiers.
In both of these suggested cases, is there actually a valid reason to transport any of the standard VBA variable data types in a Variant rather than simply use a String to transport it? Boolean, is a String truly not capable of transporting a Boolean correctly? So on and on through the VBA core simple data types.
Does anyone have examples of when using a String would loose some precision which a Variant would preserve?
Oh, looks like per my Test() function to smoke test all my data type validator functions, that there was no suitable VBA variable for the Access Dec data type, so for that I had to use a Variant. So maybe that is one singled out example of where using a Variant for a simple data type would be required.
Code:
Public Sub datatypevalidation_Test()
Dim blnX As Boolean
Dim bytX As Byte
Dim curX As Currency
Dim dtmX As Date
Dim dblX As Double
Dim decX As Variant
Dim intX As Integer
Dim lngX As Long
Dim sngX As Single
Dim strX As String
Dim vntX As Variant
blnX = CBool("100.1")
bytX = CByte("100.1")
curX = CCur("100.1")
dtmX = CDate(#2/7/2013#)
dblX = CDbl("100.1")
decX = CDec("79,228,162,514,264,337,593,543,950,335")
intX = CInt("100.1")
lngX = CLng("100.1")
sngX = CSng("100.1")
strX = CStr("100.1")
vntX = CDec("100.1")
Debug.Print datatypevalidation_IsBool(vntX) & " bln " & blnX
Debug.Print datatypevalidation_IsByte(vntX) & " byt " & bytX
Debug.Print datatypevalidation_IsCur(vntX) & " cur " & curX
Debug.Print datatypevalidation_IsDate(dtmX) & " dtm " & dtmX
Debug.Print datatypevalidation_IsDbl(vntX) & " dbl " & dblX
Debug.Print datatypevalidation_IsDec(vntX) & " dec " & decX
Debug.Print datatypevalidation_IsInt(vntX) & " int " & intX
Debug.Print datatypevalidation_IsLng(vntX) & " lng " & lngX
Debug.Print datatypevalidation_IsSng(vntX) & " sng " & sngX
Debug.Print datatypevalidation_IsStr(vntX) & " str " & strX
Debug.Print datatypevalidation_IsVnt(vntX) & " vnt " & vntX
End Sub
Code:
Immediate Window:
datatypevalidation_Test
False bln True
False byt 100
True cur 100.1
True dtm 2/7/2013
True dbl 100.1
True dec 79228162514264337593543950335
False int 100
False lng 100
True sng 100.1
True str 100.1
True vnt 100.1
Last edited: