SQL MS Access中的字符串错误

时间:2015-05-17 21:55:44

标签: sql access-vba ms-access-2010

我不确定我的标题是否100%准确,但我认为这是我的代码中的问题。我得到了这段代码,但是当我对代码的其他部分进行更改并且它停止工作时。这是完整的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

我得到的错误在这里:

Workername = '" 
& DLookup("username", "attendance", "userID = " 
& GetNextAssignee("program", "Language", "username"))

这是我得到的输出:

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

这是我应该得到的输出:

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

在这里:

WorkerID = "
& DLookup("userID", "attendance", "userID = "
& GetNextAssignee("program", "Language", "username"))

以下是GetNextAssignee代码供参考:

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 个答案:

答案 0 :(得分:1)

更改您的代码如下

Dim i as long

并在AssignNullProjects的while循环内部更改此查询

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