我有两(3)个SELECT查询,这些查询链接到excel文件,并且需要定期更新(每周一次)。
我正在尝试使用VBA在访问中创建一个模块,该模块可以做两件事:
更新一些要选择的参数,这些参数定义为可以手动更新的变量:CurrentWeek,RetailWeek,CurrentYear和Current Date
更新后它将运行查询,因此我可以刷新excel文件中的数据。
我尝试了几件事,但到目前为止还没有成功,我认为最接近的是:
私有子DevicePerformanceDashboard()
Dim CurrentWeek, RetailWeek, CurrentYear As Long
Dim CurrentDate As Date
Dim qRP, qPP, qMV As DAO.QueryDef
Dim dbs As DAO.Database
Set dbs = CurrentDb()
CurrentYear = 2018
CurrentWeek = 31
RetailWeek = CurrentWeek - 8
CurrentDate = TimeValue("2018-06-01 00:00:00")
Set qRP = CurrentDb.QueryDefs("DPDashboardRetail")
Set qPP = CurrentDb.QueryDefs("DPDashboardPricePrediction")
Set qMV = CurrentDb.QueryDefs("DPDashboardVolume")
qRP.Parameters("YearNb").Value = CurrentYear
qRP.Parameters("WeekNb").Value = ">" & RetailWeek
qPP.Parameters("WeekNb").Value = ">" & CurrentWeek
qPP.Parameters("PreWeekNb").Value = CurrentWeek
qPP.Parameters("PreYearNb").Value = CurrentYear
qMV.Parameters("VolumeAndValueDate").Value = CurrentDate
DoCmd.OpenQuery "DPDashboardRetail"
DoCmd.OpenQuery "DPDashboardPricePrediction"
DoCmd.OpenQuery "DPDashboardVolume"
结束子
当前我遇到一个错误,提示Object required
,但是我尝试了其他方法,并且在不同的时间遇到了不同的错误。
更新:现在错误在此行:
qRP.Parameters("YearNb").Value = CurrentYear
Item not found in this collection
关于我在做什么错的任何想法吗?
答案 0 :(得分:2)
许多错误:
Dim CurrentWeek As Long
Dim RetailWeek As Long
Dim CurrentYear As Long
Dim CurrentDate As Date
Dim qRP As DAO.QueryDef
Dim qPP As DAO.QueryDef
Dim qMV As DAO.QueryDef
Dim dbs As DAO.Database
Set dbs = CurrentDb
CurrentYear = 2018
CurrentWeek = 31
RetailWeek = CurrentWeek - 8
CurrentDate = DateSerial(2018, 6, 1)
Set qRP = CurrentDb.QueryDefs("DPDashboardRetail")
Set qPP = CurrentDb.QueryDefs("DPDashboardPricePrediction")
Set qMV = CurrentDb.QueryDefs("DPDashboardVolume")
qRP.Parameters("YearNb").Value = CurrentYear
' Cannot do:
' qRP.Parameters("WeekNb").Value = ">" & RetailWeek
' Cannot do:
' qPP.Parameters("WeekNb").Value = ">" & CurrentWeek
qPP.Parameters("PreWeekNb").Value = CurrentWeek
qPP.Parameters("PreYearNb").Value = CurrentYear
qMV.Parameters("VolumeAndValueDate").Value = CurrentDate
qRP.Execute
qPP.Execute
qMV.Execute
在此收藏夹中找不到物品
这表示SQL中缺少或拼写错误的字段/参数。
答案 1 :(得分:1)
Set
是仅应与对象一起使用的关键字,因此,需要对象错误。要将值分配给变量,只需VariableName = SomeValue
就足够了,仅在分配对象时使用Set variableName = SomeObject
。
此外,qdf.Parameters
与qdf.Execute
一起执行查询。 DoCmd.OpenQuery
与DoCmd.SetParameter
一起设置参数。
在使用DoCmd.SetParameter
时,由DoCmd.OpenQuery
创建的参数集合将被清除,因此需要对代码进行一些重组。
Option Compare Database
Option Explicit
Private Sub DevicePerformanceDashboard()
Dim CurrentWeek, RetailWeek, CurrentYear As Long
Dim CurrentDate As Date
Dim dbs As DAO.Database
Set dbs = CurrentDb()
CurrentYear = 2018
CurrentWeek = 31
RetailWeek = CurrentWeek - 8
CurrentDate = TimeValue("2018-06-01 00:00:00")
DoCmd.SetParameter "YearNb", CurrentYear
DoCmd.SetParameter "WeekNb", ">" & RetailWeek
DoCmd.OpenQuery "DPDashboardRetail"
DoCmd.SetParameter "WeekNb", ">" & CurrentWeek
DoCmd.SetParameter "PreWeekNb", CurrentWeek
DoCmd.SetParameter "PreYearNb", CurrentYear
DoCmd.OpenQuery "DPDashboardPricePrediction"
DoCmd.SetParameter "VolumeAndValueDate", CurrentDate
DoCmd.OpenQuery "DPDashboardVolume"
End Sub