Hi all...trying and failing to convert this sub to a function that can be called from code / worksheet.
I would like to define the Initial Catalog, Data Source, SQL string and Destination for the data.
When I tried to convert it to a function, I would expect typing =SQLADO( into the formula bar would yield the field menu showing what fields are required, but this is not the case e.g.
=VLOOKUP( > lookup_table, table_array etc
This is the original sub, with RED highlighted areas I would like to send to the function as variables :
[vba]
Sub SQLADO()
Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim stSQL As String
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim rnStart As Range
Const stADO As String = "Provider=SQLOLEDB;Integrated Security=SSPI;" & _
"Persist Security Info=False;" & _
"Initial Catalog=DATABASE;" & _
"Data Source=SERVER"
Set wbBook = ActiveWorkbook
Set wsSheet = wbBook.Worksheets(1)
With wsSheet
Set rnStart = .Range("A1")
End With
stSQL = "SELECT * FROM COUNTRIES"
Set cnt = New ADODB.Connection
With cnt
.CursorLocation = adUseClient
.Open stADO
.CommandTimeout = 0
Set rst = .Execute(stSQL)
End With
'Here we add the Recordset to the sheet from A1
rnStart.CopyFromRecordset rst
'Cleaning up.
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
End Sub
[/vba]
Any help appreciated