Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse a TabStrip to manage multiple DBGrid controls
KeywordsDBGrid, tab
CategoriesControls, Database
 
The program contains a TabStrip control, two Frames, two DBGrids, and two Data controls. The DBGrids are connected to the Data controls as usual. The DBGrids are contained inside the Frames.

When the form loads, the program positions the Frames so they lie over the TabStrip's display area and hides all of the Frames.

The code then sets the SelectedTab variable to 1 to indicate that this tab is selected. It sets the TabStrip's SelectedItem value to the first tab (to select that tab in the TabStrip) and makes the first frame visible.

 
Private Sub Form_Load()
Const MARGIN = 30
Dim i As Integer
Dim wid As Single
Dim hgt As Single

    Data1.DatabaseName = App.Path & "\CoData.mdb"
    Data2.DatabaseName = App.Path & "\CoData.mdb"

    ' Move all the frames to the same position,
    ' make them all invisible and the same size.
    For i = 1 To ChoiceFrame.UBound
        ChoiceFrame(i).Move _
            ChoiceFrame(0).Left, _
            ChoiceFrame(0).Top, _
            ChoiceFrame(0).Width, _
            ChoiceFrame(0).Height
        ChoiceFrame(i).Visible = False
    Next i
    
    ' Select the first tab.
    SelectedTab = 1
    TabStrip1.SelectedItem = TabStrip1.Tabs(SelectedTab)
    ChoiceFrame(SelectedTab - 1).Visible = True
End Sub
 
When the user clicks on a tab, the program hides the Frame for the previously selected tab, saves the newly selected tab's index, and displays the corresponding Frame.
 
Private Sub TabStrip1_Click()
    ChoiceFrame(SelectedTab - 1).Visible = False
    SelectedTab = TabStrip1.SelectedItem.Index
    ChoiceFrame(SelectedTab - 1).Visible = True
End Sub
 
A typical program might allow the user to select an entry from a DBGrid and click an Open button. In this example, when you click Open the program displays a message telling what type of detail you would provide and giving the first value in the selected record.
 
Private Sub cmdOpen_Click()
    Select Case SelectedTab
        Case 1
            MsgBox "Open Employee form for " & _
                Data1.Recordset.Fields(0)
        Case 2
            MsgBox "Open Department form for " & _
                Data2.Recordset.Fields(0)
    End Select
End Sub
 
Finally, when the user clicks a row, column, or cell in a DBGrid, the program deselects any currently selected row and then selects the clicked row. This highlights the entire row.
 
Private Sub DBGrid1_RowColChange(Index As Integer, LastRow _
    As Variant, ByVal LastCol As Integer)
    With DBGrid1(Index).SelBookmarks
        ' Deselect current selections.
        Do While .Count > 0
            .Remove 0
        Loop

        ' Select the active row.
        .Add DBGrid1(Index).RowBookmark(DBGrid1(Index).Row)
    End With
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated