【问题标题】:How to get the last record id of a form?如何获取表单的最后一个记录ID?
【发布时间】:2020-07-01 16:46:48
【问题描述】:

我目前有一个可以访问的表单。

我想要做的是获取添加的最后一条记录的值。

例如,如果我有 10 条记录,我想获取值“10”,因为这是添加的最后一条记录的 id。我正在尝试使用函数 last id insert() 运行查询,但它不起作用。

这是我正在使用的代码:

Dim lastID As Integer
Query = "select last_insert_id()"
lastID = Query
MsgBox (lastID)

我错过了什么?

【问题讨论】:

标签: vba ms-access


【解决方案1】:

有一个函数DMax会抓取最大的数字。

Dim lastID As Integer
lastID = DMax("IDField","YourTable")
' or = DMax("IDField","YourTable","WhenField=Value")
MsgBox lastID

其他领域功能是:

  • 平均数据
  • DCount
  • DFirst
  • 最后一个
  • DLookup
  • DMin
  • DStDev
  • DStDevP
  • DSum
  • DVar
  • DVarP

使用友好的 F1 键查看更多信息

【讨论】:

  • 这正是我一直在寻找的,而且效果很好,感谢您的回复,肖恩非常有帮助
【解决方案2】:

从上一个 cmets 开始,这是我最近用来将记录集的最后一个 ID 值转换为变量以在 VBA 中使用的一段代码。但是,这并不是很好,因为我仍然无法弄清楚如何将记录的 ID 字段值直接转换为变量。相反,我使用了将记录集复制到 Excel 工作簿中的不雅解决方案,然后将变量值设置为我刚刚复制到的单元格的值。

编辑:解决了如何将 ID 转换为简单变量:末尾的新代码

这都是从单个客户端工作簿运行的:

Option Explicit
Public AftUpD As Long
Public BfrUpD As Long

Sub AssignLstRowAftUpD2()

    Dim dbPP As DAO.Database
    Dim ResTemp As DAO.Recordset
    Dim z As Long
    Dim SelectLast As String

    SelectLast = "SELECT Max(Table1.ID) AS MaxOfID FROM Table1"
    'Debug.Print SelectLast

    Set dbPP = OpenDatabase("C:\filepath\Database11.mdb")
    Set ResTemp = dbPP.OpenRecordset(SelectLast)

    If ResTemp.EOF Then
            GoTo EndLoop
        End If

    Worksheets("Diagnostics").Visible = True
    Worksheets("Diagnostics").Range("C4").CopyFromRecordset ResTemp
    z = Sheets("Diagnostics").Range("C4").Value
    Sheets("Diagnostics").Visible = False
    AftUpD = z
    'Debug.Print AftUpD

EndLoop:
        ResTemp.Close
        dbPP.Close



    Set dbPP = Nothing
    Set ResTemp = Nothing
    'Set SelectionLast = Nothing
    'z = Nothing

End Sub

然后我用这个值作为一个变量来做一个新的 SQL 查询:

Sub Query()
   'This query uses the highest ID value in a companion spreadsheet (the public  
   'variable BfrUpD), which is set in a sub I haven't posted here, to find out 
   'how many records have been added to the database since the last time the
   'spreadsheet was updated, and then copies the new records into the workbook

   'Be warned: If you run this query when BfrUpD is equal to or greater than AftUpD it 
   'will cause a crash. In the end user version of this, I use several If tests,
   'comparing BfrUpD with other public variables, to make sure that this doesn't
   'happen.
    Dim WBout As Excel.Workbook, WSout As Excel.Worksheet
    Dim dbPP1 As DAO.Database
    Dim qryPP1 As DAO.Recordset
    Dim ResTemp1 As DAO.Recordset
    Dim TestValue As String
    Dim strSQL2 As String


    TestValue = BfrUpD
    'Debug.Print TestValue
    strSQL2 = "SELECT * FROM Table1 WHERE (((Table1.ID)>" & TestValue & "))"
    'Debug.Print strSQL2


    Set dbPP1 = OpenDatabase("C:\filepath\Database11.mdb")
    Set qryPP1 = dbPP1.OpenRecordset(strSQL2)

    Set WBout = Workbooks.Open("C:\filepath\h.xlsm")
    Set WSout = WBout.Sheets("sheet1")
    WSout.Range("A1").End(xlDown).Offset(1, 0).CopyFromRecordset qryPP1

    qryPP1.Close
    dbPP1.Close
    WBout.Save
    WBout.Close

    MsgBox "Data copied. Thank you."

Set WBout = Nothing
Set WSout = Nothing
Set dbPP1 = Nothing
Set qryPP1 = Nothing
Set ResTemp1 = Nothing

End Sub

编辑:将字段值直接获取到变量中的代码

    Dim dbPP As DAO.Database
    Dim ResTemp As DAO.Recordset
    Dim z As Long
    Dim SelectLast As String

    SelectLast = "SELECT Max(Table1.ID) AS MaxOfID FROM Table1"
    'Debug.Print SelectLast

    Set dbPP = OpenDatabase("C:\filepath\Database11.mdb")
    Set ResTemp = dbPP.OpenRecordset(SelectLast)
    z = ResTemp(0) 'specifying it's array location (I think) - there is only one
    'item in this result, so it will always be (0)

    AftUpD = z
    'Debug.Print AftUpD
    ResTemp.Close
    dbPP.Close



Set dbPP = Nothing
Set ResTemp = Nothing
'Set SelectionLast = Nothing
'z = Nothing

结束子

【讨论】:

    【解决方案3】:

    您要做的是设置并保存一个查询,该查询首先为您获取值。称之为 MaxID

    例如

    SELECT Max(ID) as result FROM Your_Table_Name
    

    然后,在您的 VBA 代码中,将您的变量设置为该变量

    例如。

    Dim IDresult As Integer
    IDresult = DLookup("[result]", "MaxID")
    MsgBox(IDresult)
    

    【讨论】:

    • 我不确定,我认为您不能在 VBA 中运行查询,然后将该值设置为变量。我做了我上面回答的事情,它对我很有用。不过可能有更好的方法..! :-)
    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 2013-12-17
    相关资源
    最近更新 更多