Question : 1004: Application-defined or object-defined error on QueryTable refresh

Hi All

I'm trying to execute the following VBA in Excel:
   
Set qT = Worksheets("Workspace").QueryTables.Add(constring, Worksheets("Workspace").Range("B2"), SQLstr)
qT.BackgroundQuery = False
qT.Refresh
Worksheets("Workspace").Columns("B:B").Clear
qT.CommandText = "select identityVal from identities where identityName = 'feedid'"
qT.Refresh

Basically, the constring is a SQL insert statement that does 2 things:
1. Inserts data into a feed table
2. Updates identities table with the @@identity returned by the insert.

If I comment out the last 3 lines, as well as the qT.BackgroundQuery = False line, it works fine (data is inserted into the database and identities table is updated). However, I would like to reuse the QueryTable with another query. If I just remove the qT.BackgroundQuery = False line, I get a "1004: The operation cannot be done because the data is refreshing in the background." error.

Please help - this is work-related and urgent.

Answer : 1004: Application-defined or object-defined error on QueryTable refresh

Hi,
You might find it easier to do the INSERT using ADO (I'm surprised a Querytable will let you do that to be honest) - something like:

    Dim cn As adodb.Connection, strQuery As String
    Set cn = New adodb.Connection
    With cn
        .Provider = "sqloledb"
        .ConnectionString = "Data Source=DSN_name_here;Initial Catalog=db_name;User ID=whatever;"
        .Open
    End With
    cn.Execute constring
    cn.Close
    set cn = nothing

and then just create your QueryTable using the second SQL string.

HTH
Rory
Random Solutions  
 
programming4us programming4us