Writing Excel VBA to receive data from Access

前端 未结 3 1395
南方客
南方客 2020-12-08 17:41

I am writing an excel application that draws from an Access database for work. When the user opens the Excel tool, a data table needs to populate one of the worksheets from

3条回答
  •  余生分开走
    2020-12-08 17:52

    Both DAO and ADO include recordset object types. However they are not compatible. Your declaration for the rs object variable is ambiguous.

    Dim db As DAO.Database, rs As Recordset
    

    A potential problem is that if your project includes a reference to ADO, and the precedence of that reference is above your DAO reference, rs will wind up as an ADO recordset rather than a DAO recordset.

    I'm not certain this is the cause of the error you're seeing. However setting rs to db.OpenRecordset(something) should fail if rs is an ADO recordset because OpenRecordset returns a DAO recordset.

    I think you should change the declaration to this:

    Dim db As DAO.Database, rs As DAO.Recordset
    

    Even if that change doesn't resolve your problem, I encourage you to always qualify the type when declaring recordset object variables ... to avoid ambiguity.

    And if this isn't the fix, please tell us which line of your code triggers the current error you're seeing.

    Here is another red flag:

    Dim TargetRande As String
    

    Later you have:

    Set TargetRange = Range("A1")
    

    Add Option Explict to the Declarations section of your module. Then choose Debug->Compile from the VB editor's main menu. That effort will highlight misspelled variable names, and also alert you to syntax errors.

提交回复
热议问题