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.
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.
What is the code in the click event?
And exactly which event are you using?
ASKER
Private Sub DBGrid1_Click()
Dim RecordIndex As String
Data1.Recordset.FindFirst "MyIndex=" & Data1.Recordset!MyIndex
RecordIndex = Data1.Recordset("MyIndex")
MsgBox RecordIndex
End Sub
Dim RecordIndex As String
Data1.Recordset.FindFirst "MyIndex=" & Data1.Recordset!MyIndex
RecordIndex = Data1.Recordset("MyIndex")
MsgBox RecordIndex
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 ?
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
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.
"Data1.Recordset.FindFirst
ASKER
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 ?
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
Option Explicit
Dim loaded As Boolean
Private Sub Data1_Reposition()
If loaded Then
MsgBox "Reposition"
End If
loaded = True
End Sub
ASKER
I used a flag but I was hoping there is some smarter way to do this.
Thanks anyway.
Thanks anyway.