尝试将数据库从Microsoft Access导出到Excel(2016)时出现错误(3061)

时间:2016-05-12 16:28:22

标签: vba ms-access

我收到此错误

  

“运行时错误3061 =太少参数。预期1”

当尝试在vba中运行以下代码以将我的数据库从Access导出到Excel(2016)时: -

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim SQL As String
Dim rs1 As DAO.Recordset
Dim i As Integer
DoCmd.Hourglass (True)

SQL = "SELECT PartNo, PartName, Price, SalePrice, " & _
"(Price - SalePrice) / Price AS Discount " & _
"FROM Parts " & _
"ORDER BY PartNo "

Set rs1 = CurrentDb.OpenRecordset(SQL, dbOpenSnapshot)
Set xlApp = Excel.Application
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)

With xlSheet
       Do While Not rs1.EOF
        .Range("A" & i).Value = Nz(rs1!PartNo, "")
        .Range("B" & i).Value = Nz(rs1!PartName, "")
        .Range("C" & i).Value = Nz(rs1!Price, 0)
        .Range("D" & i).Value = Nz(rs1!SalePrice, 0)
        .Range("F" & i).Value = Nz(rs1!Discount, 0)

        i = i + 1
        rs1.MoveNext
     Loop
 End With
SubExit:
On Error Resume Next

DoCmd.Hourglass False
xlApp.Visible = True
rs1.Close
Set rs1 = Nothing

Exit Sub

SubError:
MsgBox "Error Number: " & Err.Number & "= " & Err.Description,    vbCritical + vbOKOnly, _
    "An error occurred"
GoTo SubExit

和vba在调试中突出显示了这一行xlApp.Visible = True, 我从youtube的一些教程得到了这个代码,他们没有得到这个错误。 那么你认为导致这个错误的是什么? 谢谢

1 个答案:

答案 0 :(得分:0)

问题可能在于您的SQL /记录集。使用代码中的debug.print语句复制sql字符串,然后将其粘贴到新查询中,以检查是否得到了您期望的结果。还要检查表和记录集中的字段名称。