2016-12-15 67 views
0

我正在运行此语法查询表并设置一个texbox等于rs问题是文本框没有实际设置为值。它仍然是空的。我应该在这所以文本框的值来改变设置的值或rs访问VBA设置文本框等于查询结果

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 

回答

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

使用'txtID.Value = ...'那么你就不需要'.SetFocus'。 – Andre

+0

@Andre +1为好,我更新了答案 – Sorceri

0

你可以做到这一切在一个单一的线:

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