嗨,任何人请帮助我..
在运行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
答案 0 :(得分:0)
假设通过“...通过cmd提示符...”意味着暗示您将此脚本作为VBScript文件的一部分运行,以下是您要进行的更改:
DoCmd是Access.Application的子级,它只能在Access数据库的范围内全局引用。要在VBScript中引用它,您必须显式使用Access.Application实例,该实例具有变量acc
。像这样修改DoCmd行的开头:
acc.DoCmd.TransferSpreadsheet
VBScript无法识别acImport
或acSpreadsheetTypeExcel97
等访问常量,因此您必须将其替换为实际值。通过msdn快速浏览一下,显示acImport为0
且acSpreadsheetTypeExcel97不存在,但acSpreadsheetTypeExcel8(Excel 97格式)为8
。所以现在DoCmd行看起来像:
acc.DoCmd.TransferSpreadsheet 0, 8, "TempRoadMap", sheetPath, True
MsgBox
- 这只能通过VBA获得。相反,您可以使用Wscript.Echo
。例如,最后一行是:WScript.Echo "Done"
如果您在进行这些更改后仍有问题,请通过以下评论告诉我。
答案 1 :(得分:0)
VBScript肯定有msgbox和echo不是vbscript的一部分,而是Windows Scripting Host的一部分。