Access DAO使用存储的追加查询获取新创建的记录ID

时间:2016-02-03 13:44:13

标签: vba ms-access access-vba dao

我正在尝试获取最后添加的记录ID,但似乎无法加载记录集。我继续得到“运行时错误3219无效操作”。我做错了什么?

        Dim db As DAO.Database
        Dim qry As DAO.QueryDef
        Dim rs As DAO.Recordset

        Set db = CurrentDb
        Set qry = db.QueryDefs("AASourceApp")

        'qry.Parameters.Refresh
        qry.Parameters("[Forms]![AAAccounts Form]![AASourcDoc Subform].[Form]![cmbUpgradeTo]") = [Forms]![AAAccounts Form]![AASourcDoc Subform].[Form]![cmbUpgradeTo]
        qry.Parameters("[Forms]![AAAccounts Form]![AASourcDoc Subform].[Form]![SourceID]") = [Forms]![AAAccounts Form]![AASourcDoc Subform].[Form]![SourceID]
        'qry.Execute  'if unquoted qry.execute works without a problem

        'Determine newly created record id
        Set rs = qry.OpenRecordset()   ''''''ERROR - Invalid Operation''''''
        Debug.Print rs.LastModified

        rs.Close
        Set rs = Nothing
        Set db = Nothing

以下是AASourceApp查询的存储SQL:

PARAMETERS [Forms]![AAAccounts Form]![AASourcDoc Subform].[Form]![SourceID] Long, [Forms]![AAAccounts Form]![AASourcDoc Subform].[Form]![cmbUpgradeTo] Short;
    INSERT INTO [SourcDoc Tbl] ( [SourceID Link], [SubAccount ID], SourcTypeID, SDate )
    SELECT [SourcDoc Tbl].SourceID, [SourcDoc Tbl].[SubAccount ID], [Forms]![AAAccounts Form]![AASourcDoc Subform].[Form]![cmbUpgradeTo] AS DocType, Date() AS Date1
    FROM [SourcDoc Tbl]
    WHERE ((([SourcDoc Tbl].SourceID)=[Forms]![AAAccounts Form]![AASourcDoc Subform].[Form]![SourceID]));

1 个答案:

答案 0 :(得分:0)

它不应该返回任何数据。你需要一个查询来获取最新的,我不认为你可以这样做。也许选择具有相同的标准和参数?