Hi all,
I have this function which is called in excel (see code below) that is called in a larger sub while creating a spreadsheet, yhe larger sub takes 13 seconds to complete. The function below is called 135 times during this sub and takes 0.06 seconds to complete each time which is 8.1 seconds of the total 13 seconds the larger sub takes to complete.
Is there a more efficient way to write this code?
The table that the function is interrogating has about 6000 records with 17 individual sets of data per record, can I place all of this table into an array for the duration of the larger sub and then take information from it 135 times, this way I only establish a connection once? Will holding this much data in memory slow the sub down?
Not sure if I have used the correct terminology here but I hope this makes sense.
Code:
Function LocDB(Name As Variant, Col As Integer) As Variant
Dim strRec As Variant
'## Returns data from the table based on the Location Name and the column i.e. col 3 returns Lat
'get the number of records
Call openConnection
Select Case Col
Case 2
strSql = "SELECT Type FROM tblLocation WHERE [Location Name] = """ & Name & """"
Case 3
strSql = "SELECT Latitude FROM tblLocation WHERE [Location Name] = """ & Name & """"
Case 4
strSql = "SELECT Longitude FROM tblLocation WHERE [Location Name] = """ & Name & """"
Case 5
strSql = "SELECT Elevation FROM tblLocation WHERE [Location Name] = """ & Name & """"
Case 6
strSql = "SELECT Variation FROM tblLocation WHERE [Location Name] = """ & Name & """"
Case 7
strSql = "SELECT Lat FROM tblLocation WHERE [Location Name] = """ & Name & """"
Case 8
strSql = "SELECT Long FROM tblLocation WHERE [Location Name] = """ & Name & """"
Case 9
strSql = "SELECT VarCorr FROM tblLocation WHERE [Location Name] = """ & Name & """"
Case 10
strSql = "SELECT FIA FROM tblLocation WHERE [Location Name] = """ & Name & """"
Case 11
strSql = "SELECT Fuel FROM tblLocation WHERE [Location Name] = """ & Name & """"
Case 12
strSql = "SELECT User FROM tblLocation WHERE [Location Name] = """ & Name & """"
Case 13
strSql = "SELECT Lock FROM tblLocation WHERE [Location Name] = """ & Name & """"
Case 14
strSql = "SELECT PerfClass FROM tblLocation WHERE [Location Name] = """ & Name & """"
Case 15
strSql = "SELECT [Rig Type] FROM tblLocation WHERE [Location Name] = """ & Name & """"
End Select
cn.Open strConnection
Set rs = cn.Execute(strSql)
strRec = rs.Fields(0)
If IsNull(strRec) Then strRec = ""
Call closeConnection
LocDB = strRec
End Function
Thanks for your help,
Marcus