我如何将表格中的列中的所有文本集成到文本框Microsoft Access VBA ms-access

时间:2011-12-27 20:05:19

标签: vba ms-access integration

如何将表单中的列中的所有文本集成到textBox?

此代码可以将表格中的所有文字添加到表单

中的文本框中
Set db = CurrentDb
Set rs = db.OpenRecordset("names")
For i = 1 To rs.RecordCount

text1.SetFocus
text1.Text = text1.Text & " " & rs(1)

rs.MoveNext

Next i

是否可以通过相同的表单将所有文本从表单中添加到文本框中?

2 个答案:

答案 0 :(得分:0)

遍历表单中的所有控件并将其放在字符串变量(或文本框)中。

Dim cControl As Control
Dim sNames As String
sNames = ""
For Each cControl In Me.Controls
    If TypeName(cControl) = "TextBox" Then
        sNames = sNames + " " + cControl
    End If
Next cControl

答案 1 :(得分:0)

将子com1_Click替换为以下块。希望这可以帮助 。 。 。

Private Sub com1_Click()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim strNames As String
    Dim strSQL As String

    ' Following line gets the query of the form
    strSQL = Me.RecordSource
    ' Alternatively, you can replace this line with your own select query
    ' For eg.
    ' strSQL = "Select * from Table1 Where [_ID] In (1,2)"


    Set db = CurrentDb
    Set rs = db.OpenRecordset(strSQL)

    strNames = ""

    'Following loop is for going through all the records
    While Not rs.EOF
        'Following line is for collecting values of Name1 Field of your table
        strNames = strNames & " " & rs("Name1")
        rs.MoveNext
    Wend

    ' Populating the textbox with values collected from your table
    text3 = Trim(strNames)

    ' Final cleanup
    rs.Close
    Set rs = Nothing
    Set db = Nothing
End Sub