Hi All,
I have reperative code which I am trying to sort but can't seem to crack it.
What I am trying to do is put SQL in a sub and then make it connect to a seperate excel sheet.
At the minute I have to put the connect code in each module so is in about 5 times.
Is there anyway to just have the connecting code to the spread sheet in one module and the SQL in another?
This is what I have
Any help would be great
Thank you
I have reperative code which I am trying to sort but can't seem to crack it.
What I am trying to do is put SQL in a sub and then make it connect to a seperate excel sheet.
At the minute I have to put the connect code in each module so is in about 5 times.
Is there anyway to just have the connecting code to the spread sheet in one module and the SQL in another?
This is what I have
Code:
Sub LoadCategoriesUserform()
Dim rstRecordset As ADODB.Recordset
Dim strSQL As String
strSQL = "SELECT * FROM [Sheet1$] WHERE [Category] <> '0' ORDER BY 1"
Set rstRecordset = ExecuteSQLGetRS(strSQL)
Do While Not rstRecordset
UserForm1.ComboBox1.AddItem rs.Fields(0)
rs.MoveNext
Loop
rs.Close
cn.Close
Set cn = Nothing
End Sub
Code:
Public Function ExecuteSQLGetRS(ByVal strSQL As String) As ADODB.Recordset
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
With cn
.Provider = "Microsoft.ace.OLEDB.12.0"
.ConnectionString = "Data Source=" & MyExcelFile & ";" & _
"Extended Properties=Excel 12.0"
.Open
End With
rs.Open strSQL, cn
Set ExecuteSQLGetRS = rs
End Function
Any help would be great
Thank you
Last edited: