通过cmd提示运行vba脚本获取错误

时间:2014-04-25 17:27:41

标签: vbscript

嗨,任何人请帮助我..

在运行vba脚本时我收到错误 - 对象DoCmd需要创建。

我的脚本如下:

ExecuteInsert
Sub ExecuteInsert()
    Dim sheetPath
    Dim dbs, DbFullName, acc
    Set acc = CreateObject("Access.Application")

    DbFullName = "D:\G\Diamond\FINAL MS-Access\Demo\MS-Access project.accdb"
    Set dbs = acc.DBEngine.OpenDatabase(DbFullName, False, False)

    dbs.Execute "Delete from TempRoadMap"

    sheetPath = "C:\Users\270784\Desktop\CSPRV scheduled work - 2014 through 1-26-14.xlsx"

    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel97, "TempRoadMap", sheetPath, True

    MsgBox "Imported Sheet1 from " & sheetPath & " Successfully!"

    dbs.Execute "Delete from RoadMap"
    dbs.Execute "INSERT INTO [RoadMap] ( Release_Name,SPRF,SPRF_CC,Estimate_Type,PV_Work_ID,SPRF_Name,Estimate_Name,Project_Phase,CSPRV_Status,Scheduling_Status,Impact_Type,Onshore_Staffing_Restriction,Applications,Total_Appl_Estimate,Total_CQA_Estimate,Estimate_Total,Requested_Release,Item_Type,Path) SELECT [TempRoadMap.Release Name], [TempRoadMap.SPRF], [TempRoadMap.Estimate (SPRF-CC)],[TempRoadMap.Estimate Type],[TempRoadMap.PV Work ID],[TempRoadMap.SPRF Name],[TempRoadMap.Estimate Name],[TempRoadMap.Project Phase],[TempRoadMap.CSPRV Status],[TempRoadMap.Scheduling Status],[TempRoadMap.Impact Type],[TempRoadMap.Onshore Staffing Restriction],[TempRoadMap.Applications],[TempRoadMap.Total Appl Estimate],[TempRoadMap.Total CQA Estimate],[TempRoadMap.Estimate Total],[TempRoadMap.Requested Release],[TempRoadMap.Item Type],[TempRoadMap.Path] FROM [TempRoadMap] "
    dbs.Close

    MsgBox "Done"
End Sub

2 个答案:

答案 0 :(得分:0)

假设通过“...通过cmd提示符...”意味着暗示您将此脚本作为VBScript文件的一部分运行,以下是您要进行的更改:

  1. DoCmd是Access.Application的子级,它只能在Access数据库的范围内全局引用。要在VBScript中引用它,您必须显式使用Access.Application实例,该实例具有变量acc。像这样修改DoCmd行的开头:

    acc.DoCmd.TransferSpreadsheet

  2. VBScript无法识别acImportacSpreadsheetTypeExcel97等访问常量,因此您必须将其替换为实际值。通过msdn快速浏览一下,显示acImport0且acSpreadsheetTypeExcel97不存在,但acSpreadsheetTypeExcel8(Excel 97格式)为8。所以现在DoCmd行看起来像:

    acc.DoCmd.TransferSpreadsheet 0, 8, "TempRoadMap", sheetPath, True

  3. VBScript没有MsgBox - 这只能通过VBA获得。相反,您可以使用Wscript.Echo。例如,最后一行是:WScript.Echo "Done"
  4. 如果您在进行这些更改后仍有问题,请通过以下评论告诉我。

答案 1 :(得分:0)

VBScript肯定有msgbox和echo不是vbscript的一部分,而是Windows Scripting Host的一部分。