0

I'm using VBA 7 with MS Word to change field values in a SQLite database. I need to read every record in a table from one database, change it value and update some record in another table from second database. When I'm trying to update the second table, I get the error: 'only one SQL statement allowed'. I'd appreciate any help.

Sub IterateThruDB1andUpdateDB2()
    Dim mCounter As Long
    Dim mEntry As String
    Dim mTranslation As String
    Dim qExec As String
    
    Set cnn1 = New ADODB.Connection
    cnn1.ConnectionString = "DSN=SomeDSN1;"
    cnn1.Open
    Set cnn2 = New ADODB.Connection
    cnn2.ConnectionString = "DSN=SomeDSN2;"
    cnn2.Open
    Dim rs1 As ADODB.Recordset
    Dim rs2 As ADODB.Recordset

    mCounter = 0
    If cnn1.State = adStateOpen Then
        qExec = "SELECT * FROM someTable1"
        Set rs1 = cnn1.Execute(qExec)
        Do Until rs1.EOF
            mWordId = rs1.Fields("_id")
            mWord = rs1.Fields("word")
            mTranslation = rs1.Fields("translation") & "Some text"
            qExec = "UPDATE someTable2 SET translation='" & mTranslation & "' WHERE _id='" & mWordId & "'"
            Set rs2 = cnn2.Execute(qExec)
            If rs2.EOF Then
                mCounterNotFound = mCounterNotFound + 1
                resp = MsgBox("Couldn't update! [" & mWordId & "]")
                If resp = vbCancel Or resp = vbNo Then
                    rs2.Close
                    Exit Do
                End If
            Else
                mCounter = mCounter + 1
            End If
            rs2.Close
            rs1.MoveNext
        Loop
    End If
    rs1.Close
    cnn1.Close
    cnn2.Close
    Set rs1 = Nothing
    Set rs2 = Nothing
    Set cnn1 = Nothing
    Set cnn2 = Nothing
End Sub

If I change Update statement to Select, it works, so it actually allows two statements, but not Update or Insert. The second set is needed to check whether the record in the second table was successfully updated, so it needs to be closed in each cycle. The second set can be removed, but the error still occurs.

3
  • 2
    An update statement doesn't return a recordset? Commented Apr 8, 2024 at 15:40
  • stackoverflow.com/a/47446359/478884 shows how to use a parameterized query to run an update. If you want to know how many records were updated you can do this: stackoverflow.com/a/3269331/478884 Commented Apr 8, 2024 at 15:46
  • 1
    Thanks Alex, this was the reason for the error, although I no longer see your comment. Thanks Tim, that caused the second error of closed record set, when the execution came to that point. By removing it, I was able to complete the operation. Commented Apr 8, 2024 at 18:18

1 Answer 1

0

There were two problems in the code:

  1. As Alex suggested, I replaced "UPDATE someTable2 SET translation='" & mTranslation & "' WHERE _id='" & mWordId & "'" with "UPDATE someTable2 SET translation='" & Replace(mTranslation, "'", "''") & "' WHERE _id='" & mWordId & "'" and the first problem was gone.
  2. As Tim noted, executing the update query returns a closed recordset, so I removed it and it allowed to finish the job.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.