J
j_gold
Hi,
Not sure what I am doing wrong. If there is a better way to do this, please
advise. I am using linked tables (MySQL DB). The first table contains a list
of publications, the second, is the relationship between the publications and
the author.
My sub executes inserts the publications correctly, but fails to get to the
second execute statement. Also, I want the whole transaction to fail if it
doesn't finish the second insert, but when I view the database, the
publication is entered, even though the second transaction failed.
Thanks,
J
Private Sub addPublication(pubTitle As String, publisher As String, urlLINK
As String, _
pubType As String, pubStatus As String, pubAuthor As
String, _
pubPages As String, pubVolume As String, publishDate
As Date, _
pubPresentedAt As String)
Dim wrk As DAO.Workspace: Set wrk = DBEngine(0)
Dim dbP As DAO.Database: Set dbP = wrk(0)
Dim dbPB As DAO.Database: Set dbPB = wrk(0)
Dim rs As DAO.Recordset
Dim sqlQuery As String
Dim newPubID As String
Debug.Assert Not (wrk Is Nothing)
wrk.BeginTrans
On Error GoTo trans_Err
dbP.Execute "INSERT INTO Publications (pubTitle, publisher, urlLINK," _
& "pubType, pubStatus, pubAuthor, pubPages, pubVolume, " _
& "publishDate, pubPresentedAt) " _
& "VALUES ('" & Replace(pubTitle, "'", "''") & "', " _
& publisher & ", " & urlLINK & ", " _
& pubType & ", " & pubStatus & ", " & pubAuthor & ", " _
& pubPages & ", " & pubVolume & ", " _
& Format(publishDate, "\#yyyy\-mm\-dd\#") & ", " & pubPresentedAt &
");", dbFailOnError
sqlQuery = "SELECT pubID FROM Publications where pubTitle = '" + pubTitle +
"'"
Set rs = dbP.OpenRecordset(sqlQuery, dbOpenSnapshot)
If rs.RecordCount = 1 Then
newPubID = rs.Fields("pubID").value
End If
rs.Close
dbPB.Execute "INSERT INTO PublishedBy (personID, pubID) VALUES (" + pKey +
",'" + newPubID + "');"
'Commit the transaction
wrk.CommitTrans dbForceOSFlush
trans_Exit:
'Clean up
wrk.Close
Set dbP = Nothing
Set dbPB = Nothing
Set wrk = Nothing
Exit Sub
trans_Err:
'Roll back the transaction
wrk.Rollback
Resume trans_Exit
End Sub
Not sure what I am doing wrong. If there is a better way to do this, please
advise. I am using linked tables (MySQL DB). The first table contains a list
of publications, the second, is the relationship between the publications and
the author.
My sub executes inserts the publications correctly, but fails to get to the
second execute statement. Also, I want the whole transaction to fail if it
doesn't finish the second insert, but when I view the database, the
publication is entered, even though the second transaction failed.
Thanks,
J
Private Sub addPublication(pubTitle As String, publisher As String, urlLINK
As String, _
pubType As String, pubStatus As String, pubAuthor As
String, _
pubPages As String, pubVolume As String, publishDate
As Date, _
pubPresentedAt As String)
Dim wrk As DAO.Workspace: Set wrk = DBEngine(0)
Dim dbP As DAO.Database: Set dbP = wrk(0)
Dim dbPB As DAO.Database: Set dbPB = wrk(0)
Dim rs As DAO.Recordset
Dim sqlQuery As String
Dim newPubID As String
Debug.Assert Not (wrk Is Nothing)
wrk.BeginTrans
On Error GoTo trans_Err
dbP.Execute "INSERT INTO Publications (pubTitle, publisher, urlLINK," _
& "pubType, pubStatus, pubAuthor, pubPages, pubVolume, " _
& "publishDate, pubPresentedAt) " _
& "VALUES ('" & Replace(pubTitle, "'", "''") & "', " _
& publisher & ", " & urlLINK & ", " _
& pubType & ", " & pubStatus & ", " & pubAuthor & ", " _
& pubPages & ", " & pubVolume & ", " _
& Format(publishDate, "\#yyyy\-mm\-dd\#") & ", " & pubPresentedAt &
");", dbFailOnError
sqlQuery = "SELECT pubID FROM Publications where pubTitle = '" + pubTitle +
"'"
Set rs = dbP.OpenRecordset(sqlQuery, dbOpenSnapshot)
If rs.RecordCount = 1 Then
newPubID = rs.Fields("pubID").value
End If
rs.Close
dbPB.Execute "INSERT INTO PublishedBy (personID, pubID) VALUES (" + pKey +
",'" + newPubID + "');"
'Commit the transaction
wrk.CommitTrans dbForceOSFlush
trans_Exit:
'Clean up
wrk.Close
Set dbP = Nothing
Set dbPB = Nothing
Set wrk = Nothing
Exit Sub
trans_Err:
'Roll back the transaction
wrk.Rollback
Resume trans_Exit
End Sub