VBA ADODB excel - read data from Recordset

后端 未结 1 808
春和景丽
春和景丽 2020-12-03 12:37

Hope you can help me, I would like to read data from excel file, and the way I was doing was creating instance of Excel application in backgroud, but than I am prompted abo

相关标签:
1条回答
  • 2020-12-03 13:33

    I am surprised that the connection string works for you, because it is missing a semi-colon. Set is only used with objects, so you would not say Set strNaam.

    Set cn = CreateObject("ADODB.Connection")
    With cn
     .Provider = "Microsoft.Jet.OLEDB.4.0"
      .ConnectionString = "Data Source=D:\test.xls " & _
      ";Extended Properties=""Excel 8.0;HDR=Yes;"""
    .Open
    End With
    strQuery = "SELECT * FROM [Sheet1$E36:E38]"
    Set rs = cn.Execute(strQuery)
    Do While Not rs.EOF
      For i = 0 To rs.Fields.Count - 1
        Debug.Print rs.Fields(i).Name, rs.Fields(i).Value
        strNaam = rs.Fields(0).Value
      Next
      rs.MoveNext
    Loop
    rs.Close
    

    There are other ways, depending on what you want to do, such as GetString (GetString Method Description).

    0 讨论(0)
提交回复
热议问题