+ Reply to Thread
Results 1 to 9 of 9

Problem with VBA/ADO Insert Into

  1. #1
    Forum Contributor
    Join Date
    12-26-2010
    Location
    Kansas City, Kansas
    MS-Off Ver
    Excel 2007
    Posts
    147

    Problem with VBA/ADO Insert Into

    Goal: Insert WS data into MS Access Table
    using ADO and SQL

    Error:Invalid Argument

    SQL Used:
    INSERT INTO MyNewTable (C1,C2,C3) SELECT * FROM
    [Excel 12.0;HDR=YES;C:\Development\Excel_Sandy_NaturalFactors\A_Programs\NatFactors.xlsm].[BY_X$]

    Connection:
    Valid Connection

    Table Structure:
    C1 - Text
    C2 - Text
    C3 - Text

    Table Name:
    MyNewTable

    WS NAME:
    BY_X

    Code:
    The code is below but now useful
    in that it calls related properties.


    Please Login or Register  to view this content.
    Last edited by JohnM3; 07-05-2011 at 12:48 PM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,370

    Re: Problem with VBA/ADO Insert Into

    Hi John,

    Why not just import Excel data into Access?

    http://conferences.aicpa.org/tech08/...%20Fleenor.pdf
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Contributor
    Join Date
    12-26-2010
    Location
    Kansas City, Kansas
    MS-Off Ver
    Excel 2007
    Posts
    147

    Re: Problem with VBA/ADO Insert Into

    Hi MarvinP

    That is a great article and I appreciate it and have saved it.

    However, I am very interested in nailing down the correct syntax to simply use
    ADO/SQL to load a worksheet to an Access table and hope someone here might
    already be using it and might run across my question. I am googled-out and out
    of ideas but suspect my syntax is close to the correct syntax.

    regards
    John

  4. #4
    Forum Contributor
    Join Date
    12-26-2010
    Location
    Kansas City, Kansas
    MS-Off Ver
    Excel 2007
    Posts
    147

    Re: Problem with VBA/ADO Insert Into

    Goal: Insert an Excel WS to an Access table (in one shot) using ADO and SQL.

    Good SQL: The following SQL works (Excel 2007 -> Access 2007)

    INSERT INTO MyNewTable SELECT * FROM [BY_X$]
    IN 'C:\Development\Excel_Sandy_NaturalFactors\A_Programs\NatFactors.xlsm' 'Excel 12.0 Macro;HDR=YES;'

    Note:
    There has to be as space between .... xlsm' 'Excel (2nd line of SQL)

    This code uses internal properties that are not defined below.

    Please Login or Register  to view this content.

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,370

    Re: Problem with VBA/ADO Insert Into

    Great Job!!

    Now make the Forum Master happy and edit your first post and mark the Prefix to the Title as Solved!!

    I'm so glad you got this answer as I didn't feel I was any help.

    AND - Your code above might help others with the same question!!

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Problem with VBA/ADO Insert Into

    FYI, you were very close with what you had originally:

    INSERT INTO MyNewTable (C1,C2,C3) SELECT * FROM
    [Excel 12.0;HDR=YES;Database=C:\Development\Excel_Sandy_NaturalFactors\A_Programs\NatFactors.xlsm].[BY_X$]
    Everyone who confuses correlation and causation ends up dead.

  7. #7
    Forum Contributor
    Join Date
    12-26-2010
    Location
    Kansas City, Kansas
    MS-Off Ver
    Excel 2007
    Posts
    147

    Re: Problem with VBA/ADO Insert Into

    Hi RS

    _THANKS_ for posting that. I like your solution better.

    INSERT INTO MyNewTable (C1,C2,C3) SELECT * FROM
    [Excel 12.0;HDR=YES;Database=C:\Development\Excel_Sandy_NaturalFactors\A_Programs\NatFactors.xlsm].[BY_X$]


    regards
    John

  8. #8
    Registered User
    Join Date
    10-25-2012
    Location
    chennai
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Problem with VBA/ADO Insert Into

    Hi I am trying to import data to Mssql 2008, can anyone help me

    For the below query, equivalent query in SQL

    INSERT INTO MyNewTable SELECT * FROM [BY_X$]
    IN 'C:\Development\Excel_Sandy_NaturalFactors\A_Programs\NatFactors.xlsm' 'Excel 12.0 Macro;HDR=YES;'

    When i try the same query in sql it says 'incorrect syntax near IN'

    Even i tried the option suggested by RS,
    Like this
    INSERT INTO MyNewTable (C1,C2,C3) SELECT * FROM
    [Excel 12.0;HDR=YES;Database=C:\Development\Excel_Sandy_NaturalFactors\A_Programs\NatFactors.xlsm].[BY_X$]

    I am getting invalid object name.please help me.

    Regards,
    Sridevi
    Last edited by sridevi kumaravel; 10-25-2012 at 07:14 AM. Reason: missed a text

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

    Re: Problem with VBA/ADO Insert Into

    Welcome to the Forum, unfortunately:

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.
    Josie

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

+ 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