2014-10-17 39 views
1

我的Access數據庫鏈接到我的SQL Server 2008. 我試圖驗證如果輸入到文本框字段(稱爲diverNo)的值是一個數字(只允許在該字段中的數字)。我希望它儘快在用戶離開現場驗證,如果用戶試圖將它不是一個數字的任何字符,現場將被清理:訪問2013年的運行時錯誤2115

Private Sub Form_Error(DataErr As Integer, Response As Integer) 

If DataErr = 2113 Then 
Response = acDataErrContinue 
MsgBox ("Only numbers are allowed in Diver Number") 
Me.diverNo = "" ' <- this line cause the error 

Exit Sub 

End If 
End Sub 

我得到運行時錯誤2115這狀態「設置爲該字段的BeforeUpdate或ValidationRule屬性的宏或函數阻止Microsoft Office Access將該數據保存在該字段中。」

任何建議來解決這個問題?

+0

在'BeforeUpdate'方法中,值不能被改變。 – 2014-10-17 14:43:31

回答

0

我會創造的diverNo領域的更新後事件,並檢查該值:

私人小組diverNo_AfterUpdate()

如果不是則IsNumeric(Me.diverNo)然後

Me.diverNo。撤消或Me.diverNo = NULL

結束如果

結束子

使用您當前的代碼,設置Me.diverNo =「」正試圖在數字字段中保存字符串值。你也可以考慮爲diverNo控件創建一個ValidationRule。

+0

文本框不是數字字段,但除此之外,這可能是最好的方法(因爲用戶希望「清理」該字段。) – 2014-10-17 14:53:02

1

調用Undo方法和設置CancelTrueBeforeUpdate處理程序似乎工作。

Private Sub diverNo_BeforeUpdate(Cancel As Integer) 

If IsNumeric(Me.diverNo) = False Then 
    Cancel = True 'stops the record from being saved. 
    Me.diverNo.Undo 'only the control itself is affected. 

    MsgBox ("Please only enter a number") 
End If 

End Sub 

BeforeUpdate是好的,如果你也想評價一個老VS新的價值正在進入一個字段(綁定字段)。這在AfterUpdate方法中丟失了,因爲記錄已經更新。