我有用MS Access 2010(.mbd文件)编写的代码VBA代码,用于将数据写入Excel文件(.xls文件)。下面是将数据写入该excel文件的代码。当您运行此代码时,它始终抛出错误#1004“应用程序定义或对象定义的错误”。当你调试代码(F8)或运行它(F5)时,它运行绝对没有任何问题。我仍然无法弄清楚究竟是什么问题。在MS Access 2007中执行时,此代码可以正常工作。
下面是正在执行的thr代码,当它失败时,焦点设置在标记为double astriek mark的代码的第3行最后一行。
Sub PopulateReport(appExcel As Object, testcam)
Dim Site As String, intRec As Integer, i As Integer, cnt As Integer, intRecSet As Integer, cntr As Integer
Dim F1 As String, F2 As String, F3 As String, F4 As String, F5 As String, F6 As String, F7 As String
Dim F8 As String, F9 As String, F10 As String, F11 As String, F12 As String, F13 As String, CAMDate As Date
Close
i = 0
cnt = 0
cntr = 0
Set cnn = CurrentProject.Connection
rec.Open "SELECT * FROM Site", cnn, adOpenStatic, adLockPessimistic
rec.MoveLast
rec.MoveFirst
intRec = rec.RecordCount
Do Until cnt = intRec
rec.MoveLast
rec.MoveFirst
rec.Move cnt
Site = rec(4)
Select Case Site
Case "Fort Worth"
cntr = 0
recset.Open "SELECT * FROM Employee", cnn, adOpenStatic, adLockPessimistic
recset.MoveLast
recset.MoveFirst
intRecSet = recset.RecordCount
appExcel.Application.Goto Reference:="START_FW_CL"
Do Until cntr = intRecSet - 1
appExcel.Selection.EntireRow.Copy
appExcel.Selection.EntireRow.Insert
cntr = cntr + 1
Loop
appExcel.Application.CutCopyMode = False
appExcel.Application.Goto Reference:="START_FW2_CL"
go = appExcel.Application.Range("START_FW2_CL")
cntr = 1
With appExcel.Worksheets("Accts. > Clearing").[START_FW2_CL]
Do Until recset.EOF
**.Offset(cntr, 0) = recset(0)**
.Offset(cntr, 1) = recset(1)
.Offset(cntr, 2) = recset(2)
End Sub