VBA-Access:我的一些插入查询有效,其他的无效,我的最终更新查询也无效。我究竟做错了什么?

VBA-Access: Some of my insert queries work, others do not, and my final update query also does not work. What am I doing wrong?

这是代码。因为此时我真的只是想让它工作,所以一切都直接在点击事件中进行。我没有收到任何错误。它只是没有完全工作:

Private Sub cmdCreateTask_Click()
    On Error GoTo ErrHandler

    'open db and specify DAO methods
    Dim dbs As DAO.Database
    Set dbs = CurrentDb 'because the db is already open, don't need: OpenDatabase("FollowUp_Test.mdb")

    'this will eventually be accessed a different way
    Dim userId As Integer
    userId = 1

    'INSERT task and return id
    Dim qryInsertTask As String
    qryInsertTask = "INSERT INTO Task (applicant_info_id) VALUES (NULL);"
    Dim qryTaskId As String
    qryTaskId = "SELECT @@Identity"
    Dim taskId As Integer

    'INSERT applicant info row and return id
    Dim qryInsertAppInfo As String
    qryInsertAppInfo = "INSERT INTO ApplicantInfo " _
                & "(task_id, app_first_name, app_last_name, applicant_id_number, " _
                & "account_id_number, issue_id_number) VALUES " _
                & "('" & taskId & "', '" & txtAppFirstName.Value & "', '" & txtAppLastName.Value & "', " _
                & "" & txtAppId.Value & ", " & txtAcctId.Value & ", " & txtIssueId.Value & ");"
    Dim qryAppInfoId As String
    qryAppInfoId = "SELECT @@Identity"
    Dim appInfoId As Integer

    'INSERT owner row and return id
    Dim qryInsertOwner As String
    qryInsertOwner = "INSERT INTO Ownership " _
                & "(task_id, user_id, ownership_date, " _
                & "task_owned) VALUES (" & taskId & ", " & userId & ", " _
                & "#" & Format(Date, "mm/dd/yyyy") & "#, " & True & ");"
    Dim qryOwnerId As String
    qryOwnerId = "SELECT @@Identity"
    Dim ownerId As Integer

    'get values from comboboxes
    Dim actionId As Integer
    actionId = cboFolder.Value
    Dim monTypeId As Integer
    monTypeId = cboMonetaryType.Value
    'MsgBox (actionId & ", " & monTypeId)

    'INSERT schedule row and return id
    Dim qryInsertSchedule As String
    qryInsertSchedule = "INSERT INTO Schedule " _
                & "(task_id, due_date) VALUES " _
                & "(" & taskId & ",  #" & Format(txtFollowUpDt.Value, "mm/dd/yyyy") & "#);"
    Dim qryScheduleId As String
    qryScheduleId = "SELECT @@Identity"
    Dim scheduleId As Integer

    'INSERT TaskHistory row and return id
    Dim qryInsertTaskHistory As String
    qryInsertTaskHistory = "INSERT INTO TaskHistory " _
                & "(task_id, create_user_id, create_date) VALUES " _
                & "(" & taskId & ", " & userId & ", #" & Format(Date, "mm/dd/yyyy") & "#);"
    Dim qryTaskHistoryId As String
    qryTaskHistoryId = "SELECT @@Identity"
    Dim taskHistoryId As Integer

    'INSERT comment row and return id
    Dim qryInsertComment As String
    qryInsertComment = "INSERT INTO Comments " _
                & "(comment_text) VALUES ('" & txtComment.Value & "');"
    Dim qryCommentId As String
    qryCommentId = "SELECT @@Identity"
    Dim commentId As Integer

    'INSERT EventLog row and return id
    Dim qryInsertEventLog As String
    qryInsertEventLog = "INSERT INTO EventLog " _
                & "(task_id, action_id, user_id, mon_type_id, comment_id, " _
                & "event_date) VALUES (" & taskId & ", " & actionId & ", " & userId & ", " & monTypeId & ", " _
                & "" & commentId & ", #" & Format(Date, "mm/dd/yyyy") & "#);"
    Dim qryEventLogId As String
    qryEventLogId = "SELECT @@Identity"
    Dim eventLogId As Integer

    'UPDATE task with all the id's created
    Dim qryUpdateTask As String
    qryUpdateTask = "UPDATE Task SET applicant_info_id = " & appInfoId & ", " _
                & "ownership_id = " & ownerId & ", action_id = " & actionId & ", mon_type_id = " & monTypeId & ", " _
                & "schedule_id = " & scheduleId & ", event_id = " & eventLogId & ", task_history_id = " & taskHistoryId & " " _
                & "WHERE task_id = " & taskId & ";"

    'run the queries
    dbs.Execute qryInsertTask
    taskId = dbs.OpenRecordset(qryTaskId)(0)
    MsgBox (taskId)
    'MsgBox (taskId & ", " & txtAppFirstName.Value & ", " & txtAppLastName.Value & ", " _
    '            & txtAppId.Value & ", " & txtAcctId.Value & ", " & txtIssueId.Value)
    dbs.Execute qryInsertAppInfo
    appInfoId = dbs.OpenRecordset(qryAppInfoId)(0)
    'MsgBox (appInfoId)
    dbs.Execute qryInsertOwner
    ownerId = dbs.OpenRecordset(qryOwnerId)(0)
    dbs.Execute qryInsertSchedule
    scheduleId = dbs.OpenRecordset(qryScheduleId)(0)
    dbs.Execute qryInsertTaskHistory
    taskHistoryId = dbs.OpenRecordset(qryTaskHistoryId)(0)
    dbs.Execute qryInsertComment
    commentId = dbs.OpenRecordset(qryCommentId)(0)
    dbs.Execute qryInsertEventLog
    eventLogId = dbs.OpenRecordset(qryEventLogId)(0)
    MsgBox (appInfoId & ", " & ownerId & ", " & actionId & ", " & monTypeId & ", " & _ 
        & scheduleId & ", " & eventLogId & ", " & taskHistoryId & ", " & taskId)
    dbs.Execute qryUpdateTask

    'dereference and close
    dbs.Close
    Set dbs = Nothing

ExitSub:
    'rs.Close
    Exit Sub
    Set dbs = Nothing

ErrHandler:

  MsgBox "Something's wrong: " & vbCrLf & vbCrLf & "Make sure all entries are in the correct format." & vbCrLf & vbCrLf _
  & "Error", , "Validation Error"
    'dereference and close on error
    dbs.Close
    Set dbs = Nothing
    Resume ExitSub
    Resume
  End Sub

我已经包含了几个消息框,它们都显示了我保存在各种变量中的正确信息,所以我不知道为什么当我 运行 更新查询时它不起作用,至少。我对从 VBA 填充数据库还很陌生,所以我想这与我访问数据库的方式有关。我也怀疑我的约会有问题,但我不明白为什么。任何帮助,将不胜感激。

"SELECT @@Identity"

不是 Access SQL,所以用一些值替换它,或者如果字段是自动编号则完全删除它。

我终于明白了。我只需要使用 TempVars 集合。我不明白为什么这是必要的,但显然这是从插入中访问存储在标识值中的变量的方法。

我能够像这样保存变量:

TempVars("tempTaskId").Value = taskId

然后像这样在查询中访问它:

TempVars!tempTaskId.Value