运行时错误'2498' - 访问2010 VBA - DoCmd.TransferSpreadsheet

时间:2015-06-08 12:03:17

标签: access-vba

我正在编写一个基于用户输入过滤查询的脚本,然后此查询中的数据将用于创建新表qryMyExportedData。从那里,数据将导出到ExportedData.xlsx。当我尝试运行我的代码时,出现以下错误:

Run-time error '2498': An expression you entered is the wrong data type for one of the arguments.

我的代码:

Private Sub Query_Click()
  Dim strExport as String
  strExport = "SELECT * FROM qryCostDepLosses WHERE [Maintenance Type]  = '" & Me.MainType & "' AND [Date] = #" & Me.Date & "#"

  Set qdfNew = CurrentDb.CreateQueryDef("myExportQueryDef", strExport)

  DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "myExportQueryDef", CurrentProject.Path & "\ExportedData.xlsx", "True"

  CurrentDb.QueryDefs.Delete qdfNew.Name 'cleanup

End Sub

我已将错误范围缩小到DoCmd.TransferSpreadsheet行。我相信strExport看起来应该如此。数据也会导出到myExportQueryDef。

1 个答案:

答案 0 :(得分:1)

您正在尝试将布尔值作为字符串发送。所以代码应该是这样的:

Private Sub Query_Click()
  Dim strExport as String
  strExport = "SELECT * FROM qryCostDepLosses WHERE [Maintenance Type]  = '" & Me.MainType & "' AND [Date] = #" & Me.Date & "#"

  Set qdfNew = CurrentDb.CreateQueryDef("myExportQueryDef", strExport)

  DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "myExportQueryDef", CurrentProject.Path & "\ExportedData.xlsx", True

  CurrentDb.QueryDefs.Delete qdfNew.Name 'cleanup

End Sub

基本上只需删除True周围的引号。