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
Solved

DBGrid - Clicking on a line

Posted on 2004-09-12
9
187 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
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 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
 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

840 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