Xscatolare
IS-IT--Management
OK, i have Googled high and hard for a couple days to figure this out and just have to post because I find something close but not complete.
Here setting up the sql string. constring is a public varibable.
Here is the ImportSQLRange. It basically opens connection and then will insert into a specific cell.
I have several subs that run similar SQL strings however will be called at different times. Since I am going about it this way I am trying to append the return recordset to the table created (right now called table1). Each SQL string can have a returned recordset of either zero records, one record or many records.
I am wanting to do it this way to avoid using access to pull the data and then link back from Excel to retrieve. I have other sheets in the workbook that contain data points that will use Index/Match to search this table of data.
I hope this makes sense.
Here is the closest I got to adding it to the table however each run over writes the previous entry. It was placed in the Function just after testing the recordset zero records.
Thank you for any time and support.
Here setting up the sql string. constring is a public varibable.
Code:
Sub ReplenQueue()
Dim strSql As String
Dim target As Range
Set target = Worksheets("Imports").Range("tblimport")
strSql = "SELECT 'REG REPLEN WIP', Count(dbo.movewkahdr.num_move)" & _
" FROM dbo.movewkahdr INNER JOIN dbo.movewqdtl ON dbo.movewkahdr.repl_wrk_asgn = dbo.movewqdtl.repl_wrk_asgn" & _
" WHERE (((dbo.movewkahdr.wrk_fnc_code) In (60,62,63,64,66,67,82,83)));"
Call ImportSQLtoRange(constring, strSql, target)
End Sub
Here is the ImportSQLRange. It basically opens connection and then will insert into a specific cell.
Code:
Function ImportSQLtoRange(ByVal constring As String, ByVal query As String, target As Range) As Integer
On Error Resume Next
' Object type and CreateObject function are used instead of ADODB.Connection,
' ADODB.Command for late binding without reference to
' Microsoft ActiveX Data Objects 2.x Library
' Dim con As ADODB.Connection
Dim con As Object
Set con = CreateObject("ADODB.Connection")
con.ConnectionString = constring
' Dim cmd As ADODB.Command
Dim cmd As Object
Set cmd = CreateObject("ADODB.Command")
cmd.CommandText = query
cmd.CommandType = 1 ' adCmdText
' The Open method doesn't actually establish a connection to the server
' until a Recordset is opened on the Connection object
con.Open
cmd.ActiveConnection = con
' Dim rst As ADODB.Recordset
Dim rst As Object
Set rst = cmd.Execute
If rst Is Nothing Then
con.Close
Set con = Nothing
ImportSQLtoRange = 1
Exit Function
End If
Dim ws As Worksheet
Dim col As Integer
Set ws = target.Worksheet
' Column Names
For col = 0 To rst.Fields.Count - 1
ws.Cells(target.Row, target.Column + col).value = rst.Fields(col).Name
Next
ws.Range(ws.Cells(target.Row, target.Column), _
ws.Cells(target.Row, target.Column + rst.Fields.Count)).Font.Bold = True
' Data from Recordset
ws.Cells(target.Row + 1, target.Column).CopyFromRecordset rst
rst.Close
con.Close
Set rst = Nothing
Set cmd = Nothing
Set con = Nothing
ImportSQLtoRange = 0
End Function
I have several subs that run similar SQL strings however will be called at different times. Since I am going about it this way I am trying to append the return recordset to the table created (right now called table1). Each SQL string can have a returned recordset of either zero records, one record or many records.
I am wanting to do it this way to avoid using access to pull the data and then link back from Excel to retrieve. I have other sheets in the workbook that contain data points that will use Index/Match to search this table of data.
I hope this makes sense.
Here is the closest I got to adding it to the table however each run over writes the previous entry. It was placed in the Function just after testing the recordset zero records.
Code:
With ThisWorkbook.Sheets("Imports").ListObjects("Table1")
If Not .DataBodyRange Is Nothing Then
.DataBodyRange.Delete
End If
Call .Range(2, 1).CopyFromRecordset(rst)
End With
Thank you for any time and support.