誰能幫我確定這段代碼有什么問題?如果我將表中的 DOB 更改為短文本并將其 DIM 作為字串,則代碼有效。但是,將 DOB 作為日期欄位,將 DIM 作為日期,我收到此錯誤訊息。
“運行時錯誤‘3464’條件運算式中的資料型別不匹配”
當我單擊除錯時,下面加粗的行以黃色突出顯示。
Private Sub DOB_AfterUpdate()
Dim DOB As Date
Dim FirstName As String
Dim LastName As String
Dim stLinkCriteria As String
Dim PIN As Integer
'Assign the entered customer name and address to a variable
NewJII = Me.FirstName.Value
NewJII2 = Me.LastName.Value
NewDOB = Me.DOB.Value
stLinkCriteria = "[FirstName] = " & "'" & NewJII & "' and [LastName] = " & "'" & NewJII2 & "' And [DOB] = " & "'" & NewDOB & "'"
**If Me.FirstName & Me.LastName & Me.DOB = DLookup("[FirstName]",
"TblPersonLog", stLinkCriteria) Then**
MsgBox "This Customer, " & stLinkCriteria & " has already been entered in database." _
& vbCr & vbCr & "with DOB " & NewDOB & "" _
& vbCr & vbCr & "Please check Customer name and Date Of Birth again.", vbInformation, "Duplicate information"
Cancel = True
End If
End Sub
真的很感謝任何指導!這幾天我一直在嘗試解決這個問題。在VBA中不是很強大。
uj5u.com熱心網友回復:
首先,您不能使用DLookup這種方式。其次,如果您想取消,請使用BeforeUpdate事件。第三,正如 Tim 所說,對日期標準使用正確的語法。
因此,它可能是這樣的:
Private Sub DOB_BeforeUpdate(Cancel As Integer)
Dim FirstName As String
Dim LastName As String
Dim NewDOB As Date
Dim stLinkCriteria As String
If Not IsNull(Me!DOB.Value) Then
' Assign the entered customer name and DOB to variables.
FirstName = Nz(Me!FirstName.Value)
LastName Nz(Me!LastName.Value)
NewDOB = Me!DOB.Value
stLinkCriteria = "[FirstName] = '" & FirstName & "' And [LastName] = '" & LastName & "' And [DOB] = #" & Format(NewDOB, "yyyy\/mm\/dd") & "#"
Cancel = Not IsNull(DLookup("[FirstName]", "TblPersonLog", stLinkCriteria))
If Cancel = True Then
MsgBox "This Customer, " & FirstName & " " & LastName & ", has already been entered in the database" _
& vbCr & vbCr & "with DOB " & NewDOB & "." _
& vbCr & vbCr & "Please check Customer name and Date Of Birth again.", vbInformation, "Duplicate information"
End If
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/467240.html
下一篇:提取Outlook收件人地址
