폼에 날짜가 있으며 각각을 개별적으로 확인하기 시작했습니다. 이 모든 수표를 각각의 '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로 설정하려면 어떤 식으로 함수를 호출해야합니까?
@Hansup하지 함수의 서명'CheckDate (텍스트 상자로하여 DateField는)'이 불가능 텍스트 상자 이외의를 통과 할 수 있도록 하는가? –
@HansUp 음,'Call CheckDate (xray_date)'가 수행하는 동안'Cancel = CheckDate (xray_date)' 이 아무 것도하지 않는 것을보고 싶었습니다. 어쨌든 그렇게하지 않아도됩니다. –
컨트롤 수준에서 날짜를 확인해서는 안되며 폼의 BefereUpdate 이벤트를 사용하는 것이 좋습니다. 거기에 취소 옵션이 있으며, 저장을 시도 할 때까지 사용자를 괴롭히지 않습니다. 사용자가 컨트롤을 채우는 순서를 제어하지 않는다는 것을 잊지 마십시오! 그는 쥐의 주인이다. –