Solved

Help with DataGrid Control

Posted on 2002-06-23
7
293 Views
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
  Adodc1.Recordset.AddNew
    and...
  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.  

So please tell me, HOW CAN I ACCESS THE NEW VALUE THE USER ENTERED BEFORE THE UPDATE EVENT IS COMPLETE??
0
Comment
Question by:ASP_Ray
  • 4
  • 3
7 Comments
 
LVL 43

Expert Comment

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

Datagrid1.Text

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.

Or

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.
0
 
LVL 1

Author Comment

by:ASP_Ray
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.
0
 
LVL 43

Expert Comment

by:TimCottee
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.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Author Comment

by:ASP_Ray
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)'".
0
 
LVL 1

Author Comment

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

Accepted Solution

by:
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.
0
 
LVL 1

Author Comment

by:ASP_Ray
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now