Solved

Substitute error message when deleting from datagrid

Posted on 2015-01-23
9
69 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 46

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 46

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 46

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 46

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 46

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access 2016 VB code 9 115
VBS file using code from 2nd file (txt or vbs) 4 36
How to set the row selection as it was prior leaving the datagrid in vb6 3 42
MsgBox 4 59
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

786 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