Solved

DBGrid - Clicking on a line

Posted on 2004-09-12
9
184 Views
Last Modified: 2010-05-02
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.


0
Comment
Question by:chayahd99
  • 5
  • 4
9 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12037604
What is the code in the click  event?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12037606
And exactly which event are you using?
0
 

Author Comment

by:chayahd99
ID: 12037614
Private Sub DBGrid1_Click()
    Dim RecordIndex As String
    Data1.Recordset.FindFirst "MyIndex=" & Data1.Recordset!MyIndex
    RecordIndex = Data1.Recordset("MyIndex")
    MsgBox RecordIndex
End Sub
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 30 total points
ID: 12037636
Your code tries to find the current index, so does not move the current record at all.
But after the click event the current record is moved automatically.
Put this in the Data control event.

Private Sub Data1_Reposition()
    MsgBox Data1.Recordset("MyIndex")
End Sub

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:chayahd99
ID: 12037643
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 ?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12037657
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.
0
 

Author Comment

by:chayahd99
ID: 12037672
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 ?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12037725
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

0
 

Author Comment

by:chayahd99
ID: 12038047
I used a flag but I was hoping there is some smarter way to do this.
Thanks anyway.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

758 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

23 Experts available now in Live!

Get 1:1 Help Now