【问题标题】:DoEvent() Returns 0 BUT Run-time Error 2585 This action can't be carried out while processing a form or report eventDoEvent() 返回 0 但运行时错误 2585 处理表单或报表事件时无法执行此操作
【发布时间】:2018-05-19 18:30:23
【问题描述】:

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

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

我调用 DoEvent() 以响应此错误,但它返回零。我也等待 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 

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

    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

知道我在这里缺少什么吗?谢谢。

【问题讨论】:

  • DLookup() 在我看来是不完整的。您希望 DLookup 搜索什么标准?虽然看起来不是错误的原因,但它看起来不像是一个有效的表达式。 acSaveYes 与保存数据无关,它是关于保存设计更改。试试 acSaveNo。
  • 另外,打开其他表单后可能需要关闭表单。建议您将 GoToRecord 代码放在打开的表单后面。
  • 您要关闭的表单是您运行此代码的表单吗?为什么要关闭然后重新打开表单?
  • @ErikvonAsmuth,是的。我关闭了表格并重新打开它,因为我不知道如何完全清除表格以获取新记录。在我看来,必须有一种迭代方式来遍历表单字段并清除它们。

标签: ms-access vba ms-access-2016


【解决方案1】:

这行代码

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

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

你应该使用

If Me.Dirty Then Me.dirty = False 

如果有任何数据发生变化,则强制保存当前记录。

【讨论】:

    【解决方案2】:

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

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

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

    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,但我的方法与此处建议的方法不同,并且不需要第二个对象来保存引用和管理副本。

    【讨论】:

    • Erik,你的建议不太正确,但让我找到了错误。我表单中的最后一个字段有一个名为 SaveData() 的 LostFocus() 过程 - 上面的过程 - 从而在我尝试关闭表单时创建了一个打开事件。 .我将其更改为 AfterUpdate() 并解决了问题!感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多