Solved

controlling dbgrid

Posted on 1998-10-06
26
258 Views
Last Modified: 2013-12-25
hi..
just like to ask if there's any way to scroll 2 grid boxes simultaneously...i can get it to scroll down but not up..
0
Comment
Question by:wenchang
  • 10
  • 5
  • 3
  • +4
26 Comments
 
LVL 3

Expert Comment

by:TheAnswerMan
ID: 1496730
I assume you are using the same record.. or atleast the key in the tables are the same..

you can just do

sub Grid1_RowColChange
  On error resume next
  Grid2.bookmark = Grid1.Bookmark
end sub


0
 
LVL 3

Expert Comment

by:TheAnswerMan
ID: 1496731
actually with some grids.. you may have to use this to change the rows..

sub Grid1_RowColChange
  On error resume next
  Grid2.Row = Grid1.Row
end sub
0
 

Author Comment

by:wenchang
ID: 1496732
i afraid your suggestion cannot work....
i'm using 2 different tables for the two grids....

0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 3

Expert Comment

by:TheAnswerMan
ID: 1496733
I am confused.. you can get what to scroll down but not up.

You have two different tables.. and two different grids..

what do you want to happen in each grid
0
 

Author Comment

by:wenchang
ID: 1496734
i'll try to explain it more clearly.....
i have 2 grid boxes and 1 grid to display data from 1 table and the other grid to display data from another table..
if i use,
sub grid1_scroll()
   grid2.scroll 0, 1
end sub
i can get the 2 grids to scroll down together...but if i try to scroll up, grid2 will still be scrolling down....
hope it's more clear now...thanxx
0
 
LVL 1

Expert Comment

by:raygibbins
ID: 1496735
Try
   grid2.scroll 0, -1
to scroll up
I realise this will not solve your whole problem
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1496736
Add two timer1 to your form. Disable both timers (enabled = false) and set interval to 10.
Now add the following code.

Private Sub DBGrid1_Scroll(Cancel As Integer)
    Timer1.Enabled = True
End Sub

Private Sub DBGrid2_Scroll(Cancel As Integer)
    Timer2.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Timer1.Enabled = False
    DBGrid2.FirstRow = DBGrid1.FirstRow
End Sub

Private Sub Timer2_Timer()
    Timer2.Enabled = False
    DBGrid1.FirstRow = DBGrid2.FirstRow
End Sub
0
 

Author Comment

by:wenchang
ID: 1496737
oops...can't work....
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1496738
How so, can't work?? I don't understand
0
 

Author Comment

by:wenchang
ID: 1496739
the error message is "invalid bookmark"
0
 

Author Comment

by:wenchang
ID: 1496740
what's the timer for anyway???
0
 

Author Comment

by:wenchang
ID: 1496741
what's the timer for anyway???
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1496742
It works with the biblio database
try it without the timer.
the scroll event is send before the scroll is executed. that means that firstrow still contains the old value. You need the new value. That what the timer does. It allows the value to be updated.
Just try my solution in a simple project.
Add a data control, two timers and two grid controls to a form.
Bind the grids to the data control and see that it works.

0
 
LVL 3

Expert Comment

by:vbWhiz
ID: 1496743
You can solve this by using VB's built in scrollbar.

Add 2 scrollbars to your form.
add the following code to your form:

Private Sub VScroll1_Change()
DBGrid1.Scroll 0, VScroll1.Value - VScroll2.Value
DBGrid2.Scroll 0, VScroll1.Value - VScroll2.Value
VScroll2.Value = VScroll1.Value

End Sub

Private Sub VScroll2_Change()
DBGrid1.Scroll 0, VScroll2.Value - VScroll1.Value
DBGrid2.Scroll 0, VScroll2.Value - VScroll1.Value
VScroll1.Value = VScroll2.Value

End Sub


You can then align the scrollbars to the right of your grids & 
turn off the internal scrollbar of the grids.

to have the scrollbar act more like the original you can also change the vScroll1.Max & the vScroll2.Max properties to equal the number of records in the grids.

I hope this works for you.
0
 

Expert Comment

by:varsha2
ID: 1496744
in the scroll event of one dbgrid, set the toprow properties of the two girds the same
here is an example

Private Sub dbGrid1_Scroll()
    dbGrid2.TopRow = dbGrid1.TopRow
End Sub

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1496745
toprow is not a property of dbgrid. He probably means firstrow and firstrow is only set after the scroll event.
0
 

Author Comment

by:wenchang
ID: 1496746
thanxx vbWhiz....
now how do i give u the points....
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1496747
vbWhiz, how do you now when to update the max-values of the scrollbar and what if you scroll using the cursor keys or home and end.
0
 

Author Comment

by:wenchang
ID: 1496748
wait a minute...i forgot to ask this..
i use rdoResultsets in my application and i sometimes gets this "maximun open cursors exceeded"...what does this means...

0
 
LVL 3

Expert Comment

by:vbWhiz
ID: 1496749
I don't know about that error message specifically but there is some good info on cursors in the help file (RDO cursors) & (CursorDriver) you can modify the cursor style RDO uses using CursorDriver.

If your question is answered I can post my comments as an answer.
0
 

Author Comment

by:wenchang
ID: 1496750
ok....i'll go read up the help file...thanxx
think that's about it for this question.....u can post your comments as  an answer n i can give u the points..
0
 
LVL 5

Expert Comment

by:AnswerTheMan
ID: 1496751
I made that once with 4 listboxes scrolling togheter. can't
find the code now, but i remember that i had a problem, and finally - i disabled 3 of the 4, so the user could scroll
only with One scrollbar. but they all moved simulatly . if u want - i can look for the code in the old HardDisk, but have to
put him in the machine first..........
0
 

Author Comment

by:wenchang
ID: 1496752
it's alright...vbWhiz has already solved my problem...thanxx anyway....
0
 
LVL 5

Expert Comment

by:AnswerTheMan
ID: 1496753
does it work in a situation when the user scroll in one
scrollbar and stop scrollimg whitout letting the mouse button off ? i suppose u have a mismach at that moment.
0
 
LVL 3

Accepted Solution

by:
vbWhiz earned 200 total points
ID: 1496754
You can solve this by using VB's built in scrollbar.

Add 2 scrollbars to your form.
add the following code to your form:

Private Sub VScroll1_Change()
DBGrid1.Scroll 0, VScroll1.Value - VScroll2.Value
DBGrid2.Scroll 0, VScroll1.Value - VScroll2.Value
VScroll2.Value = VScroll1.Value

End Sub

Private Sub VScroll2_Change()
DBGrid1.Scroll 0, VScroll2.Value - VScroll1.Value
DBGrid2.Scroll 0, VScroll2.Value - VScroll1.Value
VScroll1.Value = VScroll2.Value

End Sub


You can then align the scrollbars to the right of your grids & 
turn off the internal scrollbar of the grids.

to have the scrollbar act more like the original you can also change the vScroll1.Max & the vScroll2.Max properties to equal the number of records in the grids.

I hope this works for you.

0
 

Expert Comment

by:varsha2
ID: 1496755
try the following, this code is written assuming dbgrid1 will scroll responding to dbgrid2

'in the scroll event of dbgrid2, write the following code. This 'is to call or post the dbgrid1_scroll event, here we should 'post the dbgrid1 scroll event so that dbgrid2 is completely 'refreshed after the scroll
Private Sub DBGrid2_Scroll(Cancel As Integer)
     PostMessage DBGrid1.hwnd, 276, 0, 0
End Sub

'In the dbgrid1_scroll event write the following code
Private Sub DBGrid1_Scroll(Cancel As Integer)
    'set the row od dbgrid2 to 0
    DBGrid2.Row = 0
    'get the bookmark of the first row
    nextbookmark = DBGrid2.GetBookmark(DBGrid2.Row)
   
    'get number of rows in dbgrid1
    Data1.Recordset.MoveLast
    rows1 = Data1.Recordset.RecordCount
    Data1.Recordset.MoveFirst

    'get number of rows in dbgrid2
    Data2.Recordset.MoveLast
    rows2 = Data2.Recordset.RecordCount
    Data2.Recordset.MoveFirst
       
    'scroll to 1st row of dbgrid2
    For i = 1 To rows2
        DBGrid2.Scroll 0, -1
        DBGrid2.Row = 0
    Next
   
   'scroll to 1st row of dbgrid1
    For i = 1 To rows1
        DBGrid1.Scroll 0, -1
        DBGrid1.Row = 0
    Next
   
    ' for each row in dbgrid2 check if the current row is same   'as the stored bookmark, if not scroll both the grids
    For i = 1 To rows2
        DBGrid2.Row = 0
        If DBGrid2.GetBookmark(0) = nextbookmark Then
            Exit For
        Else
            DBGrid2.Scroll 0, 1
            DBGrid1.Scroll 0, 1
        End If
    Next
   
End Sub
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

839 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