0
Try
            DoInsertQuery("If Not Exists(" & _
                          "SELECT * FROM tblSchoolYear " & _
                          "WHERE schoolYear = 'SY " & Trim(txtAddSchoolFrom.Text) & "-" & Trim(txtAddSchoolTo.Text) & "')" & _
                      "INSERT into [tblSchoolYear] ([schoolYear]) VALUES ('SY " & Trim(txtAddSchoolFrom.Text) & "-" & Trim(txtAddSchoolTo.Text) & "')")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

im having a confusion in the part of "if not exists", i just want to know how to put show mgbox if the query found duplicates
i've searched on google but only found how to check if there are duplicates

2
Contributors
4
Replies
22
Views
3 Years
Discussion Span
Last Post by aabbccbryanmark_1
0

For this I'd select the count of matching records:

select count(*) from tblSchoolYear where schoolYear = @schoolYear

If it's greater than 1, you have duplicates and can show a message box. However, that particular query strikes me as producing false positives unless the schoolYear column is largely unique. You'd likely need to add another condition to tie it to a specific student.

0

Well, yes. I used a SQL variable since it's a better idea to use parameters to your query rather than glue everything together directly as in your first example. But you could also replace @schoolYear with your string pasting:

"select count(*) from tblSchoolYear " & _
"where schoolYear = 'SY " & Trim(txtAddSchoolFrom.Text) & "-" & Trim(txtAddSchoolTo.Text)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.