2012-11-27 135 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 

末次

相關問題