从Access导入查询

时间:2016-08-26 17:49:01

标签: excel vba excel-vba

我通过VBA将Access数据库连接到Excel,从单个表中导入数据,"类别" ,到工作表。下面的代码,但是,我可以导入数据库中已存在的查询而不是表吗?
它显示"错误"当我将表名更改为第15行中的查询名称时。

def getStatus: MyStatus = {
  (mPending, mPublished) match {
    case (None, None) => MyStatus.inactive
    case (Some(Edit), None) => MyStatus.neverPublished
    case (None, Some(Edit)) if published.get.isSuspended => MyStatus.suspended 
    case (None, Some(Edit)) => MyStatus.published
    case (_, Some(published)) if published.get.isSuspended => MyStatus.suspendedWithChanges  // We don't care about the first param, so use _, let the compiler determine the type of `published`.
    case (_, Some(published)) => MyStatus.publishedWithChanges
  }
}

1 个答案:

答案 0 :(得分:0)

为什么不改为Recordset和TableDef?您不需要以这种方式打开DataBase;为Microsoft Office xx.x Access数据库引擎对象库添加引用(tools-> references)

Sub test()
Dim BDAnalyzed As Database
Dim RecordTable As Recordset
Dim RecordTableDef As TableDef
Dim CounterTitles As Long
Const DesiredQuery = "Title For Query"
Const PathToDB = "C:..."
    Set BDAnalyzed = DBEngine.Workspaces(0).OpenDatabase(PathToDB)
    Set RecordTable = BDAnalyzed.OpenRecordset(DesiredQuery, dbOpenDynaset)
    Set RecordTableDef = BDAnalyzed.TableDefs(RecordTable)
    For CounterTitles = 0 To RecordTableDef.Fields.Count - 1
    MsgBox RecordTableDef.Fields(CounterTitles).Name
    Next CounterTitles
End Sub