Solved

Datagrid event call problem

Posted on 2003-11-07
13
584 Views
Last Modified: 2013-12-26
Hi,

I'm using datagrid to show a set of data. The datagrid datasource is from an ado data control (adodc) which recordsource is from an SQL statement declared on form load.

e.g :

datagrid1.datasource = Adodc1
querys1 = "select * from employee"
adodc1.recordsource = querys1
adodc1.refresh

With this I have been able to populate the datagrid accordingly. Now.. to my problems.

I need to have a messagebox pop up everytime a user try to change the value of a column in the datagrid when the "add data" button was not clicked beforehand. No Problem with that.. a messagebox would appear everytime user change the value of a column in the datagrid, by using the beforecolupdate event call.

However, on every execution of the beforecolupdate event call, the new value that the user entered would be replaced by the old value again. Hence, the new value entered are not stored anywhere, and even though the message box appear and user choose to update the data, old data would still appear instead of new data.

My question : is there any event call that comes before beforecolupdate ? Because I have try using the afterColEdit event call but that doesn't get executed before the beforecolupdate event call, and therefore, I couldn't store the old value for further use when doing updating.

Any help please ?

Thanks a lot.
0
Comment
Question by:ayufans
13 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 9709592
Oldvalue is passed into the procedure
Private Sub DataGrid1_BeforeColUpdate(ByVal ColIndex As Integer, OldValue As Variant, Cancel As Integer)
0
 
LVL 1

Author Comment

by:ayufans
ID: 9711769
Hi GrahamSkan,

I'm asking whether there's an event call that can be used BEFORE the beforecoldupdate procedure ? because whenever I ran this procedure, oldvalue would automatically replace the new value, therefore, no update can be performed. Any other suggestions please ??

Thanks
0
 
LVL 1

Author Comment

by:ayufans
ID: 9712064
Hi,

I've come up with a solution for my problem. I ended up using the aftercolupdate event call. Storing the new value when this event was called, and then refreshing the datagrid, and stored the oldvalue after the datagrid are filled with old value from the database. After having both value stored on a variable, I then called the update procedure.

Thanks anyway.

I'll have this question posted up on the cleanup TA.

Thanks.
0
 
LVL 1

Author Comment

by:ayufans
ID: 9712076
Hi,

I have found a solution to my problem. Instead of using the beforecolupdate event call, I use the aftercolupdate event call. After the datagrid is updated (but the database is not yet updated) I store the new value on a variable upon calling the aftercolupdate event call. I then refresh the datagrid causing the old value to show up on the datagrid. Once I did this, I then store the oldvalue in another variable and call the update procedure after that.

Thanks anyway. I'll have this question posted up for deletion on the Community Support TA.

Thanks
0
 
LVL 1

Author Comment

by:ayufans
ID: 9712079
Yikes... page error when submitting first comment. After reload still have no new comment, so I posted new comment and the previous one shows up. Sorry.

Thanks
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 9712716
None from me. Thought because ayufans' solution seems a bit tortuous, I think I still do not understand the question.

Why not make Cancel = True to keep the old data (which could be displayed in the message box)?

If MsgBox("Do you really want to change the data in column " & ColIndex & " from " & OldValue, vbYesNo) = vbYes then
   Cancel = False
Else
   Cancel = True
End If
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:ayufans
ID: 9712849
Hi GrahamSkan,

Never thought of using Cancel = True before... :p how does that work anyway ?? I really have no clue about it at all.

Anyway.. here's the whole thing... I'm going to explain it at the best way I can, sorry if there's anything you don't understand because english is not my first language.

I have a datagrid which are populated with ADODC. A query are then binded to that adodc, which results in data filling in the datagrid according to that query.

If a user click the "Add" button, the datagrid.tag properties would have the value of "add", and therefore the datagrid_aftercolupdate event call won't be called, because it look something like this (example is for datagrid2, I have multiple datagrid in one form) :
-------------------------------------------------------------------------------------------------------------------------------------------
Private Sub DataGrid2_AfterColUpdate(ByVal ColIndex As Integer)
    If DataGrid2.Tag <> "add" Then
        i = DataGrid2.Col
        Label41.Tag = DataGrid2.Columns(i).Text
        Label42.Tag = i
        Label43.Tag = DataGrid2.Row
               
        querys2 = "select * from booking_detail where no_bl = '" & Combo2.Text & "' and item = 'T' and exp_imp = '" & Left(Combo1.Text, 1) & "'"
        Adodc2.RecordSource = querys2
        Adodc2.Refresh
        Label12.Tag = "yes"
        Label44.Tag = DataGrid2.Columns(i).Text
       
        Call mnUpdCol_Click
    End If
End Sub
-------------------------------------------------------------------------------------------------------------------------------------------
as you can see.. if the datagrid.tag properties doesn't have the value of "add" nothing would be done. This is because, the user may add data to the datagrid, and after finished inputting data, the update procedure would be called by the click of the "update" button. This update procedure is called "mnUpdCol_Click".

However, I would also like the same procedure to be used, when a user wants to perform update/correction on the data that has been previously entered. By not clicking the "add" button, user would then made changes on the column and if he/she didn't clicked the "update" button, I would call the aftercolupdate button like the one above. As you can see, I store the new value on label41.tag properties, and after refreshing the datagrid, I store the old value (taken from the database which has not yet been updated) on label44.tag properties. After I have both values, I would then call the "mnUpdCol_Click" procedure. On this procedure (which is way too long to post, because it is almost 17 pages long when being copy-pasted in a word document) I would then ask user if they're really sure that they want to update the column or not. If yes, I would update the data with new value, which is stored in the label42.tag properties. If the user choose no, then I would reupdate the database with the old value just to be sure. And that's it.

Previously, instead of using the aftercolupdate event call, I would use the beforecolupdate event call. However, if I use this event call, even before any coding on the event was executed, old value would replace new value on the datagrid itself. e.g :
On column1, the old value is "aaaaa". Once I changed the value of that column into "aaaab", the beforecolupdate event call would be triggered. Previously, this is what the beforecolupdate event looks like :
-------------------------------------------------------------------------------------------------------------------------------------------
Private Sub DataGrid2_BeforeColUpdate(ByVal ColIndex As Integer, OldValue As Variant, Cancel As Integer)
    If DataGrid2.Tag <> "add" Then
        i = DataGrid2.Col
        Label41.Tag = DataGrid2.Columns(i).Text
        Label42.Tag = i
        Label43.Tag = DataGrid2.Row
        Label44.Tag = OldValue
       
        Call mnUpdCol_Click
    End If
End Sub
-------------------------------------------------------------------------------------------------------------------------------------------
As you can see, I store new value on label41.tag properties and old value on label44.tag properties. Now.. even though I have changed the value of the column on the datagrid, whenever the beforecolupdate event was called, old data would replace new data. In this example the value of "aaaab" would be replaced by "aaaaa" and therefore label41.tag value would be the same as label44.tag value, and even though I call the column update procedure afterwards, there would seem to be no changes made, even though actually on the database old value do get replaced by new value, but because it is the same, the value remain at "aaaaa".

Now, by putting the coding that are previously located on beforecolupdate event to the aftercolupdate event, I can achieve what I initially want, and can perform the update correctly.

I know that this is not the best way of updating the datagrid, but this is the method I use for all of the datagrid in my application. I'm still a very amateur VB programmer and am not developing fast enough, but this way do work for me.

GrahamSkan : your idea of using cancel = True did not occur to me, because like I've said.. I'm pretty much a beginner and therefore do not know a lot of better method compared to the one I'm using.

If there's anything that is not clear, I'd be glad to clarify.

Thanks a lot.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 9712901
It might take me a while to get a full understanding, but meanwhile you might like to know that I thought that English WAS your first language, and I am English (and fussy about the use of language). I couldn't understand why you go up so early in the morning. (It's 08.24 here).
0
 
LVL 1

Author Comment

by:ayufans
ID: 9712995
GrahamSkan : No it's not ^_^ It's either my second or third language. I can't decide :p And it's like 4 PM (16.00) like here, so it's almost in the evening already ^_^ The time here is +7 GMT ^_^ I'm in the office banging my brain hahaha

Have a good one.

Thanks
0
 

Accepted Solution

by:
SpazMODic earned 0 total points
ID: 9735220
PAQed, with points refunded (250)

SpazMODic
EE Moderator
0
 
LVL 1

Author Comment

by:ayufans
ID: 9736462
Hi SpazMODic,

Thanks a lot.
0
 

Expert Comment

by:zorica329
ID: 12164520
Hello,

Users of application I am coding would prefer a button DELETE on the form instead of just pressing key delete.  I am working with data grids and every time I select a row and press KEY DELETE row is deleted.  How can I call event that fires when I press KEY DELETE from btnDelete_Click(....)In other words how can I call events from other events.

Thanks,

Zorica329
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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

747 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

14 Experts available now in Live!

Get 1:1 Help Now