访问VBA集文本框等于查询结果

时间:2016-12-15 14:40:13

标签: vba ms-access access-vba ms-access-2013

我正在运行此语法来查询表并将texbox设置为等于rs问题,因为文本框实际上并未设置为值。它仍然是空的。应该更改的内容,以便将文本框值设置为值或rs

Dim rs As DAO.Recordset
Dim strSQL As String
Dim db As Database

Set db = CurrentDb

strSQL = "Select MAX(pkid)+1 from tblInfo"

Set rs = db.OpenRecordset(strSQL)

Do While Not rs.EOF

    txtID = rs

Loop

2 个答案:

答案 0 :(得分:1)

首先,您尝试将记录集设置为文本框对象。您需要设置文本框的文本/值,您需要访问记录集的字段。

Dim rs As DAO.Recordset
Dim strSQL As String
Dim db As Database

Set db = CurrentDb

strSQL = "Select MAX(pkid)+1 from tblInfo"

Set rs = db.OpenRecordset(strSQL)

Do While Not rs.EOF
    txtID.SetFocus 'set the focus so we can add the text
    txtID.Text = rs.Fields(0).Value
    'txtID.Value = rs.Fields(0).Value 'uncomment out if you don't need focus on the textbox and comment out the previous 2 lines
Loop

答案 1 :(得分:0)

你可以在一行中完成所有这些:

Me!txtID.Value = DMax("pkid", "tblInfo") + 1