Results 1 to 7 of 7
  1. #1
    McArthurGDM is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    215

    Append Query Stops Working in RunSQL Command Question

    I have an Append Query that works when I write the SQL code, as such:

    INSERT INTO StatusT ( State, Metric )
    SELECT "OH" AS Expr1, [Forms]![MainNavigation].[NavigationSubform].[Form]![AddMetric].[Form]![MMetricID] AS TEST;
    In this case, the Query will append the word "OH" to the State field and the Metric is referenced by a control on another form.



    However,

    When I convert it to VBA using the RunSQL command, the SQL stops working properly. I converted the "OH" to be defined by a String variable, as such:

    Dim StrQ As String


    StrQ = "OH"


    DoCmd.RunSQL ("INSERT INTO StatusT ( State, Metric ) SELECT" & StrQ & " AS Expr1, [Forms]![MainNavigation].[NavigationSubform].[Form]![AddMetric].[Form]![MMetricID] AS TEST;")


    End Sub
    What ends up happening in this case is that it asks me for a parameter for OH. I fill it in and then it appends.

    I have no clue why.

    Thanks,

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I am guessing that Metric is a numeric field?
    Text fields require text qualifiers of single or double-quotes around them. I like to use Chr(34) so as to not confuse it with the other double-quotes I am using in my formula. I think you also need a space after the word "SELECT". And I believe that the aliases in your calculated fields are not necessary in Append Queries.

    So try:
    Code:
    DoCmd.RunSQL ("INSERT INTO StatusT ( State, Metric ) SELECT " & Chr(34) & StrQ & Chr(34) & ", [Forms]![MainNavigation].[NavigationSubform].[Form]![AddMetric].[Form]![MMetricID];")

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,825
    I prefer to use CurrentDb.Execute instead of DoCmd.RunSQL.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    McArthurGDM is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    215
    Thank you so much, JoeM.

    What you wrote worked. Aliases are needed if you were to create the query in Access designer, but not that. Thanks for the tip on Chr(34) as well.

  5. #5
    McArthurGDM is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    215
    Why do you prefer that one over the RunSQL cmd?

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,825
    Don't have to deal with popup warnings for one thing.

    http://www.eileenslounge.com/viewtopic.php?f=29&t=9414
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I prefer to use CurrentDb.Execute instead of DoCmd.RunSQL.
    I don't remember all the details, but I have had instances in the past where "CurrentDb.Execute" wouldn't work, but "DoCmd.RunSQL" does on the exact same SQL code (June, I think we actually had a discussion on that some time ago on one of these threads). Maybe it had something to do with "syntax" (I wish I could remember).

    For that reason, I have always used "DoCmd.RunSQL" myself.

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

Similar Threads

  1. Access stops working and reloads
    By Simgene in forum Programming
    Replies: 4
    Last Post: 12-26-2014, 03:28 PM
  2. find (ctrl + F) stops working
    By mad4mp3z in forum Access
    Replies: 2
    Last Post: 04-07-2014, 12:19 PM
  3. Replies: 3
    Last Post: 02-05-2013, 05:07 PM
  4. docmd.runsql not working
    By Rameez in forum Programming
    Replies: 3
    Last Post: 08-07-2009, 10:07 AM
  5. Having - Like stops working
    By tdalber in forum Queries
    Replies: 0
    Last Post: 02-03-2009, 04:30 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