Solved

Substitute error message when deleting from datagrid

Posted on 2015-01-23
9
73 Views
Last Modified: 2015-02-09
in my datagrid a user has the ability to delete a row.  However, some rows are tied to a relational database and the error pops up "the delete statement conflicted with the reference constraint "xxxxx"  the conflict occurred in database "xxxx"......."

This happens when a record is tied to a foreign key.  I would like a different error message to come up such as "This records contains hour entries.  The record will not be deleted"

This new error message should happen when a person tries to delete a record from the datagrid and the previous error comes up.  

How can I do that?
0
Comment
Question by:al4629740
[X]
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
  • 5
  • 3
9 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40567301
At the top of the sub put

On error GoTo Error_Routine

' Your code

Exit Sub

Error_Routine
    If err.Number =  ??? Then ' what ever error number you are getting
        MsgBox "Your message"
        Resume Next
    End if

Open in new window

0
 
LVL 24

Expert Comment

by:mankowitz
ID: 40567310
The error is generated by the datasource, not by the datagrid (i.e. the datagrid doesn't care, it's the underlying database that is upset). The error sounds like sql server -- is that what you are using?

This should get you started:

Private Sub dataGridView1_DataError(ByVal sender As Object, _
    ByVal e As DataGridViewDataErrorEventArgs) _
    Handles dataGridView1.DataError

    ' If the data source raises an exception when a cell value is
    ' commited, display an error message.
    If e.Exception IsNot Nothing  Then
        ' At this point you should check the exception to see what kind of error it is
        MessageBox.Show("record contains hour entries")
    End If

End Sub


EDIT: oh, sorry. Are you using VB.NET or plain VB. My answer is for VB.NET
0
 

Author Comment

by:al4629740
ID: 40567323
I'm using vb6.  Correct.  It is from sql datasource
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 47

Expert Comment

by:Martin Liss
ID: 40567336
Mine is VB6
0
 

Author Comment

by:al4629740
ID: 40567354
Martin
Can I isolate it to only trigger off of the attempted grid deletions instead of all errors
0
 
LVL 47

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40567359
mankowitz may be right but if not then something like this

Error_Routine
    If err.Number =  ??? Then ' what ever error number you are getting
        If ActiveControl.Name = ???Then ' the name of your grid
            MsgBox "Your message"
            Resume Next
        End If
    End if

Open in new window

0
 

Author Comment

by:al4629740
ID: 40571776
Martin,

What error number would this message be?

error
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40571785
I don't know but if you put Msgbox err.number as the first line in the error routine it will tell you. That assumes that you are getting to the error routine.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40599587
I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…

724 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