そのため、レコードセットから Excel にコピーしようとすると、SQL クエリとして設定しようとする長い文字列がエラーを返すという問題があります。
たとえば、このようなクエリを使用すると
select
cdf.DAY_STAMP Day, b.ACCOUNT_NUMBER Acct#, cdf.CM_DESC MAC,
b.STREET_NUMBER St#, b.STREET_NAME StName, b.CITY City,
b.TRANSPORT_ELEMENT_2 census,
i.ifalias node,
cdf.SUM_BYTES_UP, cdf.SUM_BYTES_DOWN, cdf.SUM_RESET_COUNT, cdf.AVG_TXPOWER_UP,
cdf.MAX_TXPOWER_UP, cdf.MIN_TXPOWER_UP, cdf.AVG_RXPOWER_DOWN, cdf.MAX_RXPOWER_DOWN,
cdf.MIN_RXPOWER_DOWN, cdf.AVG_RXPOWER_UP, cdf.MAX_RXPOWER_UP, cdf.MIN_RXPOWER_UP,
cdf.AVG_PATH_LOSS_UP, cdf.AVG_CER_DOWN, cdf.MAX_CER_DOWN, cdf.MIN_CER_DOWN,
cdf.AVG_CCER_DOWN, cdf.MAX_CCER_DOWN, cdf.MIN_CCER_DOWN, cdf.AVG_SNR_DOWN,
cdf.MAX_SNR_DOWN, cdf.MIN_SNR_DOWN, cdf.US_CER_MAX, cdf.US_CCER_MAX, cdf.US_SNR_MIN,
cdf.STATUS_VALUE_MIN, cdf.TIMING_OFFSET_LAST, cdf.ROWCOUNT, cdf.T3_TIMEOUTS,
cdf.T4_TIMEOUTS, cdf.SYSUPTIME
from cm_day_facts cdf
inner join int_attributes i
on i.ifalias like '72F007%' and i.topologyid = cdf.up_id
inner join billing_data b
on b.equipment_mac = cdf.cm_desc
where cdf.DAY_stamp >= '12-MAY-12 00:00'
エラーが表示されます: オブジェクトが閉じている場合、操作は許可されていません
しかし、次のようなクエリを使用する場合: Select * from "DWRVWR"."CSOC_M_WIPMASTER"
問題なく有効な応答が得られます。
.
完全なコード:
Sub CableData_SQLconn()
'Connect to Oracle server begin
Set sqlCon = New ADODB.Connection
Set sqlCommand = New ADODB.Command
Set sqlRecordSet = New ADODB.Recordset
Dim Conn As String
Dim sqlQuery As String
Dim sqlQuery2 As String
Dim sqlQuery3 As String
'grab node from user input box
node = InputBox("Node")
'Connection string
Conn = "Provider=MSDASQL; Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & "(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=myhost)(PORT=myport))" & "(CONNECT_DATA=(SID=mysid))); uid=user; pwd=pass;"
'Build SQL Query
sqlQuery = "A REALLY LONG STRING"
sqlQuery2 = sqlQuery & "A REALLY LONG STRING"
sqlQuery3 = sqlQuery2 & "A REALLY LONG STRING"
'This output is confirmed to have the full query - **VERIFIED OUTPUT IS CORRECT**
Range("A1").Select
ActiveCell.FormulaR1C1 = sqlQuery3
'---------------------------------------------------------------
' Set file details for SQL query
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 output
Open filePath For Output As #1
'Write full SQL query to file - VERIFIED
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 sqlQuery3
'open recordset
Set sqlRecordSet.ActiveConnection = sqlCon
sqlRecordSet.Open sqlCommand
'copy data to excel
'ActiveSheet.Range("A1").CopyFromRecordset (sqlRecordSet)
If Not sqlRecordSet.EOF Then '<<<<<<<<<<<<<<<<<<<<<<<<<<< ERROR: Operation is not allowed when the object is closed
ActiveSheet.Range("A1").CopyFromRecordset sqlRecordSet
Else: MsgBox "No records returned!"
End If
'close connections
sqlRecordSet.Close
sqlCon.Close
'Close file
Close #1
End Sub