i have a vba function within EXCEL that creates a connection to a Access database, and opens a query to retreive a value from Access and place it in a cell in Excel.
I now want to loop 300,000 rows in excel and run this function, so for each row, it will take 2 values and use this function to retreive data from Access. It will open and close the connection for each loop.
QUESTION: Would it be faster, more efficient, less memory usage, etc If I opened the connection within a Subroutine first and left it open during the entire 300,000 row loop then close it.
Or opening and closing the connection for each run is ok, any best practices for connecting to Access on large VBA excel loops? ** this function only retrieves 1 value not a list or array.
I now want to loop 300,000 rows in excel and run this function, so for each row, it will take 2 values and use this function to retreive data from Access. It will open and close the connection for each loop.
QUESTION: Would it be faster, more efficient, less memory usage, etc If I opened the connection within a Subroutine first and left it open during the entire 300,000 row loop then close it.
Or opening and closing the connection for each run is ok, any best practices for connecting to Access on large VBA excel loops? ** this function only retrieves 1 value not a list or array.
Code:
SUB LoopClosePrice
set rng = range("a2:a300000")
for each Cell in Rng
sym = cell.offset(0,-1)
date1 = cell.offset(0,-2)
cell.value = GetClosePrice(sym, date1)
next
END SUB
FUNCTION GetClosePrice (ticker as string, date1 as string)
dim rs as New ADODB.RecordSet
dim cn As New ADODB.Connection
cn.Open ConnectionString
Dim Query as String
query = ' query to retrieve '
rs.Open query, cn, adOpenForwardOnly, adLockReadOnly
GetClosePrice = rs.Fields(0)
set rs = Nothing
set cn = nothing
END FUNCTION