2016-12-15 81 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