Hier ist der Code. Weil ich wirklich versuche, es zu diesem Zeitpunkt zum Laufen zu bringen, liegt alles direkt im Click-Event. Ich bekomme keine Fehler. Es ist einfach nicht voll funktionsfähig:VBA-Access: Einige meiner Insert-Abfragen funktionieren, andere nicht, und meine letzte Update-Abfrage funktioniert auch nicht. Was mache ich falsch?
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
Ich habe ein paar MsgBoxes enthalten, die alle die richtigen Informationen präsentieren, was ich in den verschiedenen Variablen gespeichert haben, so dass ich weiß nicht, warum es nicht funktioniert, wenn Ich führe mindestens die Update-Abfrage aus. Ich bin ziemlich neu beim Füllen einer Datenbank von VBA, also stelle ich mir vor, dass es etwas damit zu tun hat, wie ich den Zugriff auf die Datenbank nutze. Ich vermute auch, dass meine Daten ein Problem sind, aber ich kann nicht herausfinden, warum. Jede Hilfe wäre willkommen.
Fügen Sie die 'dbFailOnError' Option, wenn Sie diese Aussagen wie diese ausführen : 'dbs.Execute qryInsertOwner, dbFailOnError' In einigen Situationen gibt diese Option Ihnen bessere Informationen, wenn eine Anweisung fehlschlägt. – HansUp
Vielen Dank, dass das darin half, eine Reihe von Problemen zu erfassen, die Änderungen in der Datenbank erforderten. –