【发布时间】:2018-05-19 18:30:23
【问题描述】:
此代码运行顺利,但现在出现错误 2585。
我查看了Gustav's answer 和Gord 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