2013-05-28 38 views
0

我正在使用ADO查詢MS Access 2003中的表格,並使用CopyFromRecordset方法將數據輸出到Excel 2003工作表。CopyFromRecordset方法沒有將光標移動到最後一行輸出

該表有超過65536條記錄,所以我不能使用DoCmd.TransferSpreadsheet並需要使用帶有ADO的VBA。

我的問題是,在打電話給CopyFromRecordset後即使只有65536記錄輸出,光標停留在1(absolutePosition位置= 1),根據我的理解時,光標應該在65537,準備好到下一個電話CopyFromRecordset

這裏下面的代碼我使用:

Dim oXL As Excel.Application 
Dim adoConn As ADODB.Connection 
Dim adoRS As ADODB.Recordset 
Dim iIndx As Integer 

Dim blnMultipleSheets As Boolean 

Set adoConn = New ADODB.Connection 
Set adoRS = New ADODB.Recordset 

With adoConn 
    .CursorLocation = adUseClient 
    .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=O:\Dev\Support\Recurring_Requests\Future_Deals_Notice_InterestValues_Rates_Data\Future Deals.mdb;Persist Security Info=False" 
    .Open 
End With 

With adoRS 
    .CursorType = adOpenForwardOnly 
    .ActiveConnection = adoConn 
    .CursorLocation = adUseClient 
    .Source = "SELECT * FROM Future_Deals_InterestValues_Rates_Data" 
    .Open 
End With 

Set oXL = New Excel.Application 

With oXL 
    If .Version < 12 Then 
     blnMultipleSheets = True 
    Else 
     blnMultipleSheets = False 
    End If 

    .Visible = True 
    .Workbooks.Add 

    .Range("B2").CopyFromRecordset adoRS 
    If adoRS.RecordCount > .ActiveSheet.Rows.Count Then 
     Do While Not adoRS.EOF 
      .Sheets.Add 
      Range("B2").CopyFromRecordset adoRS 
     Loop 
    End If 
End With 

所以我缺少什麼嗎?

+0

我不相信這是copyfromrecordset的屬性。因爲它是一種範圍方法,光標不應該從您應用的範圍移動。它也沒有在[文檔]中提到(http://msdn.microsoft.com/fr-fr/library/microsoft.office.interop.excel.range.copyfromrecordset(v = office.11​​).aspx) – scott

+1

' rs.GetRows'會移動光標,但你也必須事先指定最大值。要檢索的行數,並且還需要編寫代碼來轉置它返回的數組,因爲它會爲行提供行列。最後,你還可以循環遍歷記錄集中的一行來填充數組,然後將數組粘貼到工作表中。 – Chel

回答

0

此行

.Range("B2").CopyFromRecordset adoRS 

糊劑整個記錄作爲左頂座標將其放置在B2。

所以,刪除你的循環和上面的行。

你可以通過這樣的(僞)記錄循環:

Function pasteRecordSet(ByRef adoRS) 
    For i = 1 To adoRS.RecordCount 
     If i > 65536 Then 
      new sheet 
      + you can call here recursively 
     Else ' on the current sheet 
      adoRS.MoveNext 
      If (adoRS.EOF) Then 
       adoRS.MoveFirst 
      End If 
    Next i 
End Function 
+0

好吧,這個想法是多次使用** CopyFromRecordset **,但它的行爲並不像我預期的那樣 - 循環遍歷記錄並不是我想要的。不管怎麼說,還是要謝謝你。 –

1

這是我用什麼:

Dim iRow, iColumn, iSheet As Integer 

iSheet = 1 
iRow= 1 

With oLibrExce 
     .Visible = True 
     .Workbooks.Add 

    While Not record.EOF 
     .Worksheets.Add 
     Set oHojaExce = .ActiveWorkbook 
     With oHojaExce.Worksheets(1) 
      .Activate 
      .Name = tableName & iSheet 
      For iColumn = 0 To cantidadColumnas - 1 
       .cells(1, iColumn + 1) = Columnas(iColumn) 
       .cells(1, iColumn + 1).Font.Bold = True 
       If TipoColumnas(iColumn) = "DATE" Then 
        .Columns(iColumn + 1).Select 
        .Columns(iColumn + 1).NumberFormat = "m/d/yyyy" 
       End If 
       If TipoColumnas(iColumn) = "NUMBER" Then 
        .Columns(iColumn + 1).Select 
        .Columns(iColumn + 1).NumberFormat = "0.00" 
       End If 
      Next 
      .range("A2").copyfromrecordset record, 1048576 
      iSheet = iSheet + 1 
     End With 
     .cells.EntireColumn.AutoFit 
     .cells(1, 1).Select 
    Wend 
End With 
相關問題