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

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.

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.