0

To create dynamic select statements I am currently creating a temp file to hold the query (30k + characters due to number of joins and decodes being used). I'd like to use this temp file that has the full select statement to return the data into excel.

Using my current code I can only run a short select statements. The strings I create seem to get truncated for some reason.

Here's the snippet of code I'm using that creates the file and currently attempts to run the same string.

' Set file details
fileDir = "C:\temp\"
filePath = "C:\temp\" & node & "_SRO_TCs.sql"

'check if directory exists, if not create it
If Dir(fileDir, cbDirectory) = "" Then
MkDir fileDir
End If

' open the file
Open filePath For Output As #1

'Write to file
outputText = sqlQuery3
Print #1, outputText

'open connection
sqlCon.ConnectionString = Conn
  'Cn.CursorLocation = adUseClient
sqlCon.Open

'set and execute sql command
Set sqlCommand.ActiveConnection = sqlCon
sqlCommand.CommandText = sqlQuery3
sqlCommand.CommandType = adCmdText
sqlCommand.Execute

'open recordset
Set sqlRecordSet.ActiveConnection = sqlCon
sqlRecordSet.Open sqlCommand


'copy data to excel
ActiveSheet.Range("A1").CopyFromRecordset (sqlRecordSet)  <<<< This is where i get an error returned when stepping through the code - "Run-time error '91': Object variable or With block variable not set"

'close connections
sqlRecordSet.Close
sqlCon.Close

'Close file
Close #1

When I check the file created it has a working sql select statement. I'd like to be able to run this file or the string. Please help!

4

1 回答 1

0

命令执行方法如下所示

Set recordset = command.Execute( RecordsAffected, Parameters, Options )

您的代码完全不同。之前没有仔细查看您的代码,因为您说它可以使用较短的 SQL 语句。

于 2012-05-12T16:42:13.487 回答