G
Guest
Hi,
I have an "insert into query" sql query that runs well (code below). What I
want is to ensure that you cannot be run it twice. The primary key for the
table is a combination key of Year (Year) and CompanyID (CoID). The Insert
Into query inserts the appropriate year (which is used in the primary key).
Hence when it attempts to run it again, an error message appears saying
"Didn't add records due to key violations, Do you want to run this action
query anyway? To ignore the errors and run the query click Yes. (with yes
no & help buttons)".
I want to handle this error/situation and not get this Access created
message and somehow have my own custom message.
Any ideas how to go about this? Thanks for any assistance.
****code starts here****
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
' Turn off screen updating
DoCmd.Echo False
strSQL = "INSERT INTO tblAnalysis (Year, CoID) " & _
"SELECT [Forms]![frm23AdminAnalysisSetUp]![cboPeriodYears],
tblCompany.CoID " & _
"FROM tblCompany " & _
"WHERE
(((tblCompany.LicenceGranted)<=[Forms]![frm23AdminAnalysisSetUp]![txtPeriodEndDate]) AND ((tblCompany.LicenceSurrendered) Is Null)) " & _
"OR
(((tblCompany.LicenceGranted)<=[Forms]![frm23AdminAnalysisSetUp]![txtPeriodEndDate])
AND
((tblCompany.LicenceSurrendered)>=[Forms]![frm23AdminAnalysisSetUp]![txtPeriodStartDate]));"
' Apply the SQL statement to the stored query
Set db = CurrentDb
Set qdf = db.QueryDefs("qry37AdminAnalysisSetUpStoredQuery")
qdf.SQL = strSQL
Set qdf = Nothing
Set db = Nothing
'Open the Query
DoCmd.OpenQuery "qry37AdminAnalysisSetUpStoredQuery"
****code ends here****
I have an "insert into query" sql query that runs well (code below). What I
want is to ensure that you cannot be run it twice. The primary key for the
table is a combination key of Year (Year) and CompanyID (CoID). The Insert
Into query inserts the appropriate year (which is used in the primary key).
Hence when it attempts to run it again, an error message appears saying
"Didn't add records due to key violations, Do you want to run this action
query anyway? To ignore the errors and run the query click Yes. (with yes
no & help buttons)".
I want to handle this error/situation and not get this Access created
message and somehow have my own custom message.
Any ideas how to go about this? Thanks for any assistance.
****code starts here****
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
' Turn off screen updating
DoCmd.Echo False
strSQL = "INSERT INTO tblAnalysis (Year, CoID) " & _
"SELECT [Forms]![frm23AdminAnalysisSetUp]![cboPeriodYears],
tblCompany.CoID " & _
"FROM tblCompany " & _
"WHERE
(((tblCompany.LicenceGranted)<=[Forms]![frm23AdminAnalysisSetUp]![txtPeriodEndDate]) AND ((tblCompany.LicenceSurrendered) Is Null)) " & _
"OR
(((tblCompany.LicenceGranted)<=[Forms]![frm23AdminAnalysisSetUp]![txtPeriodEndDate])
AND
((tblCompany.LicenceSurrendered)>=[Forms]![frm23AdminAnalysisSetUp]![txtPeriodStartDate]));"
' Apply the SQL statement to the stored query
Set db = CurrentDb
Set qdf = db.QueryDefs("qry37AdminAnalysisSetUpStoredQuery")
qdf.SQL = strSQL
Set qdf = Nothing
Set db = Nothing
'Open the Query
DoCmd.OpenQuery "qry37AdminAnalysisSetUpStoredQuery"
****code ends here****