pop up message if theirs duplicate number when add or save

KenQui

Member
Joined
May 11, 2022
Messages
12
Programming Experience
Beginner
This the error that always pop up when I input duplicate Number. How can I fix that. I want to pop up a message that a user can understand easily if theirs duplicate number then it will not proceed to save. What is the code for that. Sorry about this question.
1653462577198.png
 
If you want to display a specific error message to the user then do so. Presumably you are currently displaying the Message from the exception you're catching. If that's not what you want then don't do it. Catch the specific exception and then display the specific message you want to display.

That said, you don't even have to allow an exception to be thrown. If you don't want to try to save duplicate data then check whether the data is duplicate first. It's just querying the database, same as always.
 
Sorry about that. Then what code can I add here sir/ma'am
VB.NET:
 Private Sub AddBook()
        ' ADD PARAMETERS
        Access.AddParam("@BNo", BookNo.Text)
        Access.AddParam("@Book_Category", Category.Text)
        Access.AddParam("@Book_Title", Title.Text)
        Access.AddParam("@Book_Stored_Date", DateTimePicker1.Value)
        Access.AddParam("@Copyright_Date", Copyright_Date.Text)
        Access.AddParam("@Remarks", Remarks.Text)
        Access.AddParam("@Quantity", Quantity.Text)

        ' EXECUTE INSERT COMMAND
        Access.ExecQuery("INSERT INTO db_book (Book_No,Book_Category,Book_Title,Book_Stored_Date,Copyright_Date,Remarks,Quantity) " & _
                         "VALUES (@BNo,@Book_Category,@Book_Title,@Book_Stored_Date,@Copyright_Date,@Remarks,@Quantity); ")

        If Not IsNumeric(Me.BookNo.Text) Then
            ErrorProvider1.SetError(BookNo, "PLEASE INPUT A NUMBER ONLY.")
            Return
        Else
            Me.ErrorProvider1.SetError(Me.BookNo, "")
        End If

        If Not IsNumeric(Me.Quantity.Text) Then
            ErrorProvider1.SetError(Quantity, "PLEASE INPUT A NUMBER ONLY.")
            Return
        Else
            Me.ErrorProvider1.SetError(Me.Quantity, "")
        End If


        ' REPORT & ABORT ON ERRORS
        If Not String.IsNullOrEmpty(Access.Exception) Then MsgBox(Access.Exception) : Exit Sub

     
        ' SUCCESS!!
        MsgBox("The book was added successfully")

   
        books.RefreshGrid()

        Me.Close()


    End Sub
 
It's just as I said:
VB.NET:
If Not String.IsNullOrEmpty(Access.Exception) Then MsgBox(Access.Exception) : Exit Sub
You're just displaying the error message from the exception itself. If that's not what you want then don't do that. Look for the specific type of exception you're interested in and then provide the specific error message you want to display.
 
So you're adding book number 1 (or asking the user to enter a book number). Will they know its 1?
Usually, the computer will find a unique number for you. What happens if user A and user B both enter '1' for the book number? What happens if they choose 5, but book 5 already exists?
Databases have a way of making this number unique for you, otherwise, you can find the last number by using 'max' ... but again, this doesn't work well in a multi-user environment - if user A and user B both find the maximum, they're both going to get the answer 5 ... and you'll be snookered again.
There are ways around this too, but the easiest is to forget asking the user to enter a number when adding a record, and let the computer pick a number, then display to the user, 'hey, I picked number 6 for you'.
 
Back
Top