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

Recommended Answers

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

Jump to Post

All 4 Replies

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.

Member Avatar

the @schoolYear there would be the item that would be added? is it right?

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)
Member Avatar

thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.