?
Solved

DataGrid - Multiple lines selection (part 2)

Posted on 2001-09-17
2
Medium Priority
?
989 Views
Last Modified: 2013-12-26
The following code is a sample of multiple lines selection in a datagrid control.

This code gives me one small problem: when you select a line somewhere in the middle of the grid, hold the Shift key and select the last visible line of the grid (the one that is only partly visible), a scroll occurs and a line that is not part of the selection is highlighted.

Anybody can help me on that one?

Option Explicit

Private mconX As ADODB.Connection
Private mblnShift As Boolean
Private mvarBookmark As Variant

' *****************************************************************
' ***
' ***
' ***
' *****************************************************************
Private Sub Command1_Click()
Dim intRow As Integer

    For intRow = 0 To DataGrid1.SelBookmarks.Count - 1
        MsgBox DataGrid1.Columns(0).CellValue(DataGrid1.SelBookmarks(intRow)) & _
               vbCrLf & _
               DataGrid1.Columns(1).CellValue(DataGrid1.SelBookmarks(intRow))
    Next intRow
End Sub

' *****************************************************************
' ***
' ***
' ***
' *****************************************************************
Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
    If Shift = vbShiftMask Then
        mblnShift = True
        mvarBookmark = DataGrid1.Bookmark
    End If
End Sub

Private Sub DataGrid1_KeyUp(KeyCode As Integer, Shift As Integer)
    mblnShift = False
End Sub

Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
    If Not mblnShift Then
        With DataGrid1
            ' Remove previously saved bookmark from collection
            If .SelBookmarks.Count <> 0 Then .SelBookmarks.Remove 0
            ' Append your bookmark to the collection of selected rows
            If Not IsNull(.Bookmark) Then .SelBookmarks.Add .Bookmark
        End With
    End If
End Sub

Private Sub DataGrid1_SelChange(Cancel As Integer)
Dim varBookmark As Variant
   
    With DataGrid1
        If mblnShift Then
            If .SelBookmarks.Count <> 0 Then
                If .Bookmark > mvarBookmark Then
                    For varBookmark = mvarBookmark To .Bookmark
                        .Bookmark = varBookmark
                        .SelBookmarks.Add .Bookmark
                    Next varBookmark
                Else
                    For varBookmark = .Bookmark To mvarBookmark
                        .Bookmark = varBookmark
                        .SelBookmarks.Add .Bookmark
                    Next varBookmark
                End If
                If .Bookmark < mvarBookmark Then
                    mvarBookmark = .SelBookmarks(0)
                Else
                    mvarBookmark = .SelBookmarks(.SelBookmarks.Count - 1)
                End If
            End If
        End If
    End With
End Sub

' *****************************************************************
' ***
' ***
' ***
' *****************************************************************
Private Sub Form_Load()
Dim rstX As ADODB.Recordset

    Set mconX = New ADODB.Connection
    With mconX
        .CursorLocation = adUseClient
        .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Visual Studio\VB98\Nwind.mdb;"
        .Open
    End With
   
    Set rstX = New ADODB.Recordset
    rstX.Open "Select CustomerID, CompanyName, ContactName FROM Customers", mconX, adOpenStatic, adLockReadOnly
   
    With DataGrid1
        Set .DataSource = rstX
        .AllowAddNew = False
        .AllowArrows = True
        .AllowDelete = False
        .AllowUpdate = False
        .RowHeight = 300
        .Columns(0).Locked = True
        .Columns(1).Locked = True
        .Columns(2).Locked = True
    End With
End Sub
0
Comment
Question by:Éric Moreau
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 800 total points
ID: 6487681
Eric, add

Private Sub DataGrid1_SelChange(Cancel As Integer)
Dim varBookmark As Variant
   
   With DataGrid1
       If mblnShift Then
           If .SelBookmarks.Count <> 0 Then
'Begin Change
               If .SelBookmarks(0) > .Bookmark Or .SelBookmarks(0) > mvarBookmark Then
                   .SelBookmarks.Remove 0
               End If
'End Change
               If .Bookmark > mvarBookmark Then
                   For varBookmark = mvarBookmark To .Bookmark
                       .Bookmark = varBookmark
                       .SelBookmarks.Add .Bookmark
                   Next varBookmark
               Else
                   For varBookmark = .Bookmark To mvarBookmark
                       .Bookmark = varBookmark
                       .SelBookmarks.Add .Bookmark
                   Next varBookmark
               End If
               If .Bookmark < mvarBookmark Then
                   mvarBookmark = .SelBookmarks(0)
               Else
                   mvarBookmark = .SelBookmarks(.SelBookmarks.Count - 1)
               End If
           End If
       End If
   End With
End Sub
0
 
LVL 70

Author Comment

by:Éric Moreau
ID: 6487918
Thanks TimCottee.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question