Share via

Need some help run my first ADO code

Anonymous
2022-06-24T13:49:23+00:00

I am gettin an error on the "cn.ConnectionString" line

Sub getDataFromMultipleFiles()

Dim MyFilesPath As String

Dim cn As ADODB.Connection

MyFilesPath = "C:\Users\ayo\Documents\FI with P8 Actuals"

Set cn = New ADODB.Connection

cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _

                  "Data Source=" & MyFilesPath & "Forecast\_026FI.xlsm;" & \_ 

                  "Extended Properties='Excel 12.0 Macro;HDR=YES';" 

cn.Open

cn.Close

End Sub

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

10 answers

Sort by: Most helpful
  1. Anonymous
    2022-06-24T21:19:05+00:00

    cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _

    "Data Source=" & MyFilesPath & "Forecast_026FI.xlsm;" & _

                      "Extended Properties='Excel 12.0;HDR=YES';" 
    

    if running in source data workbook,codes like below
      Set Conn = CreateObject("ADODB.Connection")
      Set rs = CreateObject("ADODB.Recordset")
      
      Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2022-06-24T16:25:37+00:00

    I just "Unprotected" all the sheets in the Workbook now I get the same error on the "cn.Open" line.

    Was this answer helpful?

    0 comments No comments
  3. HansV 462.6K Reputation points
    2022-06-24T16:16:31+00:00

    You'll have to open the workbook in Excel before opening the ADODB connection. There is no way to specify the password when you open the connection.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2022-06-24T15:53:15+00:00

    Yes it is.

    Was this answer helpful?

    0 comments No comments
  5. HansV 462.6K Reputation points
    2022-06-24T15:44:07+00:00

    Is Forecast_026FI.xlsm a password-protected workbook?

    Was this answer helpful?

    0 comments No comments