Ms Access MsgBox 以字符串形式提取最后一条记录
MsAccess MsgBox to pull last record as a string
我正在尝试创建一个按钮,向用户显示保存到数据库中的最后批次、产品代码和数量。
我不知道如何用VBA写这个,在其他语言中可以通过方法Bottom
或Max
查看最后一条记录。
我正在尝试从 Product
列中提取数据,其中 User
列等于 Environ("USERNAME")
并且 ID
列具有最大值。
Table 的拉取是 Data_Log
.
一直在四处寻找,但还没有运气。谢谢!
您可以尝试这样的操作:
Private Sub cmdShowLastData_Click()
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("select * from Data_Log where [User]='" & _
Environ("USERNAME") & "' and [ID]=" & DMax("ID", "Data_Log", "[User]='" & Environ("USERNAME") & "'"))
MsgBox "Last product: " & rst!Product & vbCrLf & _
"Last product code: " & rst!ProductCode & vbCrLf & _
"Last product qty: " & rst!Qty
rst.Close
Set rst = Nothing
End Sub
Sergeys 解决方案的替代方案,使用 TOP 1
:
strSql = "SELECT TOP 1 * FROM Data_Log WHERE [User]='" & Environ("USERNAME") & "' " & _
"ORDER BY ID DESC"
Set rst = CurrentDb.OpenRecordset(strSql)
其他同上
我正在尝试创建一个按钮,向用户显示保存到数据库中的最后批次、产品代码和数量。
我不知道如何用VBA写这个,在其他语言中可以通过方法Bottom
或Max
查看最后一条记录。
我正在尝试从 Product
列中提取数据,其中 User
列等于 Environ("USERNAME")
并且 ID
列具有最大值。
Table 的拉取是 Data_Log
.
一直在四处寻找,但还没有运气。谢谢!
您可以尝试这样的操作:
Private Sub cmdShowLastData_Click()
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("select * from Data_Log where [User]='" & _
Environ("USERNAME") & "' and [ID]=" & DMax("ID", "Data_Log", "[User]='" & Environ("USERNAME") & "'"))
MsgBox "Last product: " & rst!Product & vbCrLf & _
"Last product code: " & rst!ProductCode & vbCrLf & _
"Last product qty: " & rst!Qty
rst.Close
Set rst = Nothing
End Sub
Sergeys 解决方案的替代方案,使用 TOP 1
:
strSql = "SELECT TOP 1 * FROM Data_Log WHERE [User]='" & Environ("USERNAME") & "' " & _
"ORDER BY ID DESC"
Set rst = CurrentDb.OpenRecordset(strSql)
其他同上