Solved

DBGrid - Clicking on a line

Posted on 2004-09-12
9
185 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pull multiple cvs files into one access table 28 65
vb6 connector to SQL Server 2 34
Crystal reports - Formula Field code need assistance with code 17 52
VB6 ListBox Question 4 33
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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

895 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

12 Experts available now in Live!

Get 1:1 Help Now