+ Reply to Thread
Results 1 to 13 of 13

Run SQL Update via VBA

  1. #1
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Run SQL Update via VBA

    I know you can run a SQL select statement via VBA by using the following:
    Please Login or Register  to view this content.

    To run an update query how would I do it? Just change Select to Update? In the select statement you give the table that you want to update the records in...how would it go with an Update statement?

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2507 Win 11
    Posts
    24,929

    Re: Run SQL Update via VBA

    You will need to use this format in your SQL statement
    Please Login or Register  to view this content.
    More information on SQL here

    http://www.w3schools.com/sql/sql_update.asp
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Run SQL Update via VBA

    Awesome...I thought that was what would be needed, but I wasn't 100% sure. I am going to show you what I am trying to do (and this is probably the most AWFUL way to do it) I have my code now set as:
    Please Login or Register  to view this content.
    Last edited by jo15765; 05-11-2012 at 09:36 PM. Reason: Left out the sDate = Format piece

  4. #4
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Run SQL Update via VBA

    I have modified my code to be set up like this....It debugs fine, compiles fine, but it's not actually updating the fields in my table! What is going on?
    Please Login or Register  to view this content.
    Last edited by jo15765; 05-12-2012 at 11:48 PM.

  5. #5
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Run SQL Update via VBA

    that should not compile since you are passing arguments to functions that don't take any. you also never actually run any SQL commands in it.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  6. #6
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Run SQL Update via VBA

    Quote Originally Posted by JosephP View Post
    that should not compile since you are passing arguments to functions that don't take any.
    Are you referencing me passing upRP from my main module to the two functions? If I place a msgBox in the two functions and ask it to show upRP it will give me the correct table name, so it is passing the data from the Array to the Functions?

    Quote Originally Posted by JosephP View Post
    you also never actually run any SQL commands in it.
    Is the update that I am attempting to run in the function not SQL?

    How would I need to re-write this procedure so that it actually executes?

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Run SQL Update via VBA

    Your function declaration
    Please Login or Register  to view this content.
    does not declare any parameters so you cannot pass it arguments. the code you posted will not run. even if it did, the functions do not do anything other than assign some SQL to string variables. they don't actually run it-for that you would need a
    Please Login or Register  to view this content.
    type of statement.

  8. #8
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Run SQL Update via VBA

    Quote Originally Posted by JosephP View Post
    Your function declaration
    Please Login or Register  to view this content.
    does not declare any parameters so you cannot pass it arguments.
    Holy cow, IDK how I missed that! I modified the functions to accept the parameter like so:
    Please Login or Register  to view this content.
    Quote Originally Posted by JosephP View Post
    The functions do not do anything other than assign some SQL to string variables. they don't actually run it-for that you would need a
    Please Login or Register  to view this content.
    type of statement.
    Where would I place that line of sql so that the functions execute? Where in my code would I place that?

  9. #9
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Run SQL Update via VBA

    I have it functioning properly now...Thank you for pointing the error in my ways. Now I said it is working probably not the most efficient way of coding it. I modified my code to this:
    Please Login or Register  to view this content.
    Last edited by jo15765; 05-12-2012 at 11:47 PM.

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Run SQL Update via VBA

    I would just have the function return the SQL to run. I don't really understand why you are passing arguments to the functions if you don't use them?

  11. #11
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Run SQL Update via VBA

    Quote Originally Posted by JosephP View Post
    I would just have the function return the SQL to run. I don't really understand why you are passing arguments to the functions if you don't use them?
    I'm not following what you mean?

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Run SQL Update via VBA

    I mean that you pass upRP to the functions but never use it. since the only difference in SQL seems to be number of days, you could have one function and pass that
    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Run SQL Update via VBA

    Oh that is much cleaner and nicer than the monstrous code I was working with! I will definitely use that, thank you

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1