Results 1 to 3 of 3
  1. #1
    alvinmathew88 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jul 2015
    Posts
    2

    Exclamation Excel to Access via ADO



    Code:
    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & vntPath _
    & ";Extended Properties=""Excel 12.0 XML;HDR=Yes;"";"
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    
    'Open Connection
    cn.Open strConn
    
    
    Dim strSQL As String
    
    
    strPath = CurrentProject.FullName
    
    
    Debug.Print strPath
    strSQL = "Insert into [;Database=" & strPath & "].Table1 Select * From [" & strRangeAddress & "];"
    Debug.Print strConn
    Debug.Print strSQL
    rs.Open strSQL, cn
    
    
    
    
    rs.Close
    cn.Close

    - Code is contained in a module in the database located at "strPath" location.

    - All variables are assigned values before the code snippet above.

    - Im trying to insert the data from excel into Access. The error I get is "the database has been placed in a state by user 'Admin' on machine 'xxxxx' that prevents it from being opened or locked". its fails at this line : rs.Open strSQL, cn

    - I would normally use DoCmd.Transferspreadsheet, but the field names don't correlate with excel header fields.

    What do I need to modify to get my code working?

    Please help. Thanks in advance.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,649
    You don't open a recordset with an action SQL. Recordsets are based on SELECT queries. You don't even need a recordset object in this case just to populate table. I've never tried building a query from an Excel range.

    Try:

    Dim strSQL As String, strConn As String, strPath As String, strRangeAddress As String, vntPath As Variant, cn As ADODB.Connection

    'where is code that sets strRangeAddress and vntPath?

    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & vntPath _
    & ";Extended Properties=""Excel 12.0 XML;HDR=Yes;"";"

    Set cn = New ADODB.Connection
    cn.Open strConn

    strPath = CurrentProject.FullName
    strSQL = "Insert into [;Database=" & strPath & "].Table1 Select * From [" & strRangeAddress & "];"
    CurrentDb.Execute strSQL

    cn.Close
    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.

  3. #3
    alvinmathew88 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jul 2015
    Posts
    2
    The issue was having resolved by Compacting and Repairing the database.
    I ended up actually using CurrentDb.execute before you suggested it. Thanks!

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

Similar Threads

  1. Import access query data into excel file, using excel vba
    By exceljockey in forum Import/Export Data
    Replies: 3
    Last Post: 11-15-2013, 02:36 PM
  2. Replies: 7
    Last Post: 04-25-2013, 03:47 PM
  3. Replies: 10
    Last Post: 12-28-2012, 02:06 PM
  4. Replies: 4
    Last Post: 12-17-2012, 01:21 PM
  5. Replies: 1
    Last Post: 08-31-2009, 10:24 AM

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