2

I've inherited a script that runs and inserts records from one database into another database.

Everything appeared to be working fine until a few days ago when people complained that a record wasn't being pulled from one system to the other system.

I looked at the macro which was set to run with a scheduled task on Windows Server 2003. The macro sends it's logged output to SQL Server, and so I went there and looked for the appropriate log statements which read that one employee had been inserted into the database. However, when going to the other system, the employee that was reportedly transferred and inserted, was not there.

Upon further inspection pulled just the fields that were being selected from the one database, to the insert statement which added them to the other database.

When I tried to insert these values manually I received an SQL error that the fields were not allowed to be NULL or a zero length string. However, despite error checking in the script,

Function SomeFunc
   On Error GoTo The_Err

   DoCmd.SetWarnings False ' This statement seems a bit suspect to me...maybe fields being
                           ' null or a zero length are just warnings?  Seems wrong because
                           ' I don't want a warning when my record will not be inserted, 
                           ' I want an error.

   ....

   DoCmd.OpenQuery "qryAddNewEmp_S1toS2", acViewNormal, acAdd ' This is the statement that
                                                              ' selects from the one db
                                                              ' and inserts into the other db

...

The_Exit:

    Exit Function

The_Err:
    ' MsgBox Error$ ' You can turn this on and off.
    dmsg = "Error AUCP"
    RunCheck dmsg, errArea, 0, 0
    DoCmd.SetWarnings True
    Resume The_Exit

End Function

Are the warnings turned off from DoCmd.SetWarnings False preventing the errors from being logged? Also should I just be doing the validation in the code instead, checking each of the values? It doesn't seem right to me, it seems like that's something that should be the responsibility of the database, since the database has that built in.

4

1 回答 1

0

而不是DoCmd.OpenQuery,使用这样的方法......

Dim db As DAO.Database
Set db = CurrentDb
db.Execute "qryAddNewEmp_S1toS2", dbFailOnError
Set db = Nothing

那么您将没有理由SetWarnings关闭 ( False)。您的INSERT错误处理程序代码将捕获失败,并将记录错误(假设RunCheck是进行日志记录的自定义过程)。

于 2012-08-28T13:31:45.553 回答