2015-01-02 120 views
0

這與MS Access數據庫有關。SQL變量 - 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 
+0

不知道很多關於MS Access的知識,但是您應該爲此使用存儲過程。此外,請注意'如果不是rs.Fields(6).Value就像「[A-Z] [A-Z] [A-Z]」然後'只檢查大寫字母(取決於您的排序規則)。 – HoneyBadger

+0

在MS Access中沒有存儲過程... – ProtoVB

+0

@ProtoVB您可以在Access中進行傳遞查詢以執行存儲過程 –

回答

0

是,使用功能,通過提供一個錯誤信息給用戶ErrorHandling很容易實現 - 您需要在ErrorHandling函數中聲明這些值,並引用內置的錯誤屬性。

例如,這是一個功能上的任何窗體,報表,或在另一個模塊中可以使用引用的ErrorHandler:

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 

然後這將是的ErrorHandler功能:

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 

當然,您可以修改'ErrorLog'函數以將錯誤實際記錄到表格中,就像我過去所做的一樣。這將允許您對任何發生的錯誤進行分析,以觀察模式等。

我知道自從問這個問題以來已經有一段時間了,但我希望這有助於某人!