[否当前记录"在acCmdSaveRecord错误时更新前被取消 [英] "No Current Record" error on acCmdSaveRecord when BeforeUpdate is cancelled
问题描述
我用 RunCommand acCmdSaveRecord
保存记录,而我试图使用表单的更新前
事件验证某些领域,我不希望被留空在保存记录之前。
I'm using RunCommand acCmdSaveRecord
to save a record, and I'm trying to use the form's BeforeUpdate
event to validate certain fields that I don't want to be left blank before the record is saved.
我有点新的使用更新前
所以我不知道如果我正确地使用它;这里是我的更新前
code:
I'm a bit new to using BeforeUpdate
so I'm not sure if I'm using it correctly; here's my BeforeUpdate
code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.[First Name]) Or IsNull(Me.LastName) Or IsNull(Me.DateOfBirth) Or IsNull(Me.CourseStartDate) Then
MsgBox "Before the enrolment can be saved, you must provide:" & vbCrLf & vbCrLf _
& "- First Name" & vbCrLf _
& "- Last Name" & vbCrLf _
& "- Date of Birth" & vbCrLf _
& "- Start Date" & vbCrLf & vbCrLf _
& "You must also attach a course. This can be done by selecting " _
& "the appropriate course in the Prospectus Search and clicking " _
& "the Use Prospectus Code button." & vbCrLf & vbCrLf _
& "If your course is not currently in the prospectus, you can " _
& "add it first by clicking the Add Course button.", vbOKOnly Or vbInformation
Cancel = True
Else
Me!EnrolmentID = "Enr" & Format(Me!ID, String(12 - Len("Enr"), "0"))
End If
End Sub
所以这基本上是测试以查看是否某些领域已经留空,如果已经离开,然后一个消息框,显示出哪些字段需要的数据,然后更新被取消空白。否则,它分配一个自定义的主键,并允许要更新的记录。
So this basically tests to see if certain fields have been left blank, if they have been left blank then a message box is displayed showing what fields need data and then the update is cancelled. Otherwise it assigns a custom primary key and allows the record to update.
它抛出取消虽然指出没有当前记录,并强调了 RunCommand acCmdSaveRecord
线code更新时发生错误。
It's throwing an error when cancelling the update though stating "No Current Record" and highlighting the RunCommand acCmdSaveRecord
line of code.
什么是我需要做的,以避免错误?我也试过 Me.Dirty =假
,但仍然得到同样的错误。
What do I need to do to avoid the error? I've also tried Me.Dirty = False
, but still get the same error.
@Johnny骨骼:
下面是一个简单的测试,我已经试过你给我的答案是:
Here's a simplified test I've tried on the answer you gave me:
Public ShouldRun As Boolean
Private Sub Command7_Click()
If ShouldRun = True Then
MsgBox ShouldRun
RunCommand acCmdSaveRecord
End If
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.Field1) Or IsNull(Me.Field2) Then
MsgBox "This is the true"
ShouldRun = False
Else
MsgBox "This is the false"
ShouldRun = True
End If
End Sub
基本上没有任何的 MSGBOX
功能射击和记录被保存,无论现场是否被留空与否。我想这是一个考验,因为 MSGBOX
在我真正的code没有射击前事件要么。不明白为什么不过,如果用户离开的空白,这应该引起if语句在更新前事件的真实部分领域之一......吧?
Basically none of the MsgBox
functions are firing and the record is being saved regardless of whether a field is being left blank or not. I tried this as a test because the MsgBox
in my real code was not firing in the BeforeUpdate event either. Can't see why though.. if a user leaves one of the fields blank this should trigger the true part of the if statement in the BeforeUpdate event, right?
我唯一能想到的是,前事件未通过 RunCommand acCmdSaveRecord
触发,尽管的 MSDN说:
The only thing I can think of is that the BeforeUpdate event is not being triggered by RunCommand acCmdSaveRecord
, despite MSDN saying:
如果你再移动到另一条记录或保存记录,窗体的前事件确实发生了。
"if you then move to another record or save the record, the form's BeforeUpdate event does occur."
不知道该命令是在这里什么...没有VBA运行 RunCommand acCmdSaveRecord
那么前事件?如果是这种情况,那么在 ShouldRun
变量将不会有任何的分配在第一步骤( RunCommand acCmdSaveRecord
),因为它只是变得,在第二(更新前)。
Not sure what the order is here... does VBA run the RunCommand acCmdSaveRecord
then the BeforeUpdate event? If this is the case then the ShouldRun
variable won't have any assignment at the first step (RunCommand acCmdSaveRecord
) because it only gets that at the second (BeforeUpdate).
推荐答案
我会设置一个全局变量是这样的:
I would set a global variable like this:
Global ShouldRun as Bool
您扔了,在最高层的code页,最多在这里声明选项显式。然后,在的if / then / else语句声明你的上方,替换行:
You throw that at the very top of the code page, up where you declare Options Explicit. Then, in the If/Then/Else statement you provided above, replace the line:
Cancel = True
与
ShouldRun = False
和else部分添加行
and in the Else part add the line
ShouldRun = True
最后,您的命令按钮的OnClick事件应该看起来更像是这样的:
Finally, your command button's OnClick event should look more like this:
If ShouldRun = True then
RunCommand acCmdSaveRecord
End If
通过这样做,它甚至不会尝试保存记录,如果它不应该。
By doing this, it will never even try to save the record if it shouldn't.
这篇关于[否当前记录"在acCmdSaveRecord错误时更新前被取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!