SQL变量 - MS Access

时间:2015-01-02 11:48:56

标签: sql variables ms-access

这与MS Access数据库有关。

我知道可以在T-SQL中定义变量,但这可以在MS Access中使用吗?

如何为SQL中的变量赋值,方法与下面的VBA脚本相同? 如您所见,目的是使用以下方式向用户提供错误的描述:

strErrorDescription = strErrorDescription & " " & strErrorDescription2 & " " & strErrorDescription3

    strTableName = "tblStagingGL_SS04"
    strLogTableName = "tblLogGL_SS04"

    Set rs = db.OpenRecordset(strTableName)
    Set rs2 = db.OpenRecordset(strLogTableName)

    rs.MoveFirst

        Do Until rs.EOF

            'Verifies that the GL_Number only contains numbers
            If rs.Fields(2).Value Like "*[!0-9]*" Then
            strErrorDescription = "Error on GL_Number"
            End If

                'Verifies that the Currency is made of 3 numbers only
                If Not rs.Fields(6).Value Like "[A-Z][A-Z][A-Z]" Then
                strErrorDescription2 = "Error on Currency"
                End If

           'Verifies that the Rate does not contain letters
           If rs.Fields(10).Value Like "*[A-Z]*" Then
           strErrorDescription3 = "Error on rate"
           End If

        strErrorDescription = strErrorDescription & " " & strErrorDescription2 & " " & strErrorDescription3

                'If an Error has been caught, create a record with the associated information in the Log table
                If strErrorDescription <> "  " Then

                rs2.AddNew
                rs2.Fields(1).Value = rs.Fields(2).Value
                rs2.Fields(2) = rs.Fields(7)
                rs2.Fields(3) = strErrorDescription
                rs2.Update

                End If

            strErrorDescription = ""
            strErrorDescription2 = ""
            strErrorDescription3 = ""

        rs.MoveNext

        Loop

1 个答案:

答案 0 :(得分:0)

是的,使用函数通过ErrorHandling向用户提供错误消息很容易 - 您需要在ErrorHandling函数中声明值,并引用内置的错误属性。

例如,这是任何表单,报表或其他模块中的函数可用于引用错误处理程序的内容:

Public Function AnyFunctionName() 
    On Error GoTo ErrHandler                   'Initial Error Handling
    'Whatever your function Does goes here

ExitCode:                                      '
    Exit Function                              '
ErrHandler:                                    'The error handler
    ErrorLog Err.Number, Err.Description 
    Resume ExitCode                            'Resume after error handling at exit code to clear memory
End Function

然后这将是错误处理程序函数:

Public Sub ErrorLog(lngErrNum As Long, strErrDesc As String)
    'Purpose:                                           '
On Error GoTo ErrHandler                                'Initial Error Handling
    'Declare                                            '
    Dim strMsg as string
    'Set                                                '
    'Use                                                '
    strMsg = "The following error has occurred:" & vbNewLine
    strMsg = strMsg & "Error # " & lngErrNum & vbNewLine
    strMsg = strMsg & "Description: " & strErrDesc
    MsgBox strMsg, vbOKOnly Or vbExclamation, "Error"

ExitCode:                                               '
    'Clear                                              'clear all variables declared
    Exit Sub                                            '
ErrHandler:                                             'The error handler
    strMsg = "There has been an error with the error handler."
    MsgBox strMsg, vbOKOnly Or vbExclamation, "Error"
    Resume ExitCode                                     'Resume after error handling at exit code to clear memory
End Sub

您当然可以修改错误日志&#39;函数实际上将错误记录到表中,就像我过去所做的那样。这将允许您对任何发生的错误进行分析,以观察模式等。

我知道问题已经有一段时间了,但我希望这有助于某人!