【发布时间】: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