+ Reply to Thread
Results 1 to 14 of 14

Excel and SQL - Update Excel Table

  1. #1
    Registered User
    Join Date
    11-04-2014
    Location
    Portugal
    MS-Off Ver
    XL2013/XL365
    Posts
    88

    Excel and SQL - Update Excel Table

    Hi

    I am trying, from a form, to update a table through SQL, with the following instructions:

    Please Login or Register  to view this content.
    Everything goes well except when Var2 is Null.
    What was intended was that when Var2 is null, the table would be updated with a NULL value and not zero, it is as if you delete the value in a cell.

    Is it possible to do that? How?

    Thanks
    Jorge
    Last edited by JCabral; 09-20-2019 at 10:44 AM.

  2. #2
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Excel and SQL - Update Excel Table

    Hi,

    Maybe just add an else condition to the below line, if it gets into else, then Set Var2 to Null (Not with Double Quotes), just Null.

    Please Login or Register  to view this content.
    This is just a guess, please try and let me know if it works..

    Cheers!
    Last edited by NeedForExcel; 09-24-2019 at 05:13 AM.
    Cheers!
    Deep Dave

  3. #3
    Registered User
    Join Date
    11-04-2014
    Location
    Portugal
    MS-Off Ver
    XL2013/XL365
    Posts
    88

    Re: Excel and SQL - Update Excel Table

    Hi
    Thanks for the sugestion.

    I put the following code and gave me this error:

    Please Login or Register  to view this content.
    ERROR:

    Run-time error '94':
    Invalid use of Null



    Any extra help, or new suggestions?

    Thank you very much
    Jorge

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: Excel and SQL - Update Excel Table

    It's combobox so, check if value's length is >0. And for numeric variable, you can't assign empty/null. It should be set to 0.
    If you want to use null/empty you need to declare variable as variant.

    Ex:
    Please Login or Register  to view this content.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  5. #5
    Registered User
    Join Date
    11-04-2014
    Location
    Portugal
    MS-Off Ver
    XL2013/XL365
    Posts
    88

    Re: Excel and SQL - Update Excel Table

    Hi
    To better understand the help I need, I attach a sample file.
    And I will restrict myself to the problem that is happening in the "Private Sub cmdSave_Click" subroutine

    When I select an item in ListBox1 I have the possibility to select and deselect values in lstbDF and lstbFO. I'll just talk about lstbDF.

    So when I select the first item from ListBox1, for example, it turns out that the item "NAME_DF_1" from lstbDF is preselected, so the error appears when I deselect this item in lstbDF, and do "SAVE" since the I want the DB update in BDBACK to be done with a null value and not zero

    Thank you very much
    Jorge
    Attached Files Attached Files

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: Excel and SQL - Update Excel Table

    Then declare variable as variant. Double can't hold null. As VBA interprets empty numeric variable as Zero.

    I.E.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    11-04-2014
    Location
    Portugal
    MS-Off Ver
    XL2013/XL365
    Posts
    88

    Re: Excel and SQL - Update Excel Table

    Keep giving me a mistake.

    "Bad data type in criteria expression"

    Attachment 642678

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: Excel and SQL - Update Excel Table

    You'll need to change SQL query syntax then.

    When you "debug.print Sql" in code before the line that generates that error. What do you get?

    I'm guessing you'll need to change from "Column = 'xxx'" syntax to "Column Is null" syntax when variable is null.

  9. #9
    Registered User
    Join Date
    11-04-2014
    Location
    Portugal
    MS-Off Ver
    XL2013/XL365
    Posts
    88

    Re: Excel and SQL - Update Excel Table

    The error is in ".open"

    Please Login or Register  to view this content.

  10. #10
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: Excel and SQL - Update Excel Table

    I assumed as much. But what does "Debug.Print Sql" return in immediate window?

    I'm guessing that you see "Update SET ... ... [Table$].[Column] = ''," If you need to update column with null value. Syntax should be...
    "Update Set ... ... [Table$].[Column] = NULL,".

    Notice that single quotes are removed when setting column value to NULL. As null isn't a string data type.

  11. #11
    Registered User
    Join Date
    11-04-2014
    Location
    Portugal
    MS-Off Ver
    XL2013/XL365
    Posts
    88

    Re: Excel and SQL - Update Excel Table

    With the code above I have:

    debug.print Sql
    UPDATE [BDBACK$] SET [BDBACK$].[idDF1] = '' , [BDBACK$].[PercDF1] = '' , [BDBACK$].[idDF2] = '' , [BDBACK$].[PercDF2] = '' , [BDBACK$].[idFO1] = 'NOME_FO_3' , [BDBACK$].[PercFO1] = '0,4' , [BDBACK$].[idFO2] = '' , [BDBACK$].[PercFO2] = '' , [BDBACK$].[idFO3] = '' , [BDBACK$].[PercFO3] = '' , [BDBACK$].[idFO4] = '' , [BDBACK$].[PercFO4] = '' WHERE [BDBACK$].[idCodAtv] = 883;

  12. #12
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: Excel and SQL - Update Excel Table

    So like I said. You need to change your SQL query construction when variable is null. Single quotes should be removed and use null.

    Easiest way to do it is... instead of setting value to Empty. set it to "NULL" string.

    It will construct your Sql as...
    "UPDATE [BDBACK$] SET [BDBACK$].[idDF1] = 'NULL' , [BDBACK$].[PercDF1] = 'NULL' , ..."

    Then you'd use Replace() to remove single quotes around NULL.
    Ex:
    Please Login or Register  to view this content.
    Or just leave variables as is, and replace "''" with "NULL".

  13. #13
    Registered User
    Join Date
    11-04-2014
    Location
    Portugal
    MS-Off Ver
    XL2013/XL365
    Posts
    88

    Re: Excel and SQL - Update Excel Table

    I've replaced by:

    Please Login or Register  to view this content.
    And it seems to work perfectly.
    I'll just test it thoroughly, and then close the post.

    Thank you very much

  14. #14
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: Excel and SQL - Update Excel Table

    You are welcome and thanks for the rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Excel VBA to update table in Access 2013
    By SUMIF in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-21-2015, 01:04 PM
  2. Trying to update an access table from Excel based on unique ID. rst.Update not working
    By Newbie0924 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-22-2015, 04:15 PM
  3. [SOLVED] update table in access from excel using sql
    By specialk9203 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-19-2014, 03:44 PM
  4. Replies: 2
    Last Post: 06-16-2013, 02:58 PM
  5. Update all dynamic table in excel file
    By filnirv in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-16-2011, 01:27 PM
  6. Update Access Table from Excel using DAO
    By vish2025 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-15-2010, 06:02 AM
  7. Update Excel table in Word automatically from data in Excel
    By yvonnedemulder in forum Excel General
    Replies: 1
    Last Post: 12-06-2005, 02:55 PM

Tags for this Thread

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