我正在使用DoCmd.TransferSpreadsheet
来填充表格。使用表单上的按钮调用此命令。传输完成后,我想告诉用户添加了多少条记录。为了尝试和完成这一点,我使用db.OpenRecordset("select * from tblImport")
然后MsgBox(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
答案 0 :(得分:5)
你需要额外的一行:
Set rs = db.OpenRecordset("select * from tblImport")
'Populate recordset
rs.MoveLast
MsgBox rs.RecordCount & " records"
答案 1 :(得分: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"