【问题标题】: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?VBA-Access:我的一些插入查询有效,其他查询无效,我的最终更新查询也无效。我究竟做错了什么?
【发布时间】:2016-04-18 14:12:31
【问题描述】:

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

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

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

【问题讨论】:

  • 在执行这些语句时包含dbFailOnError 选项,如下所示:dbs.Execute qryInsertOwner, dbFailOnError 在某些情况下,该选项会在语句失败时为您提供更好的信息。
  • 谢谢,把它放在那里确实有助于发现一些需要对数据库进行更改的问题。

标签: vba sql-update sql-insert dao ms-access-2013


【解决方案1】:
"SELECT @@Identity"

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

【讨论】:

  • 我不确定我是否理解。我同意我现在的问题与@@Identity 有关。问题是,我可以看到它正在获取生成的各种自动编号 ID 的值并将它们存储在我为它们创建的变量中。然后,当我尝试在一些插入和最终更新中使用这些变量时,就好像它们不存在一样。
【解决方案2】:

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

我能够像这样保存变量:

TempVars("tempTaskId").Value = taskId

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

TempVars!tempTaskId.Value

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-03
    • 2020-08-11
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 2020-02-25
    • 2017-03-16
    • 1970-01-01
    相关资源
    最近更新 更多