如何获取表单的最后一个记录ID?

时间:2012-11-27 15:35:45

标签: access-vba

我目前有一个访问表单。

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

例如,如果我有10条记录,我想得到值“10”,因为这是添加的最后一条记录的id。我正在尝试使用函数last id inserted()运行查询,但它无效。

这是我正在使用的代码:

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

我错过了什么?

3 个答案:

答案 0 :(得分:7)

有一个函数DMax可以获得最高的数字。

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

其他域功能是:

  • DAVG
  • DCOUNT
  • DFirst
  • DLast
  • 使用DLookup
  • DMIN
  • DSTDEV
  • DSTDEVP
  • DSUM
  • DVAR
  • DVARP

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

答案 1 :(得分:1)

在上一篇评论之后,这里是我最近使用的一段代码,用于将记录集的最后一个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

End Sub

答案 2 :(得分:0)

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

e.g

SELECT Max(ID) as result FROM Your_Table_Name

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

例如

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