Results 1 to 11 of 11
  1. #1
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776

    CurrentDb.Execute Question


    Hi all,
    I want to try something new to me that June7 informed me on a bit. Never using it i want this to run an update qry and not real sure on how to put it into code?

    Code:
    Private Sub TxtDomainName_AfterUpdate()    
    ' If form is a new record, then no update necessary as no email addresses exist!
    
    
        If (Not Form.NewRecord) Then
        ' Run UpDate Qry to update 
            CurrentDb.Execute " CompanyDomainUpDateQry"
        End If
    End Sub
    Would it go as I have it above or do I need something else for it to run. Since its an update qry i dont want to practice with it, lol....
    Also, would i put this in the AfterUpdate or OnChange Property?????

    Thanks
    Dave

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    I think what you have would work once you delete the space at the beginning of the query name.
    Since its an update qry i dont want to practice with it, lol....
    Why not make a copy and play about with that one. (and the backend, of course, if it's split)
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Thank you, I will give it a shot!

  4. #4
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Ran it as AfterUpDate and received error!

    Error:
    Too few parameters. Expected 2

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    post the SQL statement of the query
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  6. #6
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Click image for larger version. 

Name:	Qry.jpg 
Views:	11 
Size:	130.2 KB 
ID:	42885

    The qry works great when i dbl clk it. I am wondering if i dont need a DoCmd in that Exe Code?

  7. #7
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    This Qry changed and no longer works. It puts an expression 1 in instead of the Field and tbl name

  8. #8
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    OK, I fixes Qry. Sorry about that. I was using a practice copy for this. Forgot to change the domain in email tbl...
    I ran again after coirrections, i got same error excpet Expected 1


    Click image for larger version. 

Name:	Untitled.jpg 
Views:	11 
Size:	131.0 KB 
ID:	42886

  9. #9
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    If you get the query to work again (open in design view and reset the fields and table names where appropriate) you have two options:

    1. use Docmd.OpenQuery instead of CurrentDB.Execute as that will not trigger the error (queries with references to forms will do that when used with .Execute). You will need to add Docmd.SetWarnings False before the .OpenQuery and Docmd.SetWarnings True after.
    Code:
    Docmd.SetWarnings False 
    Docmd.OpenQuery "CompanyDomainUpDateQry"
    Docmd.SetWarnings True
    2. To use execute try the following:
    Code:
    Dim prm as Parameter
    For Each prm In CurrentDB.QueryDefs("CompanyDomainUpDateQry")
          prm.Value=Eval(prm.Name)
    Next prm
    CurrentDb.Execute "CompanyDomainUpDateQry",dbFailOnError
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #10
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Thank you,
    The First version worked well as expected.
    I will stick with that.

  11. #11
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Glad to hear, actually the second version should be slightly different:
    Code:
    Dim prm as Parameter, qdf as DAO.QueryDef
    Set qdf=CurrentDB.QueryDefs("CompanyDomainUpDateQry")
    For Each prm In qdf.Parameters
          prm.Value=Eval(prm.Name)
    Next prm
    qdf.Execute dbFailOnError
    
    Set qdf=Nothing
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

Please reply to this thread with any new information or opinions.

Similar Threads

  1. RunSQL vs. QueryDef.Execute vs. CurrentDB.Execute
    By caubetiep1337 in forum Queries
    Replies: 8
    Last Post: 12-16-2015, 05:35 PM
  2. CurrentDB.Execute insert working just once
    By krausr79 in forum Access
    Replies: 4
    Last Post: 10-07-2015, 12:04 PM
  3. Error in CurrentDb.Execute
    By Stefan Moser in forum Access
    Replies: 5
    Last Post: 04-17-2015, 01:38 PM
  4. vba code help - Currentdb.execute
    By joycesolomon in forum Programming
    Replies: 5
    Last Post: 08-11-2014, 11:25 AM
  5. Update sql using currentdb.execute
    By laavista in forum Access
    Replies: 13
    Last Post: 08-15-2011, 03:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums