0

This sub is run in Access to import data from an Excel file, "FileName"

This connection string/open procedure is pretty much copied from MSDN references, but there is an error.

When I step through, I receive "unrecognized database format" on the oConn.Open line.

Filename is a spreadsheet, not a database. How do I indicate this?

Public Sub Import2(FileName As Variant)
    Dim wb As Object, ws As Object
    Dim xl As Object
    Set xl = CreateObject("excel.Application")
    Dim qs As String
    Dim ValueString As String
    Dim sConn As String

    Dim oConn As Object
    Set oConn = CreateObject("adodb.connection")

    oConn.ConnectionString = "Provider=microsoft.ACE.OLEDB.12.0; Data Source=filename; Extended Properties=Excel 12.0 XML; hdr=yes" & ";"
    oConn.Open FileName

    Set wb = xl.Workbooks.Open(FileName)
    Set ws = wb.worksheets("For Export")
    data1 = ws.cells(2, 1)
    Data2 = ws.cells(2, 2)
    Data3 = ws.cells(2, 3)

    'Following lines may or may not be correct.  Working on the connection string
    'ValueString = Format(data1, "yyyy\/mm\/dd")
    'qs = "INSERT INTO MAF (FormDate) VALUES (#" & ValueString & "#)"

    DoCmd.RunSQL (qs)
    'CurrentDb.Execute qs

End Sub
2
  • Regarding your code i see that there is two different methods in together: adodb and Excel.application. If you comment this line 'oConn.Open FileName' you code work as you exspect? Commented Jan 29, 2016 at 9:30
  • That did it! Although i'm not sure why. I have a LOT more studying to do. Thanks! Commented Jan 29, 2016 at 9:34

1 Answer 1

1

Change Data Source=filename with something like Data Source=c:\test\db1.mdb with the path of your file

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this but no change. It looks like the connection string is looking for a database, but I'm pulling data from a spreadsheet. Error was "Unrecognized database format"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.