Hi,
I have written the following procedure which prevents duplicate records from being inserted into a table. This works 100%, but was wondering if there were any other (more efficient?) ways of doing this. I am aware that this could be done with table definitions, but not quite sure of the user-friendlyness of this approach.
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQLrs As String
Dim strSQL As String
strSQLrs = "SELECT SiteID,ContactDetailsID FROM tblDramatic " _
& "WHERE (((tblDramatic.SiteID)=" & gblSiteID & ") AND " _
& "((tblDramatic.ContactDetailsID)=" & Me.txtContactDetailsID & ")); "
'Create connection.
Set cnn = CurrentProject.Connection
'Create recordset
Set rs = cnn.Execute(strSQLrs)
'Check to see if there are any records
If rs.EOF And rs.BOF Then
strSQL = _
"INSERT INTO tblDramatic (ContactDetailsID, SiteID)" & _
" SELECT '" & Me.txtContactDetailsID & "' AS ContactDetailsID," & _
"'" & gblSiteID & "' AS SiteID;"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
Me.Requery
Else
MsgBox "This record already exists"
End If
'Tidy up
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing
I have written the following procedure which prevents duplicate records from being inserted into a table. This works 100%, but was wondering if there were any other (more efficient?) ways of doing this. I am aware that this could be done with table definitions, but not quite sure of the user-friendlyness of this approach.
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQLrs As String
Dim strSQL As String
strSQLrs = "SELECT SiteID,ContactDetailsID FROM tblDramatic " _
& "WHERE (((tblDramatic.SiteID)=" & gblSiteID & ") AND " _
& "((tblDramatic.ContactDetailsID)=" & Me.txtContactDetailsID & ")); "
'Create connection.
Set cnn = CurrentProject.Connection
'Create recordset
Set rs = cnn.Execute(strSQLrs)
'Check to see if there are any records
If rs.EOF And rs.BOF Then
strSQL = _
"INSERT INTO tblDramatic (ContactDetailsID, SiteID)" & _
" SELECT '" & Me.txtContactDetailsID & "' AS ContactDetailsID," & _
"'" & gblSiteID & "' AS SiteID;"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
Me.Requery
Else
MsgBox "This record already exists"
End If
'Tidy up
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing