• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

Help with DataGrid Control

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.  

  • 4
  • 3
1 Solution
TimCotteeHead of Software ServicesCommented:
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.
ASP_RayAuthor Commented:
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.
TimCotteeHead of Software ServicesCommented:
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.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

ASP_RayAuthor Commented:
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)'".
ASP_RayAuthor Commented:
Sorry, that was supposed to be '2.5(4403.3)'.
TimCotteeHead of Software ServicesCommented:
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.
ASP_RayAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now