Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    schulzy175 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Oct 2017
    Posts
    98

    VBA only appends on second run?

    Here is my code:


    Code:
    Private Sub runreport_Click()
    
    DoCmd.SetWarnings False
    Dim runn As Integer
    Dim starttime As String
    Dim endtime As String
    Dim ftime As String
    
    
    Dim ssql(1 To 7) As String
    ssql(1) = "LMSB_Recharges"
    ssql(2) = "LMSB_Volume"
    ssql(3) = "LMSB_Wextras"
    ssql(4) = "LMSB_WType_Q"
    ssql(5) = "PriorSales_Append"
    ssql(6) = "GF_NoNote_RemovedARM"
    ssql(7) = "GF_Month_Signup"
    
    
    starttime = Timer
    
    
    For runn = 1 To 7
        DoCmd.OpenQuery ssql(runn)
    Next runn
    DoCmd.SetWarnings True
    
    
    endtime = Timer
    ftime = Format(((endtime - starttime) / 86400), "hh:mm:ss")
    Debug.Print ftime
    DoCmd.OpenReport "TestReport_Table", acViewReport
    End Sub
    I do not know where to begin to do research on this issue. For some reason, If I run this code once, nothing appends except my sales. If I run it a 2nd time, all the following queries append including the sales twice. Any clue why the queries don't append on the first run?

  2. #2
    schulzy175 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Oct 2017
    Posts
    98
    Update: So I ran it step by step and on
    Code:
    docmd.openquery "LMSB_Recharges"
    it just skips running the query. It doesn't actually skip the line, but it registers and runs the line of code, but nothing gets run.

  3. #3
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Arrays are zero based, so try restructuring your array code to set up as 0-6.

  4. #4
    schulzy175 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Oct 2017
    Posts
    98
    So I changed
    Code:
    DoCmd.OpenQuery
    to
    Code:
    CurrentDB.Execute
    and now it's not recognizing any of the queries. All the queries listed in my code are action queries.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    To me it's an over-complication of things to populate an array, then loop through it. Why not just have 7 lines to execute queries, either with OpenQuery or Execute? You can add to Execute to make it error, it will fail silently if not included:

    CurrentDb.Execute "QueryName", dbFailOnError
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    schulzy175 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Oct 2017
    Posts
    98
    Quote Originally Posted by pbaldy View Post
    To me it's an over-complication of things to populate an array, then loop through it. Why not just have 7 lines to execute queries, either with OpenQuery or Execute? You can add to Execute to make it error, it will fail silently if not included:

    CurrentDb.Execute "QueryName", dbFailOnError
    Thanks for the suggestion. When I first chose to run multiple queries back to back using VBA, I didn't know if the best way would be an array or just lines for each query.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem. Arrays have their place, but this didn't seem like one of them to me.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    schulzy175 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Oct 2017
    Posts
    98
    I am still getting a parameter error (missing parameters 1) when it reaches "PriorSales_Append". I'm still doing some debugging so I'll keep everyone posted.

  9. #9
    schulzy175 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Oct 2017
    Posts
    98
    So I figured out my problem. On my query "PriorSales_Append", I have a date range that uses my forms textboxes. So I just needed to change my VBA to run SQL instead of Currentdb.Execute. All other queries are run from "PriorSales_Append_Table", which is the appended table of "PriorSales_Append."

  10. #10
    schulzy175 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Oct 2017
    Posts
    98
    Update: So I fixed the issue with parameters, but still it doesn't run my action queries. I've purposes added both to debug:
    Code:
    DoCmd.OpenQuery "LMSB_Recharges"
    CurrentDb.Execute "LMSB_Recharges"
    It gives no errors, but just goes to the next line not executing anything?

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Did you try adding the dbFailOnError argument as mentioned earlier?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    schulzy175 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Oct 2017
    Posts
    98
    Ok, I've really found the solution this time. So my
    Code:
    DoCmd.SetWarnings False
    was basically selecting no when asked to append the results/make changes to the table.

  13. #13
    schulzy175 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Oct 2017
    Posts
    98
    Apologies for the run-around. I appreciate all the help. Still learned stuff though Thanks again!

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Really? SetWarnings should just suppress the warnings, basically answering yes so the query still runs.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  15. #15
    schulzy175 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Oct 2017
    Posts
    98
    Yea, the last thing I could think of to debug it was to set warnings back to true in the beginning and voila, it asked if I wanted to append the results, but when I re-ran the code setting warnings back to false, it just went to the next line of code not appending anything. I figured setting warnings to false would automatically choose yes, but this is the first case where that isn't the case. Now I need to do some research to see what I need to do to have it choose yes. I was going to just run the whole thing as an actual SQL statement in VBA, but I kept getting an error "Query must contain at least one query or table". So I just reverted back to DoCmd.RunQuery.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 01-24-2017, 08:26 AM
  2. Replies: 5
    Last Post: 12-15-2015, 07:11 AM
  3. Replies: 2
    Last Post: 08-12-2012, 10:56 AM

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