2013-01-03 61 views
3

我正在使用DoCmd.TransferSpreadsheet填充表。該命令使用表單上的按鈕進行調用。傳輸完成後,我想告訴用戶添加了多少條記錄。要嘗試並完成此操作,我使用db.OpenRecordset("select * from tblImport") 然後MsgBox(rs.RecordCount)
問題是在傳輸完成之前正在調用記錄計數。無論如何要同步調用它?訪問VBA TransferSpreadsheet計數

下面是完整的代碼

Private Sub cmdVIT_Click() 
On Error Resume Next 

Dim strPath As String 
Dim filePicker As FileDialog 
Dim db As DAO.Database 
Dim rs As DAO.Recordset 

Set db = CurrentDb 

Set filePicker = Application.FileDialog(msoFileDialogFilePicker) 

With filePicker 
    .AllowMultiSelect = False 
    .ButtonName = "Select" 
    .InitialView = msoFileDialogViewList 
    .Title = "Select File" 

    With .Filters 
     .Clear 
     .Add "All Files", "*.*" 
    End With 
    .FilterIndex = 1 

    .Show 
End With 

strPath = filePicker.SelectedItems(1) 
Debug.Print strPath 
DoCmd.TransferSpreadsheet TransferType:=acImport, SpreadsheetType:=acSpreadsheetTypeExcel12, TableName:="tblImport", FileName:=strPath, HasFieldNames:=True 
Set rs = db.OpenRecordset("select * from tblImport") 

MsgBox rs.RecordCount & " records" 
End Sub 
+0

它應該工作。你的代碼是什麼? – Fionnuala

+0

我的意思是,你可以將程序代碼剪切並粘貼到你的問題中嗎? – Fionnuala

+0

剛剛添加了代碼 – DasPete

回答

4

你需要一個額外的行:

Set rs = db.OpenRecordset("select * from tblImport") 
'Populate recordset 
rs.MoveLast 
MsgBox rs.RecordCount & " records" 
+0

謝謝!那樣做了。爲什麼我需要這條線? – DasPete

+1

首先打開記錄集時,不會填充所有記錄。移動上一個填充記錄集。 – Fionnuala

+0

明白了。再次感謝你的幫助! – DasPete

2

你想顯示包含在tblImport的行數。我不認爲你需要一個記錄集才能提供這些信息。嘗試其中之一......

MsgBox CurrentDb.TableDefs("tblImport").RecordCount & " records" 
MsgBox DCount("*", "tblImport") & " records" 

不過,如果你需要,或者只是想有一個記錄做到這一點,使用更快的方法爲OpenRecordset

Set rs = db.OpenRecordset("tblImport", dbOpenTable, dbReadOnly) 
rs.MoveLast 
MsgBox rs.RecordCount & " records"