2012-05-12 3 views
0

을 실행 나는 내가 ERROR 받기엑셀 VBA, Select 문 변경 후 경우에, 그래서 문제에 봉착

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' 

같은 쿼리를 사용합니다. "DWRVWR" "CSOC_M_WIPMASTER"

SELECT * FROM : 객체가

를 닫을 때 작업이 허용되지 않습니다하지만 같은 쿼리를 사용하는 경우

문제없이 유효한 응답을받습니다.

.

전체 코드 :

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 
+0

문자열은 얼마나 걸립니까? 나는 8k보다 긴 ADO 쿼리에 문제가있었습니다. –

+0

@GordonLinoff 문자열이 매우 깁니다 ... 30k 문자 ... 어떻게이 문제를 피할 수 있습니까? –

답변

0

당신이 원하는 매개 변수를 사용하여 저장 프로 시저에 쿼리 변환은 다음 Excel에서 저장 프로 시저를 호출하고 통합 문서에 결과를 붙여 넣습니다. 아래에 나열된 예에서는 SQL Server에 연결하고 있지만 Oracle에 연결하는 경우에도 동일하게 작동합니다. 위의 코드의 출발점이었다 책 Professional Excel Development에서이 주제에 대한 좋은 장입니다

Public Sub RunStoredProcedureFromExcel() 
    Dim rsData As ADODB.Recordset 

    Dim ProductID, CategoryID, StoreNumber As Integer 
    ProductID = Sheets("Variables").Range("nrProductID").Value 
    CategoryID = Sheets("Variables").Range("nrCategoryID").Value 
    StoreNumber = Sheets("Variables").Range("nrSelectedStore").Value 

    ' Clear the destination worksheet 
    Sheet39.UsedRange.Clear 

    ' Set the ADODB Connection 
    ConnectToSQLServer 

    ' Create the Recordset object. 
    Set rsData = New ADODB.Recordset 

    ' Open the pooled connection 
    mcnSQLServer.Open 
    mcnSQLServer.espGetLookupData ProductID, CategoryID, StoreNumber, rsData 

    If Not rsData.EOF Then 

     ' Paste the results into workbook 
     Sheet39.Range("a1").CopyFromRecordset rsData 

    Else 
     MsgBox "No data located.", vbCritical, "Error!" 
    End If 

    ' Close the pooled connection 
    mcnSQLServer.Close 

    End Sub 

:

다음은 응용 프로그램과 함께 작동하도록 수정할 수있는 몇 가지 코드입니다.