2015-01-02 4 views
0

이것은 MS Access 데이터베이스와 관련이 있습니다.SQL 변수 - MS 액세스

나는 T-SQL에서 변수를 정의하는 것이 가능하다는 것을 알고 있지만 MS Access에서 어떻게 작동합니까?

아래의 VBA 스크립트와 동일한 방법으로 SQL의 변수에 값을 할당 할 수 있습니까?

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 Like [A-Z] [A-Z] [A-Z]'Then '는 일지 여부에 따라 대문자 만 검사합니다. – 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'기능을 수정하여 과거에했던 것처럼 실제로 오류를 테이블에 기록 할 수 있습니다. 이렇게하면 발생하는 오류를 패턴 등을 볼 수있는 분석을 수행 할 수 있습니다.

이 질문이 있은 지 오래되었지만 이것이 도움이 되었기를 바랍니다.