无法根据条件将表复制到Excel

时间:2015-11-11 16:11:33

标签: sql excel vba access-vba

我在尝试运行时收到错误运行时错误'424':需要对象

Function CreateDailyReadOnlyCVG()

Dim sql, strStatusReport, strDailyCVG, strOutputFileName As String

strOutputFileName = "mit-msp01\cs\CUS Supply Chain\All Division Reporting\Field Retrievals\Status Reports\CVG" & _
Format(Date, "yyyyMMdd") & ".xlsx"

    If tRetrieval.[Retrieval_Status] = "A" And tRetrieval.[BU] = "CVG" Then _
      sql = "INSERT * FROM tRetrieval_Status_Report_CVG INSERT INTO tDaily_Report_CVG"
      DoCmd.RunSQL sql
      DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "Daily Report", " & strOutputFileName & ", True
      SetAttr " & strOutputFileName & ", vbReadOnly

End Function

它编译得很好,但后来我运行它并得到错误。此外,我觉得这不会运行。

1 个答案:

答案 0 :(得分:0)

想出来:

Sub CreateDailyReadOnlyCVG()

Dim sql, strOutputFileName As String
Dim intCounter As Integer

strOutputFileName = "J:\CUS Supply Chain\All Division Reporting\Field Retrievals\Status Reports\CVG\Retrieval Status Report CVG" & _
Format(Date, "yyyyMMdd") & ".xls"


sql = "INSERT INTO tDaily_Report_CVG SELECT * FROM tRetrieval_Status_Report_CVG;"

intCounter = DCount("[Retrieval_ID]", "tRetrieval", "[BU] = 'CVG' AND [Retrieval_Status] = 'A'")

    If intCounter > 0 Then
        DoCmd.RunSQL sql
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "tDaily_Report_CVG", _
        " & strOutputFileName & ", True
        SetAttr " & strOutputFileName & ", vbReadOnly
    End If

End Sub