Ms access 访问VBA电子表格计数
我正在使用Ms access 访问VBA电子表格计数,ms-access,vba,Ms Access,Vba,我正在使用DoCmd.TransferSpreadsheet填充表格。正在使用窗体上的按钮调用此命令。传输完成后,我想告诉用户添加了多少记录。为了尝试并实现这一点,我使用了db.OpenRecordset(“select*fromtblimport”) 然后MsgBox(rs.RecordCount) 问题是在传输完成之前调用了记录计数。是否有同步调用的方法 这是完整的代码 Private Sub cmdVIT_Click() On Error Resume Next Dim strPath
DoCmd.TransferSpreadsheet
填充表格。正在使用窗体上的按钮调用此命令。传输完成后,我想告诉用户添加了多少记录。为了尝试并实现这一点,我使用了db.OpenRecordset(“select*fromtblimport”)
然后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
您需要一条额外的线路:
Set rs = db.OpenRecordset("select * from tblImport")
'Populate recordset
rs.MoveLast
MsgBox rs.RecordCount & " records"
您想显示
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"
它应该会起作用。你的代码到底是什么?我的意思是,你能把程序代码剪切并粘贴到你的问题中吗?我刚在导入后尝试通过一个单独的命令按钮运行“RecordCount”,但它仍然只返回一条记录的值。关于记录集或记录计数,有什么我不了解的吗?谢谢!成功了。为什么我需要这一行?当您首先打开记录集时,它并没有填充所有记录。移动最后一个会填充记录集。明白了。再次感谢你的帮助!