Share via

Problem to query

Scott Huang 3,511 Reputation points
2021-05-07T02:35:51.513+00:00

Hi,
Can you help to error "Invalid operation" due to last line below?

Option Explicit
Private mDbEngine As DAO.DBEngine
Private mCurrDB As DAO.Database
...
Do While True
RowID = RowID + 1
If Trim(ActiveSheet.Cells(RowID, 1).Value) = "" Then
Exit Do
End If
s0 = "insert into t1 values (" & CStr(RowID) & ",'" & Trim(ActiveSheet.Cells(RowID, 1).Value) & "','" & Trim(ActiveSheet.Cells(RowID, 2).Value) & "','" & _
Trim(ActiveSheet.Cells(RowID, 3).Value) & "','" & Trim(ActiveSheet.Cells(RowID, 4).Value) & "','" & Trim(ActiveSheet.Cells(RowID, 5).Value) & _
"','" & Trim(ActiveSheet.Cells(RowID, 6).Value) & "','" & Trim(ActiveSheet.Cells(RowID, 7).Value) & "','" & _
Trim(ActiveSheet.Cells(RowID, 8).Value) & "','" & Trim(ActiveSheet.Cells(RowID, 9).Value) & "','" & Trim(ActiveSheet.Cells(RowID, 10).Value) & "');"

    Set rs = mCurrDB.openrecordset(s0)  
Microsoft 365 and Office | Access | Development
0 comments No comments

Answer accepted by question author

DBG 11,711 Reputation points Volunteer Moderator
2021-05-07T04:48:26.85+00:00

You can't open a recordset using an action query. Instead, you can use the Execute method.

mCurrDB.Execute s0, dbFailOnError

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Scott Huang 3,511 Reputation points
    2021-05-07T06:46:07.357+00:00

    There is still error. Please see error like
    94665-1m.png
    due to last line below

    Do While True    
        RowID = RowID + 1    
        If Trim(ActiveSheet.Cells(RowID, 1).Value) = "" Then    
            Exit Do    
        End If    
        s0 = "insert into [" & ActiveSheet.Name & "] values (" & CStr(RowID) & ",'" & Trim(ActiveSheet.Cells(RowID, 1).Value) & "','" & Trim(ActiveSheet.Cells(RowID, 2).Value) & "','" & _    
            Trim(ActiveSheet.Cells(RowID, 3).Value) & "','" & Trim(ActiveSheet.Cells(RowID, 4).Value) & "','" & Trim(ActiveSheet.Cells(RowID, 5).Value) & _    
            "','" & Trim(ActiveSheet.Cells(RowID, 6).Value) & "','" & Trim(ActiveSheet.Cells(RowID, 7).Value) & "','" & _    
            Trim(ActiveSheet.Cells(RowID, 8).Value) & "','" & Trim(ActiveSheet.Cells(RowID, 9).Value) & "','" & Trim(ActiveSheet.Cells(RowID, 10).Value) & "');"    
        
        mCurrDB.Execute s0, dbFailOnError    
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.