MS Access运行时错误'3011以一个代码显示,而不以另一个代码显示

时间:2018-10-05 00:21:40

标签: access-vba runtime-error ms-access-2016

周年快乐,祝社区生活愉快 我正在尝试学习使用DoCmd.TransferSpreadsheet将查询和图表导出到Excel 在我的数据库中,我有两种形式可在每一种中导出对Excel的查询并创建图表 在每个表单中,用户在文本框中选择一个值,并且图像显示在表单上 在表格frm_createxlstacked中,用户选择两个日期,然后单击命令按钮以将查询导出到Excel并创建一个xlClustered图表。此VBA代码可以正常工作。

这是createxlstacked的VBA代码

Private Sub cmbexpqry_stacked_Click()

Dim wb As Object
Dim xl As Object
Dim sExcelWB As String
Dim ws As Worksheet
Dim r As Range
Dim ch As Object ''Excel.Chart
Dim mychart As ChartObject
Dim myMax, myMin As Double
Dim qry_createxlstacked As Object
Dim fullPhotoPath   As String

If IsNull(Me.cbxclstacked.Value) Then Exit Sub
Dim wb As Object, xl As Object, ch As Object, mychart As ChartObject
Dim fullPhotoPath   As String

fullPhotoPath = Add_PlotMap(Form_frm_createxlstacked.cbxclstacked.Value)
Set xl = CreateObject("excel.application")
On Error Resume Next
Kill TrailingSlash(CurrentProject.Path) & Form_frm_createxlstacked.cbxxlstacked.Value & "qry_createxlstacked.xlsx"
Err.Clear
On Error GoTo 0
sExcelWB = TrailingSlash(CurrentProject.Path) & "qry_createxlstacked.xlsx"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qry_createxlstacked.xlsx", sExcelWB, True
Set wb = xl.Workbooks.Open(sExcelWB)
Set ws = wb.Sheets("qry_createxlstacked.xlsx")
Set ch = ws.Shapes.AddChart.Chart
Set mychart = ws.ChartObjects("Chart 1")
ws.Shapes.AddPicture fullPhotoPath, msoFalse, msoCTrue, r.Left, r.Top, 500, 250

With ch
    .ChartType = xlColumnClustered
    .SeriesCollection(2).AxisGroup = 2
    .SeriesCollection(2).ChartType = xlLineMarkers
    .ChartGroups(1).GapWidth = 69
    .ChartArea.Height = 250
    .ChartArea.Width = 550
End with

wb.Save
xl.Visible = True
xl.UserControl = True
Set ws = Nothing
Set wb = Nothing

End Sub

在窗体frm_creategannt中,用户选择两个日期,然后单击命令按钮以将查询导出到Excel并创建xlClustered图表,但是VBA显示: 运行时错误'3011'。 Microsoft Office Access数据库引擎找不到对象“ qry_creategantt.xlsx”。确保对象存在并且您...

这是VBA代码

Private Sub cmbexpqry_gantt_Click()

If IsNull(Me.cmbexpqry_gantt) Then Exit Sub
Dim wb As Object
Dim xl As Object
Dim sExcelWB As String
Dim ws As Worksheet
Dim r As Range
Dim ch As Object ''Excel.Chart
Dim mychart As ChartObject
Dim qry_creategantt As Object
Dim fullPhotoPath   As String

fullPhotoPath = Add_PlotMap(Form_frm_creategantt.cbxcreategantt.Value)
Set xl = CreateObject("excel.application")
On Error Resume Next
Kill TrailingSlash(CurrentProject.Path) & Form_frm_creategantt.cbxcreategantt.Value & "qry_creategantt.xlsx"
Err.Clear
On Error GoTo 0
sExcelWB = TrailingSlash(CurrentProject.Path) & "qry_creategantt.xlsx"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qry_creategantt.xlsx", sExcelWB, True
Set wb = xl.Workbooks.Open(sExcelWB)
Set ws = wb.Sheets("qry_creategantt.xlsx")
Set ch = ws.Shapes.AddChart.Chart
Set mychart = ws.ChartObjects("Chart 1")
ws.Shapes.AddPicture fullPhotoPath, msoFalse, msoCTrue, r.Left, r.Top, 500, 250

With ch
    .ChartType = xlBarStacked
End With

wb.Save
xl.Visible = True
xl.UserControl = True
Set ws = Nothing
Set wb = Nothing 

End Sub

此行中出现错误“ 3011”:     DoCmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcel12Xml,qry_creargantt.xlsx”,sExcelWB,True

我每行代码一一比较。 另外,我检查了每种形式的查询。

我需要修复运行时错误'3011',开始测试VBA代码以创建甘特图

我认为没有发现错误,但是我被卡住了

感谢您在错误代码中的答复,建议和努力。

2 个答案:

答案 0 :(得分:0)

如果您所指示的错误发生在DoCmd.TransferSpreadsheet行上,则该错误非常明显。

您已告知Microsoft Access运行名为qry_creategantt.xlsx的查询(以便将该数据导出到Excel),并且该查询在您的数据库中不存在。

在“查询”列表和您的代码中检查查询的拼写。在上述问题中,您将代码行重述为发生错误的位置,您对查询名称的拼写有所不同:qry_creargantt.xlsx。哪个拼写正确?那可能是你的问题。

答案 1 :(得分:0)

不可能将查询称为“ .anything”。是访问查询名称中的非法字符。

因此,请仔细检查您所引用的查询的名称。因为不是qry_creategantt.xlsx