我将数据从frmSearchEmployeeWorksheets发送到运行查询的frmStatsCorr(qryStatsCorr)。在frmStatsCorr上我正在检查以确保查询返回记录,否则我将Msg用户并返回搜索表单。我的问题是我遇到了'忽略'由 DoCmd.OpenForm(“frmStatsCorr”)引起的2501问题,这是我在Stackoverflow上学到的......
我做错了什么导致我主要的Access VBA挫折?
这是搜索表单上的子目录(frmSearchEmployeeWorksheets):
Private Sub btnSearch_Click()
' I only change focus to force the updated data to submit to query
Me.[txtEmployee].SetFocus
Me.txtShift.SetFocus
If txtUnit = "7" Then
'First close the form in order to update
DoCmd.Close acForm, "frmStatsCorr"
' Open Stats form
On Error GoTo myErr
**DoCmd.OpenForm ("frmStatsCorr") 'causes error**
End If
myExit:
Exit Sub
myErr:
Echo True
If Err.Number = 2501 Then GoTo myExit
MsgBox Err.Description
GoTo myExit
End Sub
在frmStatsCorr中我只是检查以确保查询返回记录,如果不是我通知用户,关闭表单,并返回到frmSearchEmployeeWorksheets
Private Sub Form_Load()
If strFormStatus = "view" Then
If DCount("*", "qryStatsCorr") = 0 Then
MsgBox "Your search does not produce any results. Try a different search.", vbOKOnly
DoCmd.Close
DoCmd.OpenForm ("frmSearchEmployeeWorksheets")
Exit Sub
End If
txtDay = WeekdayName(Weekday(Me.WorkDate)) 'This line returns an error so I check for an empty query and return to the search form.
Me.[WorkDate].SetFocus
Me.txtUnit.Enabled = False...
答案 0 :(得分:1)
我不确定我对您的代码或其背后的逻辑有多了解。我的预感是你应该检查来自DCount
的{{1}}结果,而不是关闭然后重新打开btnSearch_Click
,并且当frmStatsCorr
不包含任何数据时让frmStatsCorr
关闭。只是在它不包含数据时不要打开frmStatsCorr
。
如果包含您的frmSearchEmployeeWorksheets
程序的当前表单(btnSearch_Click
)包含未保存的数据更改,您可以使用Me.Dirty = False
Private Sub btnSearch_Click()
Dim strPrompt As String
If Me.Dirty Then ' unsaved data changes
Me.Dirty = False ' save them
End If
If Me.txtUnit = "7" Then
If DCount("*", "qryStatsCorr") = 0 Then
strPrompt = "Your search does not produce any results. " & _
"Try a different search."
MsgBox strPrompt, vbOKOnly
Else
' if frmStatsCorr is open, just Requery
' else open frmStatsCorr
If CurrentProject.AllForms("frmStatsCorr").IsLoaded Then
Forms("frmStatsCorr").Requery
Else
DoCmd.OpenForm "frmStatsCorr"
End If
' uncomment next line to close current form
'DoCmd.Close acForm, Me.Name
End If
End If
End Sub
如果frmStatsCorr
已打开,您需要检查它是否在设计视图中,请检查其CurrentView
属性。
Forms("frmStatsCorr").CurrentView ' Design View = 0
我建议这种做法,因为我怀疑frmStatsCorr
的{{1}}可能会在关闭时触发2501错误。但我不确定这是错误的原因,而且我没有足够的动力去设置测试。
如果您建议的方法仍然有2501个错误,我还会遇到另外两个可能的原因: