从访问数据库中提取多个记录

时间:2016-04-26 20:26:27

标签: vb.net oledb access

我在Access中有两个表:

[Groups]  
GroupID - GroupName - GroupDesc - Members
[Users]  
UserID - UserName - .... - GroupID

一个VBA GUI,它有一个Combobox(填充了组名)和listbox(填充了所有UserNames)。

我正在尝试让列表框突出显示所有具有与Combobox中所选组相匹配的GroupID的用户名。我已经得到它突出显示匹配的第一个用户名,但无法弄清楚如何让它循环遍历数据库并找到与查询匹配的所有用户名。

这是我到目前为止所做的:

Private Sub adminGroupsGroupNameCmbox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles adminGroupsGroupNameCmbox.SelectedIndexChanged
    Dim mainConnection As OleDbConnection = New OleDbConnection
    mainConnection.ConnectionString = connString

    Using mainConnection
        mainConnection.Open()
        Dim str As String
        str = "SELECT * FROM [Groups] WHERE (GroupName = '" & adminGroupsGroupNameCmbox.Text & "')"
        Dim cmd As OleDbCommand = New OleDbCommand(str, mainConnection)
        Dim dr As OleDbDataReader = cmd.ExecuteReader

        adminGroupGroupLstbx.ClearSelected()

        Using cmd
            Using dr
                While dr.Read()

                    adminGroupsGroupNameTxt.Text = dr("GroupName").ToString
                    adminGroupsGroupDescTxt.Text = dr("GroupDesc").ToString

                    'input name, get ID
                    Dim GrabbedGroupID As String = GrabGroupID(adminGroupsGroupNameTxt.Text)
                    '

                    str = "SELECT * FROM [Users] WHERE (GroupID = '" & GrabbedGroupID & "')"
                    Dim cmd2 As OleDbCommand = New OleDbCommand(str, mainConnection)
                    Dim dr2 As OleDbDataReader = cmd2.ExecuteReader

                    Using cmd2
                        Using dr2
                            While dr2.Read()

                                Dim Username As String = dr2("UserName").ToString

                                ' Ensure we have a proper string to search for.
                                If Username <> String.Empty Then
                                    Dim idx As Integer
                                    While idx <= adminGroupGroupLstbx.Items.Count

                                        ' Find the item in the list and store the index to the item.
                                        Dim index As Integer = adminGroupGroupLstbx.FindStringExact(Username)
                                        ' Determine if a valid index is returned. Select the item if it is valid.
                                        If index <> ListBox.NoMatches Then
                                            adminGroupGroupLstbx.SetSelected(index, True)
                                        Else
                                            MessageBox.Show("The search string did not find any items in the ListBox that exactly match the specified search string")
                                        End If 'index <> ListBox.NoMatches

                                        idx = idx + 1
                                    End While 'idx <= adminGroupGroupLstbx.Items.Count

                                End If 'Username <> String.Empty

                            End While 'dr2.Read()
                        End Using 'dr2
                    End Using 'cmd2

                End While 'dr.Read()
            End Using 'dr
        End Using 'cmd
    End Using 'mainConnection
End Sub

0 个答案:

没有答案