Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用vba从access中的数据表中检索特定记录_Vba_Ms Access - Fatal编程技术网

如何用vba从access中的数据表中检索特定记录

如何用vba从access中的数据表中检索特定记录,vba,ms-access,Vba,Ms Access,我被困在这个密码里了,你们有人能帮我吗。我有一个“TblParticipants”表,在这里我存储了所有关于编队的数据。每个参与者都有一个特定的ID“ParticipantID”,他们接受的课程也有相应的ID“LessonID”。我想做的是在一个表格中,在无限制的文本框下显示所有参与者参加过的lessonID。我不想将所有匹配的记录存储在一个新表中,但我只想将它们显示出来,而不是用逗号分隔的一个文本框,而是在不同的文本框中 代码如下: Private Sub Form_Current()

我被困在这个密码里了,你们有人能帮我吗。我有一个“TblParticipants”表,在这里我存储了所有关于编队的数据。每个参与者都有一个特定的ID“ParticipantID”,他们接受的课程也有相应的ID“LessonID”。我想做的是在一个表格中,在无限制的文本框下显示所有参与者参加过的lessonID。我不想将所有匹配的记录存储在一个新表中,但我只想将它们显示出来,而不是用逗号分隔的一个文本框,而是在不同的文本框中

代码如下:

   Private Sub Form_Current()
   Dim db As Database
   Dim rs As Recordset
   Dim Arr As String
   Dim i As Integer
   Dim MyQuery As String
        MyQuery = "select *" & "from [TblParticipants]" & "where [ParticipantID] ='" & Me.IDtxtBox.Value & "'"
    Set db = CurrentDb
    Set rs = db.OpenRecordset(MyQuery)
    On Error GoTo ErrorHandler
    While rs.EOF = False
        For i = 0 To rs.RecordCount - 1
            Arr = rs.Fields("LessonID") 'The field where I want to extract all matching records
                                    'with my query and put each matching record in separate text boxes
        'MsgBox Arr   'This message returns one by one what I exactly want but I don't know how to put each of them in a separated text box (unbound records without creating a new table)
            Select Case i
                Case i = 0
                    txtbox1.Value = Arr 'first lessonID the participant have been attending
                Case i = 1
                    txtbox1.Value = Arr '2nd lessonID the participant have been attending
                Case i = 2
                    txtbox1.Value = Arr '3rd lessonID the participant have been attending
                Case i = 3
                    txtbox1.Value = Arr '4th lessonID the participant have been attending
                Case i = 4
                    txtbox1.Value = Arr '5th lessonID the participant have been attending
            End Select
            rs.MoveNext
        Next i
    Wend
        rs.Close
        Set rs = Nothing
        db.Close
        Set db = Nothing
    ErrorHandler:
       MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & 
    Err.Description
    End sub

如果有5个文本框的名称为
txtbox1
txtbox5
,并且查询返回的记录不超过5条,则可以尝试以下操作:

 While rs.EOF = False
    For i = 0 To rs.RecordCount - 1
        Arr = rs.Fields("LessonID") 
        Me.Controls("txtbox" & i + 1).Value = Arr
        rs.MoveNext
    Next i
Wend

如果你想把Arr值放在不同的文本框中,为什么要把它们放在同一个txtbox1中?对不起,我犯了一个错误,应该是txtbox1到txtboxn谢谢你。正是我想要的。