Link to home
Start Free TrialLog in
Avatar of chayahd99
chayahd99

asked on

DBGrid - Clicking on a line

Hi,

I'm using the DBGrid in my VB application.
I implemented the event that occurs when clicking on a line in the grid.
Just for the test, I put a msgBox that pops up with the record number everytime I click a record.
I noticed that the record number returned is always of the last record I clicked on & not the current one.

For example:
If I click record 3, I get in the msgBox 1 (that's the starting point).
If I click record 5, I get in the msgBox 3.
If I click record 2, I get in the msgBox 5.
And on, and on...

How do I fix this?

Thanks.


Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

What is the code in the click  event?
And exactly which event are you using?
Avatar of chayahd99
chayahd99

ASKER

Private Sub DBGrid1_Click()
    Dim RecordIndex As String
    Data1.Recordset.FindFirst "MyIndex=" & Data1.Recordset!MyIndex
    RecordIndex = Data1.Recordset("MyIndex")
    MsgBox RecordIndex
End Sub
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Let me see if I got this right:

The repositioning is done automatically after each click, so the event I wish to execute should be put into the repositioning function.

Should the line "Data1.Recordset.FindFirst "MyIndex=" & Data1.Recordset!MyIndex" also go in the repositioning function ?
Yes, you don't need to do the repostioning; anything that you want to happen after the position change can be initiated in the _Position() event.

"Data1.Recordset.FindFirst "MyIndex=" & Data1.Recordset!MyIndex" does nothing and what you wanted it to do happens automatically because the grid is bound to the data control.
OK, now it's better.

But one more thing. How do I avoid having the first msgBox displayed when the form is first loaded ?

How can I know if this is the first time "Data1_Reposition()" is being executed ?
You could have a form-level variable to flag it:

Option Explicit
Dim loaded As Boolean

Private Sub Data1_Reposition()
If loaded Then
    MsgBox "Reposition"
End If
loaded = True
End Sub

I used a flag but I was hoping there is some smarter way to do this.
Thanks anyway.