所以我一直在这里搜索,但似乎无法找到使我的代码有效的答案。我知道我只是遗漏了一些东西,但无法弄清楚。
我正在运行MS Access 2013并尝试运行以下SQL"更新"在VBA但继续得到
运行时错误3464:数据类型不匹配
Function ChangeTaskRank(NewRank As Integer, PriorityLevel As Integer, UserGroup As String)
Dim db As DAO.Database
Dim tableDF As DAO.TableDef
Dim sqlString As String
Dim currentRank As Integer
Dim nextRank As Integer
Set db = CurrentDb()
Set tableDF = db.TableDefs("Task_List")
SelectTaskQueryDB PriorityLevel, UserGroup
currentRank = TempVars!tmp_Task_MaxRank
Do While (currentRank >= NewRank)
nextRank = currentRank + 1
db.Execute "UPDATE Task_List " & _
"SET [Task_Rank]= " & nextRank & " " & _
"WHERE (([Task_Rank] = " & currentRank & ") AND ([Task_Priority] = " & PriorityLevel & ") AND ([Task_UserGroup] = '" & UserGroup & "'));"
'Update
'SET Task_Rank = (currentRank + 1)
'WHERE (([Task_Priority] = PriorityLevel) AND ([Task_UserGroup] = UserGroup));
currentRank = currentRank - 1
Loop
End Function
我正在尝试使用代码运行一个循环,它将在添加新任务时更改任务的等级(IE:您有任务等级1到5并在3处添加新任务,代码将将任务等级3,4,5更改为4,5,6。)
数据库信息:
Table name: Task_List
Field Name: Task_Rank (Integer)
过滤器:
[Task_Rank] = currentRank (Integer)
[Task_Priority] = PriorityLevel (Integer)
[Task_UserGroup] = UserGroup (String)
任何帮助都会很棒,因为这让我疯狂大声笑。提前谢谢。
答案 0 :(得分:2)
考虑带有条件PARAMETERS NewRankParam LONG, PriorityLevelParam LONG, UserGroupParam TEXT;
UPDATE Task_List t
SET [Task_Rank]= IIF(t.[Task_Rank] >= NewRankParam, t.[Task_Rank] + 1, t.[Task_Rank])
WHERE ((t.[Task_Priority] = PriorityLevelParam) AND (t.[Task_UserGroup] = UserGroupParam));
的参数化SQL查询,以避免任何循环,引用机箱和字符串连接。
SQL (另存为已保存的查询)
Function ChangeTaskRank(NewRank As Integer, PriorityLevel As Integer, UserGroup As String)
Dim db As DAO.Database
Dim qdef As QueryDef
Set db = CurrentDb
Set qdef = db.QueryDefs("mySavedQuery")
qdef!NewRankParam = NewRank
qdef!PriorityLevelParam = PriorityLevel
qdef!UserGroupParam = UserGroup
qdef.Execute dbFailOnError
Set db = Nothing
Set qdef = Nothing
End Function
<强> VBA 强>
{{1}}