Help with DataGrid Control

Posted on 2002-06-23
Last Modified: 2013-12-26
I am using VB6, MDAC 2.5, and my project references Microsoft ActiveX Data Objects 2.0 Library.  (I get errors with 2.5)

I am setting up some forms which I would like to look like Access tables or Excel spreadsheets to display data from databases.  I am therefore using DataGrid controls which use ADO Data controls as their data source to get this look.  I am having a lot of trouble with both these controls however so if anyone can give me a source which discusses how to properly program these controls, or a source of sample code, I would greatly appreciate it.

The question I would really like to be answered here though is this.  When a user enters a value in a cell in the DataGrid, I would like to validate its data type myself before the DataGrid control does it, so that I can return my own error message and cancel the recordset update if the value entered is not compatible with the field type in which it is entered.  I prefer to return my own error messages rather than the cryptic error messages that the DataGrid control returns.  The problem is I cannot access the value entered in the cell by the user in any of the events that are triggered by the user entering a value.  For example, in the event

Private Sub DataGrid1_BeforeColUpdate(ByVal ColIndex As Integer, OldValue As Variant, Cancel As Integer)

the values OldValue, DataGrid1.Columns(ColIndex).Text, and DataGrid1.Columns(ColIndex).Value all contain the same value, the value that is being replaced with the new value the user entered.  They are all the old value.  I cannot get the new value before Cell update is complete.

Interestingly enough, this is not true if I programmatically add values to cells in a newly added row, like in a paste operation I implemented.  The paste function just uses
  DataGrid1.Columns(i).Value = strValue
to enter the values into the cells and the new cell values are validated in event DataGrid1_BeforeUpdate().  If any of the values cannot be converted to the type of the field, a custom message is posted, the update is canceled, and the user can then fix it.  The event, and therefore the validation for the row, will continue to fire each time the user tries to leave the row until all the bad values are fixed.  I would just like to do this for pre-existing rows and cells.  

Question by:ASP_Ray
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
  • 4
  • 3
LVL 43

Expert Comment

ID: 7103168
ASP_Ray, the value you want at this time is:


Referencing this in the BeforeColUpdate event is permissible because .Text refers to the current cell, as you haven't yet left it then it is always the current cell.

As for the errors that you got with MDAC 2.5, I will bet the following:

1) You have Service Pack 3 or lower

2) The error is a mismatch in the event declarations for ADODC1_MoveComplete and similar.

If so the solution is this:

Change ADODB.Recordset to ADODB.Recordset20 in each event declaration that it occurs.


Upgrade to Service Pack 4 or higher.

The reason is that Microsoft (contrary to all known rules) changed the interface of the ADODB.Recordset object between MDAC 2.0 and MDAC 2.1 this means that you either have to use MDAC 2.0 as you have, change the event declaration as suggested or upgrade to Service Pack 4 which contains a newer version of the DataGrid which works with MDAC 2.1 or higher without modification of the event declaration.

Author Comment

ID: 7103599
Thanks for the help TimCottee but it did not work.  In event BeforeColUpdate, DataGrid1.Text has the same value as OldValue.  They are both the value being replaced by the new entry.  I still cannot get the newly entered value.
LVL 43

Expert Comment

ID: 7103635
Interesting, I did a quick test with NWIND.mdb, an ado datacontrol and a datagrid and DataGrid1.Text worked perfectly in this event. I am using MDAC 2.5 and the SP4 version of the datagrid/ado data control so I don't know if this has any effect.
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


Author Comment

ID: 7103714
I believe you.  I suspect there must be a software problem here, perhaps with MDAC because of how quirky this thing is behaving.  Several times an event will not fire and when I restart my computer it does.  Anyway, the highest service pack I can find at MSDN for MDAC 2.5 is MDAC 2.5 SP2 (2.52.6019.2).  Can you suggest anything?  At least I can try upgrading that.

I am using Microsoft ADO Data Control 6.0 (OLEDB), Microsoft DataGrid Control 6.0 (OLEDB), and Component Checker tells me "The MDAC version that is closest to the version on [my] computer is '2.4(4403.3)'".

Author Comment

ID: 7103722
Sorry, that was supposed to be '2.5(4403.3)'.
LVL 43

Accepted Solution

TimCottee earned 100 total points
ID: 7103738
If you already have MDAC 2.5 then upgrading it may not help much, I would prefer upgrading VB from your current service pack to service pack 5 (the latest). This will update both the grid control and the data control to be compatible with MDAC 2.5 and also perhaps fix some other underlying problems with these controls. Unfortunately it is a HUGE download so could take some time.

Author Comment

ID: 7104239
Good news TimCottee, Service Pack 4 for Visual Studio 6 seems to have solved the problem.  I can now get and validate the new value in BeforeColUpdate for when a cell is edited and in BeforeUpdate for when I paste a row.  Thanks!!!

Since this thing has a habit of developing problems with a feature which was working before, let me use it for a little while before I release the points, ok.  Probably tomorrow I will.  In the meanwhile, if you can refer me to a source which has source code for using these controls or discusses how to properly use them I would appreciate it.  Understand though that whether you tell me this or not has nothing to do with me releasing the points.  You seem to have already earned that already.

Featured Post

SharePoint Admin?

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

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

688 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