DBGrid - Clicking on a line

Posted on 2004-09-12
Last Modified: 2010-05-02

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?


Question by:chayahd99
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
LVL 76

Expert Comment

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

Expert Comment

ID: 12037606
And exactly which event are you using?

Author Comment

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

LVL 76

Accepted Solution

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


Author Comment

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 ?
LVL 76

Expert Comment

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.

Author Comment

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 ?
LVL 76

Expert Comment

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


Author Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

624 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