2012-11-27 72 views
1

我目前有一个访问表单。如何获取表单的最后一个记录ID?

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

例如,如果我有10条记录,我想得到值“10”,因为这是添加的最后一条记录的ID。我试图运行一个查询与功能最后id插入(),但它不工作。

这我使用的代码:

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

我缺少什么?

+0

可能重复[如何执行在VBA代码MS-访问查询?](http://stackoverflow.com/questions/4567437/how-to-execute-a-query -in-ms-access-in-vba-code) – Treb

+0

获取所有,按ID登录限制1? 'SELECT * FROM table ORDER BY id DESC LIMIT 1' –

回答

6

有一个功能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

这正是我正在寻找,它的工作完美,谢谢你的回应肖恩这是非常有益的 – derek

0

你要做的就是设置并保存一个查询,它首先为你获取值。说它MaxID

e.g

SELECT Max(ID) as result FROM Your_Table_Name 

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

如。

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

我可以在vba中运行查询吗? – derek

+0

我不确定,我不认为你可以在VBA中运行查询,然后将该值设置为一个变量。我做了我上面的回答,对我很好。虽然有可能更好的方式做!..! :-) – TheRedOne

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 

末次

相关问题