Link to home
Start Free TrialLog in
Avatar of João serras-pereira
João serras-pereiraFlag for Portugal

asked on

run aoppend queries

How do, in VBA, run a set of predefined append queries?
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

You can use:

DoCmd.OpenQuery

or

 CurrentDB.Execute

The later is preferable because you can use the optional argument dbFailOnError and have error handling.

There is also:

DoCmd.RunSQL

  which let's you execute a SQL statement directly.
Avatar of João serras-pereira

ASKER

is it currentDB.execute "queryName", vbdvfailonerror?
this is because I am getting an error: "object does not support this type of method"

User generated image
...
the related source code is:

    CurrentDb.Execute "DELETE FROM zAlertas", dbFailOnError
    
    Me.progressBar_92 = " Passo 1 / 8 "
    CurrentDb.Execute "q32_zAlertas_p01" ', dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 1
    
    Me.progressBar_92 = " Passo 2 / 8 "
    CurrentDb.Execute "q32_zAlertas_p02", dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 2
    
    Me.progressBar_92 = " Passo 3 / 8 "
    CurrentDb.Execute "q32_zAlertas_p03", dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 3
    
    Me.progressBar_92 = " Passo 4 / 8 "
    CurrentDb.Execute "q32_zAlertas_p04", dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 4
    
    Me.progressBar_92 = " Passo 5 / 8 "
    CurrentDb.Execute "q32_zAlertas_p05", dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 5
    
    Me.progressBar_92 = " Passo 6 / 8 "
    CurrentDb.Execute "q32_zAlertas_p06", dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 6
    
    Me.progressBar_92 = " Passo 7 / 8 "
    CurrentDb.Execute "q32_zAlertas_p07", dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 7
    
    Me.progressBar_92 = " Passo 8 / 8 "
    CurrentDb.Execute "q32_zAlertas_p07", dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 8

Open in new window

all queries have the adequate name. You'll notice that I commented "dbfailonerror for the 1st query, just to check. The weird part is that the final table look alright...
Sorry should have given you some examples, but your syntax is correct.

This:

CurrentDb.Execute "DELETE FROM zAlertas", dbFailOnError

Should be:

CurrentDb.Execute "DELETE * FROM zAlertas", dbFailOnError

The queries you are trying to run, do they execute on their own?   Do they have references to form controls in them or other parameters?  Please post the SQL here for the first query q32_zAlertas_p01

Jim.



Hi Jum,

1. The queries run OK by themselves but yield the usual warnings asking permission to modify table."

1. the SQL
INSERT INTO zAlertas ( pk_patron_id, user_type, email, full_name, cellphone, userCreated, tipoAlerta )
SELECT q32_01_alertas_01_RegistoUtente_02.pk_patron_id, q32_01_alertas_01_RegistoUtente_02.user_type, q32_01_alertas_01_RegistoUtente_02.email, q32_01_alertas_01_RegistoUtente_02.full_name, q32_01_alertas_01_RegistoUtente_02.cellphone, q32_01_alertas_01_RegistoUtente_02.userCreated, q32_01_alertas_01_RegistoUtente_02.tipoAlerta
FROM q32_01_alertas_01_RegistoUtente_02;


3. The Table

User generated image

Thanks  for caring!!
It all looks fine.

Put a STOP at the top of the procedure, execute it, then step through with F8 and see which statement is actually causing the error.

Jim.
Hi. Already did it... It's ALL of the currentDB.execute yield the error but, somehow, they are working. The table is cleared and then records are added... It must be something related to the table itself but not to de queries. Anyway, on standalone ther really work!
Could you please try and doing it this way:

Dim strSQL as string

strSQL = "INSERT INTO zAlertas ( pk_patron_id, user_type, email, full_name, cellphone, userCreated, tipoAlerta )
SELECT q32_01_alertas_01_RegistoUtente_02.pk_patron_id, q32_01_alertas_01_RegistoUtente_02.user_type, q32_01_alertas_01_RegistoUtente_02.email, q32_01_alertas_01_RegistoUtente_02.full_name, q32_01_alertas_01_RegistoUtente_02.cellphone, q32_01_alertas_01_RegistoUtente_02.userCreated, q32_01_alertas_01_RegistoUtente_02.tipoAlerta
FROM q32_01_alertas_01_RegistoUtente_02; "

CurrentDB.Execute strSQL, dbFailOnError

If that still fails with the same error, then modify to:

Dim db as DAO.Database


Set db = CurrentDB()
strSQL = "INSERT INTO zAlertas ( pk_patron_id, user_type, email, full_name, cellphone, userCreated, tipoAlerta )
SELECT q32_01_alertas_01_RegistoUtente_02.pk_patron_id, q32_01_alertas_01_RegistoUtente_02.user_type, q32_01_alertas_01_RegistoUtente_02.email, q32_01_alertas_01_RegistoUtente_02.full_name, q32_01_alertas_01_RegistoUtente_02.cellphone, q32_01_alertas_01_RegistoUtente_02.userCreated, q32_01_alertas_01_RegistoUtente_02.tipoAlerta
FROM q32_01_alertas_01_RegistoUtente_02; "

db.Execute strSQL, dbFailOnError

 I'm really curious now.

Jim.
will do!
The first option does not yield any error...
Please let me know what was the modification because I do have 8 queries extremely alike.....
back to square 1 - first option does not work any longer... sigh. The problem must be elsewhere as the "    CurrentDb.Execute "DELETE * FROM zAlertas", dbFailOnError" yields exactly the same error
SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yeap!!!! well... I have trapped the specific error and pass it through... but I am scratching my head!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes. When I run directly they run OK. They yield the usual warnings demanding authorization to ass records, but that's it... The strangest thing is that the delete also yields the same error...

the current full code  is:

Private Sub btn_constroiEventos_Click()


    Dim strSQL As String

On Error GoTo errorTrap

    Call logMe("frm_importarDados btn_constroiEventos_Click", "start")
    
    Me.lbl_92.Visible = True
    Me.box_92.Visible = True
    Me.progressBar_92.Visible = True
    Me.progressBar_92.Width = 0
    
'   call completaDiarioEventos()

    CurrentDb.Execute "DELETE * FROM zAlertas", dbFailOnError
    Me.progressBar_92 = " Passo 1 / 8     "
    strSQL = "INSERT INTO zAlertas ( pk_patron_id, user_type, email, full_name, cellphone, userCreated, tipoAlerta )"
    strSQL = strSQL & " SELECT q32_01_alertas_01_RegistoUtente_02.pk_patron_id, q32_01_alertas_01_RegistoUtente_02.user_type, q32_01_alertas_01_RegistoUtente_02.email, "
    strSQL = strSQL & "q32_01_alertas_01_RegistoUtente_02.full_name, q32_01_alertas_01_RegistoUtente_02.cellphone, q32_01_alertas_01_RegistoUtente_02.userCreated, "
    strSQL = strSQL & "q32_01_alertas_01_RegistoUtente_02.tipoAlerta FROM q32_01_alertas_01_RegistoUtente_02; "
    CurrentDb.Execute strSQL, dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 1


    

    'CurrentDb.Execute "q32_zAlertas_p01" ', dbFailOnError
    '
    
    Me.progressBar_92 = " Passo 2 / 8 "
    strSQL = "INSERT INTO zAlertas ( pk_patron_id, user_type, email, full_name, cellphone, userCreated, tipoAlerta )"
    strSQL = strSQL & " SELECT q32_01_alertas_02_feedBackCurso_02.pk_patron_id, q32_01_alertas_02_feedBackCurso_02.user_type, q32_01_alertas_02_feedBackCurso_02.email, "
    strSQL = strSQL & " q32_01_alertas_02_feedBackCurso_02.full_name, q32_01_alertas_02_feedBackCurso_02.cellphone, q32_01_alertas_02_feedBackCurso_02.userCreated, "
    strSQL = strSQL & " q32_01_alertas_02_feedBackCurso_02.tipoAlerta FROM q32_01_alertas_02_feedBackCurso_02; "
    CurrentDb.Execute strSQL, dbFailOnError
    'CurrentDb.Execute "q32_zAlertas_p02", dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 2
    
    Me.progressBar_92 = " Passo 3 / 8 "
    CurrentDb.Execute "q32_zAlertas_p03", dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 3
    
    Me.progressBar_92 = " Passo 4 / 8 "
    CurrentDb.Execute "q32_zAlertas_p04", dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 4
    
    Me.progressBar_92 = " Passo 5 / 8 "
    CurrentDb.Execute "q32_zAlertas_p05", dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 5
    
    Me.progressBar_92 = " Passo 6 / 8 "
    CurrentDb.Execute "q32_zAlertas_p06", dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 6
    
    Me.progressBar_92 = " Passo 7 / 8 "
    CurrentDb.Execute "q32_zAlertas_p07", dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 7
    
    Me.progressBar_92 = " Passo 8 / 8 "
    CurrentDb.Execute "q32_zAlertas_p08", dbFailOnError
    Me.progressBar_92.Width = 2268 / 8 * 8
    
    Call logMe("frm_importarDados btn_constroiEventos_Click", "end")
    
    Exit Sub
    
errorTrap:
    If Err.Number <> 2501 And Err.Number <> 438 Then
        MsgBox "[frm_importarDados btn_constroiEventos_Click errorTrap]." & _
                "[" & Err.Number & "].[" & Err.description & "]"
        Call logMe("frm_importarDados btn_constroiEventos_Click errorTrap", _
                    "btn_constroiEventos_Click]." & "[" & _
                    Err.Number & "].[" & Err.description & "]")
    End If
    Resume Next
    
    
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well everything id local.... but I'll modify the log a bit to include the error...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if you see the lines: such as Me.progressBar_92 = " Passo 2 / 8 "

They were the problem. progressBar_nn are BOXES! I was addressing the wrong field and became the victim of the Wrath of the programmer's God. Thanks for caring

joao