Solved

Substitute error message when deleting from datagrid

Posted on 2015-01-23
9
68 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
  • 5
  • 3
9 Comments
 
LVL 45

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
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40567336
Mine is VB6
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 45

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 45

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 45

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

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

9 Experts available now in Live!

Get 1:1 Help Now