DoEvent() Returns 0 BUT 运行-时间错误 2585 处理表单或报表事件时无法执行此操作

DoEvent() Returns 0 BUT Run-time Error 2585 This action can't be carried out while processing a form or report event

此代码 运行 运行顺利,但现在出现错误 2585。

我看过 and Gord Thompson's answer,但除非我遗漏了什么(很有可能!)第一个不起作用,第二个似乎不适用。我在另一个网站上看到一条建议,可能存在重复的记录 ID,但我检查了这种可能性。

我调用了 DoEvent() 以响应此错误,但它 returns 为零。我也等10秒让其他进程运行。仍然收到错误。

Private Sub SaveData_Click()
  Dim myForm As Form
  Dim myTextBox As TextBox
  Dim myDate As Date
  Dim myResponse As Integer

If IsNull(Forms!Ecoli_Data!DateCollected.Value) Then

  myReponse = myResponse = MsgBox("You have not entered all the required data. You may quit data entry by hitting 'Cancel'", vbOKOnly, "No Sample Date")
  Forms!Ecoli_Data.SetFocus
  Forms!Ecoli_Data!Collected_By.SetFocus
  GoTo endOfSub
End If

If Me.Dirty Then Me.Dirty = False

myDate = Me.DateCollected.Value

Dim yearAsString As String, monthAsString As String, dayAsString As String, clientInitial As String

Dim reportNumberText As String

reportNumberText = Me!SampleNumber.Value
Debug.Print "reportNumberText = " & reportNumberText

Debug.Print "CollectedBy Index: " & Me!Collected_By & " Employee Name: " & DLookup("CollectedBy", "Data_Lookup", Me.Collected_By)


Dim whereString As String
whereString = "SampleNumber=" & "'" & reportNumberText & "'"
Debug.Print whereString
On Error GoTo errorHandling
DoCmd.OpenReport "ECOLI_Laboratory_Report", acViewPreview, , whereString
DoCmd.PrintOut
DoCmd.Close acReport, "ECOLI_Laboratory_Report", acSaveNo

Dim eventsOpen As Integer
eventsOpen = DoEvents()
Debug.Print "Number of Open Events = " & DoEvents()

    Dim PauseTime, Start, Finish, TotalTime

        PauseTime = 10    ' Set duration.
        Start = Timer    ' Set start time.
        Do While Timer < Start + PauseTime
            DoEvents    ' Yield to other processes.
        Loop
        Finish = Timer    ' Set end time.
        TotalTime = Finish - Start  ' Calculate total time.
    myResponse = MsgBox("Processing Report Took " & TotalTime & " seconds.", vbOKOnly)


myResponse = MsgBox("Do you want to add more data?", vbYesNo, "What Next?")


If myResponse = vbYes Then

    DoCmd.Close acForm, "ECOLI_Data", acSaveYes 

错误由上面的行生成并且无论对 MsgBox 的响应是 Y 还是 N 都会发生。

    DoCmd.OpenForm "ECOLI_Data", acNormal, , , acFormAdd
    DoCmd.GoToRecord , , acNewRec
Else
    DoCmd.Close acForm, "ECOLI_Data", acSaveYes

End If
Exit Sub

 errorHandling:
 If Err.Number = 2501 Then
   myResponse = MsgBox("Printing Job Cancelled", vbOkayOnly, "Report Not Printed")
ElseIf Err.Number = 0 Then
   'Do nothing
Else
   Debug.Print "Error Number: " & Err.Number & ": " & Err.Description

   myResponse = MsgBox("An Error occurred: " & Err.Description, vbOKOnly, "Error #" & Err.Number)
End If


If Application.CurrentProject.AllForms("ECOLI_Data").IsLoaded Then DoCmd.Close acForm, "ECOLI_Data", acSaveNo

If Application.CurrentProject.AllReports("ECOLI_Laboratory_Report").IsLoaded     Then DoCmd.Close acReport, "ECOLI_Laboratory_Report", acSaveNo

endOfSub:
End Sub

知道我在这里遗漏了什么吗?谢谢。

这行代码

`DoCmd.Close acForm, "ECOLI_Data", acSaveYes`  

不保存您所在的记录,它只保存对表单设计的任何更改。

你或许应该使用

If Me.Dirty Then Me.dirty = False 

如果有任何数据更改,则强制保存当前记录。

我无法重现该问题,但以下内容可能会有所帮助:

我假设您 运行 遇到了麻烦,因为您在同一操作中关闭和打开表单。为避免这样做,您可以打开表单的第二个副本,并在第二个副本打开后关闭表单。这样就避免了这个问题。

要打开表格的第二个副本:

Public Myself As Form

Public Sub CopyMe()
    Dim myCopy As New Form_CopyForm
    myCopy.Visible = True
    Set myCopy.Myself = myCopy
End Sub

(CopyForm为表格名称)

关闭可能是也可能不是由此函数创建的表单的表单

Public Sub CloseMe()
    If Myself Is Nothing Then
        DoCmd.Close acForm, Me.Name
    Else
        Set Myself = Nothing
    End If
End Sub

可以找到有关打开同一表单的多个变体的更多信息 here,但我的方法不同于此处建议的方法,并且不需要第二个对象来保存引用和管理副本。