2012-03-23 2 views
1

폼에 날짜가 있으며 각각을 개별적으로 확인하기 시작했습니다. 이 모든 수표를 각각의 'before update'이벤트에서 호출 할 수있는 하나의 함수로 바꾸고 싶었습니다. 문제는 유효성 검사가 실패 할 때 컨트롤에 포커스를 유지할 수 없다는 것입니다.Access VBA get Cancel 속성에 대해 Sub에 데이터를 전달하는 함수

Public Function CheckDate(datefield As TextBox) As Integer 

Dim this_date As Date 
Dim DOB As Date 
Dim first_seen As Date 
this_date = Conversion.CDate(datefield.text) 
DOB = [Forms]![generic]![date_of_birth] 
first_seen = [Forms]![generic]![date_first_seen] 

If Not IsNull(this_date) Then 
    'date of birth must precede any other date 
    If this_date < DOB Then 
     MsgBox "This date precedes the date of birth", vbExclamation, "Invalid date" 
     CheckDate = -1 
     Exit Function 
    End If 
    'date can't be in the future 
    If this_date > DateTime.Date Then 
     MsgBox "This date is in the future", vbExclamation, "Invalid date" 
     CheckDate = -1 
     Exit Function 
    End If 
    'all investigation/treatment dates must be >= date first seen 
    If Not IsNull(first_seen) Then 
     If this_date < first_seen Then 
      MsgBox "This date precedes the date patient was first seen", vbExclamation, "Invalid date" 
      CheckDate = -1 
      Exit Function 
     End If 
    End If 
End If 

End Function 

Private Sub xray_date_BeforeUpdate(Cancel As Integer) 

내에서 나는 시도했다 : 올바른 메시지를 표시하지만, 이동 편집을 위해 거기를 유지하는 대신 컨트롤에서 초점

Call CheckDate(xray_date) 

.

Cancel = CheckDate(xray_date) 

은 무언가를 수행하는 것으로 보이지 않으므로 잘못된 데이터를 저장 장치로 전달할 수 있습니다. 그렇다면 유효성 검사가 실패 할 때 BeforeUpdate의 Cancel 이벤트를 True로 설정하려면 어떤 식으로 함수를 호출해야합니까?

+0

@Hansup하지 함수의 서명'CheckDate (텍스트 상자로하여 DateField는)'이 불가능 텍스트 상자 이외의를 통과 할 수 있도록 하는가? –

+0

@HansUp 음,'Call CheckDate (xray_date)'가 수행하는 동안'Cancel = CheckDate (xray_date)' 이 아무 것도하지 않는 것을보고 싶었습니다. 어쨌든 그렇게하지 않아도됩니다. –

+0

컨트롤 수준에서 날짜를 확인해서는 안되며 폼의 BefereUpdate 이벤트를 사용하는 것이 좋습니다. 거기에 취소 옵션이 있으며, 저장을 시도 할 때까지 사용자를 괴롭히지 않습니다. 사용자가 컨트롤을 채우는 순서를 제어하지 않는다는 것을 잊지 마십시오! 그는 쥐의 주인이다. –

답변

2

샘플 코드를 이해하는 데 어려움을 겪었으므로 날짜/시간 필드가있는 테이블을 만들었습니다. date_of_birth; date_first_seen; 및 xray_date. 그런 다음 해당 텍스트 상자가 해당 필드에 바인딩 된 테이블을 기반으로 양식을 작성했습니다. txtDate_of_birth; txtDate_first_seen; 및 txtXray_date.

이것은 내 양식의 코드 모듈이며 AFAICT는 txtXray_date의 유효성을 검사합니다.

Option Compare Database 
Option Explicit 

Private Function CheckDate(ctlDate As TextBox) As Integer 
    Const clngChecks As Long = 3 ' change this to match the number 
           ' of conditions in the For loop 
    Const cstrTitle As String = "Invalid date" 
    Dim i As Long 
    Dim intReturn As Integer 
    Dim lngButtons As Long 
    Dim strPrompt As String 
    Dim strTitle As String 

    lngButtons = vbExclamation 
    strPrompt = vbNullString ' make it explicit 
    intReturn = 0 ' make it explicit 

    For i = 1 To clngChecks 
     Select Case i 
     Case 1 
      'date of birth must precede any other date 
      If ctlDate < Me.txtDate_of_birth Then 
       strPrompt = "This date precedes the date of birth" 
       Exit For 
      End If 
     Case 2 
      'date can't be in the future 
      If ctlDate > DateTime.Date Then 
       strPrompt = "This date is in the future" 
       Exit For 
      End If 
     Case 3 
      'all investigation/treatment dates must be >= date first seen 
      If ctlDate < Me.txtDate_first_seen Then 
       strPrompt = "This date precedes the date patient was first seen" 
       Exit For 
      End If 
     End Select 
    Next i 

    If Len(strPrompt) > 0 Then 
     MsgBox strPrompt, lngButtons, cstrTitle 
     intReturn = -1 
    End If 
    CheckDate = intReturn 
End Function 

Private Sub txtXray_date_BeforeUpdate(Cancel As Integer) 
    Cancel = CheckDate(Me.txtXray_date) 
End Sub 
+1

나는 같은 결론에 도달했다. 어쩌면 OP가 디버그 -> 컴파일을했다면 약간의 불빛을 흘릴 수도 있습니다. –

+0

@HansUp : 내 기능을 당신의 것으로 대체했지만 여전히 어려움이 있습니다. 먼저 Me 키워드를 잘못 사용했다는 메시지가 나왔기 때문에 txtDate_of_birth가 정의되지 않았으므로 [Forms]! [form]! [txtDate_of_birth]로 변경했습니다. 그러나 그것은 정확히 내 함수가 메시지 상자를 표시하지만 다음 컨트롤에서 폼의 다음 컨트롤로 이동되고 잘못된 날짜가 테이블에 저장된 않았다 작동합니다. VBA 사용에 익숙하지 않은 원본 코드가 혼란스러운 것 같습니다. – Chelle

+0

'Me' 키워드를 잘못 사용했다는 오류는 그 코드를 폼의 코드 모듈 대신 다른 모듈에 저장했다는 것을 의미합니다. 나는 왜 '나'가 무효가 될지 전혀 생각할 수 없다. – HansUp

관련 문제