+ Reply to Thread
Results 1 to 12 of 12

update table in access from excel using sql

  1. #1
    Registered User
    Join Date
    02-14-2013
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2010
    Posts
    55

    update table in access from excel using sql

    Hello,

    I would like to update a table in access using a sql statement in Excel. I am not overly familiar with SQL and am getting a syntax error when trying to update. Any feedback is appreciated. The full SQL statement is as follows: UPDATE tbl_RETRO_EFF_76_PREV_WK AS t1 INNERJOIN tablec AS t2 ON t1.NUMBER = t2.NUMBER set COMPLETED =True, DATE =3/17/2014, NTID =Mike;
    Please Login or Register  to view this content.
    Last edited by Leith Ross; 03-18-2014 at 11:10 PM. Reason: Added Code Tags

  2. #2
    Registered User
    Join Date
    02-14-2013
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: update table in access from excel using sql

    The error is specific to the update statement

  3. #3
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: update table in access from excel using sql

    With Access Date is keyword and I think you must pass value between # symbols:
    Please Login or Register  to view this content.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  4. #4
    Forum Contributor
    Join Date
    06-16-2011
    Location
    London
    MS-Off Ver
    Excel XP
    Posts
    276

    Re: update table in access from excel using sql

    What Izandol said. Dates must be within # symbols and strings must be within ' symbols.

  5. #5
    Registered User
    Join Date
    02-14-2013
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: update table in access from excel using sql

    Hi, thanks for the replies. I adjusted the fileds and even commented out all but the date and am still getting a syntax error in the update statement. Could it be how I am refernceing the tables?

    Sub test()

    Dim acApp As Object
    Dim sqlString As String
    Dim dbs As Database
    Dim ws As Worksheet
    Dim wsn As String
    Dim c As Range
    Dim tn As String
    Dim rng As Range

    wsn = Range("wsName").Value
    tn = Range("tblName").Value

    Set acApp = CreateObject("Access.Application")
    Set rng = Application.Range(tn & "[completed]")


    Worksheets(wsn).Activate

    For Each c In rng
    If c.Value = True Then
    sqlString = "UPDATE tbl_RETRO_EFF_76_PREV_WK AS t1 INNERJOIN tablec AS t2 ON t1.HIC_NUMBER = t2.HIC_NUMBER set"
    sqlString = sqlString & " [COMPLETED] =" & c.Value & ","
    sqlString = sqlString & " [DATE] = #" & c.Offset(0, -1).Value & "#,"
    sqlString = sqlString & " [NTID] '=" & c.Offset(0, -2).Value & "';"

    Debug.Print sqlString


    With acApp
    Set dbs = OpenDatabase("mydb")
    With dbs
    .Execute sqlString, dbFailOnError
    .Close
    End With
    Set dbs = Nothing
    End With
    Else
    End If

    Next

    'tablec is the excel table and tbl_RETRO_EFF_76_PREV_WK is the access table

    Thanks again

  6. #6
    Registered User
    Join Date
    02-14-2013
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: update table in access from excel using sql

    here's the sql statement - UPDATE tbl_RETRO_EFF_76_PREV_WK AS t1 INNERJOIN [tablec] AS t2 ON t1.HIC_NUMBER = t2.HIC_NUMBER set [COMPLETED] =True, [DATE] = #3/17/2014#, [NTID] ='Mike';

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: update table in access from excel using sql

    You have the first single quote in the wrong place, it should be after the =.
    Please Login or Register  to view this content.
    By the way, a good way to check the SQL statement is to print it out to the debug window (CTRL+G) with ?sqlString, copying it and then pasting it into the SQL view of a query in the database
    If posting code please use code tags, see here.

  8. #8
    Registered User
    Join Date
    02-14-2013
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: update table in access from excel using sql

    I believe it's an issue with the date. I changed a few things and added some variables. All of them work aside from the date variable. I used the ntid and completed together and it updates fine, but as soon as I add the date, it fails.

    sqlString = "UPDATE " & qn & " AS t set NTID = '" & z & "', COMPLETED = " & x & ", DATE = #" & y & "# WHERE HIC_NUMBER = '" & h & "'"

    here's the debug print - UPDATE tbl_RETRO_EFF_76_PREV_WK AS t set NTID = 'chuck', COMPLETED = True, DATE = #3/17/2014# WHERE HIC_NUMBER = '1234ABC'

    **The columns that use dates are formatted as dates and the y variable is a date.**

    Thanks in advance!

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: update table in access from excel using sql

    The SQL statement looks fine, I certainly don't see anything wrong with the date.

    What happens when you try and execute the SQL in the database?

  10. #10
    Registered User
    Join Date
    02-14-2013
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: update table in access from excel using sql

    It did the same thing. I changed the field DATE to COMPLETED_DATE and it worked fine. Any idea why DATE wouldn't work in updating data from excel to access?

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: update table in access from excel using sql

    As Izandol mentioned in post #3, DATE is a reserved word.

    If you must have a field with the name DATE, or any other reserved word or with illegal characters, enclose it in [].

  12. #12
    Registered User
    Join Date
    02-14-2013
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: update table in access from excel using sql

    ahh that makes sense, thanks for all the help!

+ 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. How do I Update Specific Access 2007 Table Fields Using Excel 2007 Macro?
    By anton20 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-05-2014, 02:49 PM
  2. 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
  3. Update & Append Access Table Using Excel Macro - ADO
    By erock24 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-19-2010, 04:49 AM
  4. VBA to update cells with data from Microsoft Access table
    By drewship in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-01-2009, 07:31 AM
  5. [SOLVED] export access to excel. change access & update excel at same time
    By fastcar in forum Excel General
    Replies: 0
    Last Post: 06-24-2005, 05:05 PM

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