将DAO.Querydef附加到Access 2007中的DAO.Querydefs集合的正确方法是什么?

时间:2017-07-05 15:55:02

标签: ms-access ms-access-2007

我正在尝试运行以下代码,但它不起作用。我收到错误“3219:无效操作”。我确定我错过了一些东西,很明显,我会感到愚蠢,但我无法发现它。

在伪代码中,这就是我想要做的事情:

  1. 为传递查询创建Querydef。
  2. 使用Querydef打开Recordset。
  3. 检查记录以确定查询是否有效。
  4. 如果一切顺利,请将Querydef追加到Querydef。
  5. 以下是代码:

        Public Sub Test()
    
            Dim db As DAO.Database
            Dim qd As DAO.QueryDef
            Dim rs As DAO.Recordset
            Dim name As String
            Dim conn As String
            Dim sql As String
    
            name = "TestQuery"
    
            conn = _
                "ODBC;" & _
                "DRIVER={iSeries Access ODBC Driver};" & _
                "UID=REMOVED;" & _
                "PWD=REMOVED;" & _
                "SIGNON=1;" & _
                "QRYSTGLMT=-1;" & _
                "PKG=QGPL/DEFAULT(IBM),2,0,1,0,512;" & _
                "LANGUAGEID=ENU;" & _
                "DFTPKGLIB=QGPL;" & _
                "DBQ=QSYS2;" & _
                "SYSTEM=REMOVED;" & _
                "FILEDSN=REMOVED;"
    
            sql = _
                "SELECT TABLE_SCHEMA AS """ & "SCHEMA" & """, " & _
                "TABLE_NAME AS ""TABLE"", " & _
                "ORDINAL_POSITION AS ""POS"", " & _
                "COLUMN_NAME AS ""COLUMN"", " & _
                "DATA_TYPE AS ""TYPE"", " & _
                "LENGTH AS ""LEN"", " & _
                "NUMERIC_SCALE AS ""SCALE"", " & _
                "COLUMN_TEXT AS ""DESCRIPTION"" " & _
                "FROM QSYS2.SYSCOLUMNS " & _
                "WHERE TABLE_SCHEMA='REMOVED' " & _
                "AND TABLE_NAME='REMOVED'"
    
            Set db = CurrentDb
            Set qd = db.CreateQueryDef(name)
    
            qd.Connect = conn
            qd.sql = sql
    
            Set rs = qd.OpenRecordset(DAO.RecordsetTypeEnum.dbOpenSnapshot)
    
            If rs.RecordCount > 0 Then
                'check records to see if query worked as expected
                If True Then
                    db.QueryDefs.Append qd 'Error 3219: Invalid Operation
                End If
            End If
    
        End Sub
    

    如果我将其修改为:

            Dim qd As DAO.QueryDef
    

    OpenRecordset给出错误“3420:对象无效或不再设置”。

0 个答案:

没有答案