Dataset not bringing back all data from serveer

RichardMGreen

Well-known Member
Joined
Feb 20, 2006
Messages
2,177
Hi all

I'v got a query that's supposed to bring back data into a recordset but it's only bringing back the first two columns.
The query looks like this:-
Code:
Option ExplicitDim conn As ADODB.Connection, cmd As ADODB.Command, rs As ADODB.Recordset
Dim strConn As String, strQry As String
Dim wsData As Worksheet
Dim rngData As Range
Dim param1



Sub refresher3()'----- set up all the initial bits
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
'----- Refresh the running queries table
    ActiveWorkbook.Connections("Query from DW-DEV").Refresh
'----- Now set up the connection -----
    Set conn = New ADODB.Connection
    strConn = "DRIVER=SQL Server;SERVER=DW-DEV;DATABASE=Utilities"
    conn.ConnectionString = strConn
    conn.Open
    Set cmd = New ADODB.Command
    cmd.CommandTimeout = 0
'----- Get a slightly different parameter for the running queries -----
        param1 = Sheets("Stats").Range("M3").Value
'----- get running query data -------
    strQry = "SELECT q.[Date], q.[ObjectName]" & _
        ", [Text] = left(replace(replace(q.Text,char(10),''),char(13),''),500), q.[dbname], q.[session_id]" & _
        ", q.[open_tran], q.[nt_username], q.[nt_domain]" & _
        " FROM Utilities.dbo.tbl_Running_Queries q where q.Date >= '" & _
        param1 & "' and q.Text not like '%tbl_Running_Queries%' order by q.Date desc"
'----- Drop it into a worksheet -----
    cmd.CommandType = adCmdText
    cmd.CommandText = strQry
    cmd.ActiveConnection = conn
    Set rs = cmd.Execute
    Set wsData = Worksheets("Queries")
    Set rngData = wsData.Range("A2")
    rngData.CopyFromRecordset rs
'----- clear down all the connections -----
    Set rs = Nothing
    Set cmd = Nothing
    Set conn = Nothing
'----- switch everything back on -----
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub

The code runs fine but only brings back the first 2 columns.

Anyone any ideas why?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
could it be
", [Text] = left(replace(replace(q.Text,char(10),''),
", q.[Text] = left(replace(replace(q.Text,char(10),''),

SELECT
q.[Date]
, q.[ObjectName]
, [Text] = left(replace(replace(q.Text,char(10),''),char(13),''),500)
, q.[dbname]
, q.[session_id]
, q.[open_tran]
, q.[nt_username]
, q.[nt_domain]
FROM Utilities.dbo.tbl_Running_Queries q
where q.Date >= '" param1 & "'
and
q.Text not like '%tbl_Running_Queries%'
order by q.Date desc
 
Last edited:
Upvote 0
It looks like there's a type in my original code when I posted on here (but it's fine in my VBA).
My code looks like they code you posted.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top