Solved

DataGrid - Multiple lines selection (part 2)

Posted on 2001-09-17
2
968 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
2 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 200 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 69

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now