Solved

Substitute error message when deleting from datagrid

Posted on 2015-01-23
9
70 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA : retain and exclude values based on a config sheet and multiple parameters 5 62
using Access 8 76
Spell Check in VB6 13 127
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 56
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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

821 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