Help with DataGrid Control

Posted on 2002-06-23
Medium Priority
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
  • 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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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 400 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

839 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