3

DoCmd.TransferSpreadsheet用来填充表格。使用表单上的按钮调用此命令。传输完成后,我想告诉用户添加了多少条记录。为了尝试实现这一点,我使用db.OpenRecordset("select * from tblImport") thenMsgBox(rs.RecordCount)
问题是在传输完成之前调用了记录计数。反正有同步调用它吗?

这是完整的代码

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
4

2 回答 2

5

您需要额外的一行:

Set rs = db.OpenRecordset("select * from tblImport")
'Populate recordset
rs.MoveLast
MsgBox rs.RecordCount & " records"
于 2013-01-03T15:05:50.133 回答
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"
于 2013-01-03T18:17:16.123 回答