I have the following Function:
Function downloadsqltoexcel(conn As ADODB.Connection, sSQL As String, exceldestinationrangename As String, sqltablename As String, bDownload As Boolean, Optional ws As Worksheet) As Variant
'================================================================================================================================
'== Procedure Name: downloadsqltoexcel
'== Purpose: downloads SQL table data (or query data) to Excel named range or grabs a specific value from an SQL table
'== Notes: ensure that SQL table or query name and Excel named range are identical
'================================================================================================================================
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset
Dim DestinationRange As Range
Dim sqlstring As String
Dim s As String
With rsPubs
.ActiveConnection = conn
.Open sSQL, conn, adOpenStatic, adLockReadOnly, adCmdText
If bDownload Then 'if download switch is on, dump into Excel named range
If ws Is Nothing Then
Set DestinationRange = Range(exceldestinationrangename)
Else
Set DestinationRange = ws.Range(exceldestinationrangename)
End If
With DestinationRange
.ClearContents
.CopyFromRecordset rsPubs
End With
'.... more code below that is not relevant
Let's say I call the function like this:
Dim conPDDB As New ADODB.Connection
conPDDB.Open "myConnectionString" - see below
Dim sSQL as String
sSQL = "SELECT ... "
downloadsqltoexcel conPDDB, sSQL, "DSN", "", True, Sheet1
If I pass a hard-code connection string into myConnectionString I get the results I want. If I pass a DSN connection string the last column of data does not dump into Excel.
See picture
Provider=SQLOLEDB;Data Source=MYSERVER;Initial Catalog=MYDB;User Id=MYUID;Password=MYPWD
DSN=PDDB_QA;UID=MYUID;PWD=MYPWD (DSN configmirrors connection string)
The field itself is returning because when I print ?rsPubs.Fields(5).Value to the immediate window, I recieve AU,CI,GL,IM,PF in return. The last data has commas in this example, but it does not always commas. I have tried playing with DSN settings and also rs.Open arguments. No avail.
Anyone have any ideas?

CopyFromRecordsetmethod does not dump last column when passed with DSN. SQL is identical in both issues so not sure if data is the issue. - Scott HoltzmanOption Base 0orOption Base 1has been set someplace. Finally, have you set a break-point to verify thersPub.Fields.Count? - RalphrsPub.Fields(5).Valuereturns result to immediate window.... this was essentially same as verifyingrsPub.Fields.Count). Re Option Base - are you saying it's set in Excel somewhere or on the DSN Config or something? I have an empty excel file with no option base set to recreate my issue. I run once with String, again with DSN and get above results. - Scott HoltzmanSET NOCOUNT ONatop the SQL statement. - Scott Holtzman