When to use Variant

stalakos
10-24-2002, 05:01 AM
Hi,

I have got these two functions...

Private Function ABCScore(bytAttNum As Byte, strField As String) As Single
Dim rst As ADODB.Recordset
Dim strsql As String
strsql = " SELECT tblAttributeIntervals.[" & strField & "] " _
& " FROM tblAttributeIntervals " _
& " WHERE (((tblAttributeIntervals.Attribute) = " & bytAttNum & "));"
Set rst = OpenADORecordSet(strsql)
ABCScore = rst(strABC)
rst.Close
End Function

Private Function GetFieldName(bytAttNum As Byte, strField) As String
Dim rst As ADODB.Recordset
Dim strsql As String
strsql = " SELECT tblAttributes.[" & strField & "] " _
& " FROM tblAttributes " _
& " WHERE (((tblAttributes.AttributeNo)=" & bytAttNum & "));"
Set rst = OpenADORecordSet(strsql)
GetFieldName = rst(strField)
rst.Close
End Function

that both use another function:

Private Function OpenADORecordSet(strsql As String) As ADODB.Recordset
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
Set rst.ActiveConnection = CurrentProject.Connection
rst.Open strsql
Set OpenADORecordSet = rst
End Function

Now the top two Functions essentially do the same thing, expect they return a different type of data back.

Would it be better to combine the two into something like this:

Private Function ValueFromRecordset(strsql As String, strField As String) As Variant
Dim rs As ADODB.Recordset
Set rs = OpenADORecordSet(strsql)
ValueFromRecordset = rs(strField)
rs.Close
End Function

where I use the Variant variable to make it more generic.

really my question is:

Will my code be quicker with one function using the Variant or with two functions using a specific variable type?

or is it six of one and half of the other??

norm
10-24-2002, 06:13 AM
Unless you're executing it many times consecutively, i don't think it would be noticable. But to answer your question, i'm pretty sure two specific data types would be faster. Constants would help too.

if these attributes are the same could you return them both in one query?

maybe you could test your idea by executing it 1000 times or so each... someting like...

debug.print now()
for i = 1 to 1000

ABCscore(this,"that")

next i
debug.print now()

good luck...
norm

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum