I have an excel app I'm developing which fetches some data from a database over a network using SQL. This works fine and returns a recordset which I've passed into an array for calcs.
The intention is to also use this returned recordset to populate a set of comboboxes based on other combobox inputs. Since several comboboxes will be selected I only want the first combobox to return a recordset from the database based on code below then further comboboxes to use the already returned data. I only want to connect over the network once only
The code so far is
If I made "Rs As ADODB.Recordset" a public recordset what is the correct VBA syntax for then using this as a master table to run further SQL statements to filter and sort data into smaller arrays?
Is there a better way perhaps WITHOUT connecting over the network every time?
The intention is to also use this returned recordset to populate a set of comboboxes based on other combobox inputs. Since several comboboxes will be selected I only want the first combobox to return a recordset from the database based on code below then further comboboxes to use the already returned data. I only want to connect over the network once only
The code so far is
Code:
Function Access_Data(wSQL As String) As Variant()
'Requires reference to Microsoft ActiveX Data Objects xx Library [msado15.dll is v2.8]
'Application.VBE.ActiveVBProject.References.AddFromFile "C:\Program Files\Common Files\System\ado\msado15.dll"
Dim Cn As ADODB.Connection, Rs As ADODB.Recordset
Dim MyConn, sSQL As String
Dim vaData As Variant
Dim vaData_transposed(), test As Variant
Dim vaData_rowcount, vaData_columncount As Long
'Set source
MyConn = "\\sr2iw03\ConnectDocuments\xxxxxxxx\Documents\Delivery\xxx\TheProjectManagementFramework\Templates\Editable\CAF 1.2 Data.mdb"
'Create query
sSQL = "SELECT ......... "
fullSQL = sSQL & wSQL
'Debug.Print (fullSQL)
'Create RecordSet
Set Cn = New ADODB.Connection
With Cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open MyConn
Set Rs = .Execute(fullSQL)
End With
If Not Rs.BOF And Not Rs.EOF Then
'write recordset to array VaData
vaData = Rs.GetRows()
vaData_columncount = UBound(vaData)
vaData_rowcount = UBound(vaData, 2)
test = TransposeArray(vaData, vaData_transposed)
Access_Data = vaData_transposed
If test = True Then
vaData_transposed_columncount = UBound(vaData)
vaData_transposed_rowcount = UBound(vaData, 2)
End If
Else
MsgBox ("No Records to Export")
End
End If
Set Cn = Nothing
End Function
If I made "Rs As ADODB.Recordset" a public recordset what is the correct VBA syntax for then using this as a master table to run further SQL statements to filter and sort data into smaller arrays?
Is there a better way perhaps WITHOUT connecting over the network every time?