问题
I'm not sure if my title is 100% accurate, but I think that is the problem in my code. I got this code working, but when I made changes to other portions of my code and it stopped working. Here's the full SQL:
UPDATE CFRRR
SET assignedto = " & GetNextAssignee("program", "Language", "username") & ",
assignedby = '"
& Forms!Supervisor!NavigationSubform!assignedby.Value
& "', Dateassigned = #"
& Now & "#, actiondate = #"
& Now & "#, Workername = '"
& DLookup("username", "attendance", "userID = "
& GetNextAssignee("program", "Language", "username"))
& "', WorkerID = " & DLookup("userID", "attendance", "userID = "
& GetNextAssignee("program", "Language", "username"))
& " WHERE CFRRRID = "
& rs!CFRRRID
The error I am getting is here:
Workername = '"
& DLookup("username", "attendance", "userID = "
& GetNextAssignee("program", "Language", "username"))
This is the output I am getting:
UPDATE CFRRR SET assignedto = 6,
assignedby = '33',
Dateassigned = #5/17/2015 7:46:40 PM#,
actiondate = #5/17/2015 7:46:40 PM#,
Workername = 'Valentino',
WorkerID = 7 WHERE CFRRRID = 40
This is the output I should be getting:
UPDATE CFRRR SET assignedto = 6,
assignedby = '33',
Dateassigned = #5/17/2015 7:46:40 PM#,
actiondate = #5/17/2015 7:46:40 PM#,
Workername = 'John',
WorkerID = 6 WHERE CFRRRID = 40
And here:
WorkerID = "
& DLookup("userID", "attendance", "userID = "
& GetNextAssignee("program", "Language", "username"))
Here is the GetNextAssignee code for reference:
Public Function GetNextAssignee(program As String,
language As String,
username As String) As Long
Dim db As dao.Database
Dim rs As dao.Recordset
Dim strSQL As String
Set db = CurrentDb
strSQL = "SELECT TOP 1
userID
FROM attendance as a,
CFRRR WHERE a.Status = 'Available'
AND a.Programs LIKE CFRRR.program
AND a.Language = CFRRR.language
ORDER BY TS ASC, userID, CFRRRID"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
If Not rs.BOF And Not rs.EOF Then
strSQL = "UPDATE attendance
SET TS= " & DMax("[TS]", "attendance") + 1
& " WHERE [userID]=" & rs!userID
& " AND [Status]=""Available"""
db.Execute strSQL, dbFailOnError
GetNextAssignee = rs!userID
Else
GetNextAssignee = 0
End If
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Function
回答1:
Change your code as below
Dim i as long
and inside your while loop of AssignNullProjects
change the query like this
If Not rs.BOF And Not rs.EOF Then
While Not rs.EOF
i = GetNextAssignee("program", "Language", "username")
strSQL = "UPDATE CFRRR SET assignedto = " & i & ",
assignedby = '" & Forms!Supervisor!NavigationSubform!assignedby.Value
& "', Dateassigned = #" & Now & "#, actiondate = #"
& Now & "#, Workername = '"
& _DLookup("username", "attendance", "userID = " & i)
& "', WorkerID = " & i & " WHERE CFRRRID = " & rs!CFRRRID
Debug.Print strSQL
db.Execute strSQL, dbFailOnError
rs.MoveNext
Wend
End If
来源:https://stackoverflow.com/questions/30292567/string-error-in-sql-ms-access