【发布时间】:2021-10-29 06:26:51
【问题描述】:
我正在尝试创建一个可以搜索名字和姓氏的表单。如果找到记录,则编辑找到的记录。如果没有找到记录,则创建一个新记录。
搜索现有记录非常有效。当我找不到记录并尝试将表单模式切换到新建并创建新记录时,我当前收到运行时错误 2105 You can't go to the specified record。我已经为此工作了大约一个星期,但无济于事。我希望我能得到一些帮助。
Private Sub cmdSearch_Click()
Dim strFixID As String
Dim lMaxCustNum As Long
Dim lMaxId As Long
Set frmCurrentForm = Screen.ActiveForm
Stop
DoCmd.SetWarnings False
strTable = "tblMarketBulletinNew"
Me.FilterOn = False
Me.Filter = "[FirstName1] like '" & Me.txtSearchFirstName & "*' AND [LastName1] like '" &
Me.txtSearchlastName & "*'"
Me.FilterOn = True
If Me.Recordset.recordCount > 0 Then
'If record exists
DoCmd.GoToRecord , , acFirst
'Change Default value for Action for existing record
Me.txtOrigSignUpDate.Locked = True
Me.txtAction.Locked = False
Me.txtExpirationDate.Locked = True
Else
'No record exists so create new record
Me.FilterOn = False
Me.DataEntry = True
Me.AllowEdits = True
Me.AllowAdditions = True
Me.AllowDeletions = True
'To prevent error the changed you were requested were unsucessful
lMaxId = DMax("ID", strTable) + 1
strFixID = "ALTER TABLE " & strTable & " ALTER COLUMN ID COUNTER(" & lMaxId & ",1);"
Debug.Print strFixID
'DoCmd.RunSQL strFixID
lMaxCustNum = DMax("NewCustNum", strTable) + 1
Me.Recordset.AddNew
Me.txtCustNumber = lMaxCustNum
Me.SetFocus
DoCmd.GoToRecord , , acNewRec
Me.txtAction.DefaultValue = "New"
Me.txtAction.Locked = True
Me.lblRenSignupDate.Visible = False
Me.txtrenSignupDate.Visible = False
Me.txtStatus.DefaultValue = "Active"
Me.txtExpirationDate.Locked = True
'Add First and Last name already entered
Me.txtFirstName1 = txtSearchFirstName
Me.txtLastName1 = txtSearchlastName
Me.txtOrigSignUpDate = Date
End If
DoCmd.SetWarnings True
End Sub
【问题讨论】:
-
在哪一行抛出异常?
-
另外,我假设此代码与您的表单相关联?也就是“我”指的是形式?
-
DoCmd.gotorecord, , acNewrec
-
是的,我就是现在的形式
-
如果我注释掉 DoCmd.GoToRecord , , acNewRec 行,我会收到错误消息 The changes you request to the table was not successful 因为它们会创建重复值。当我到达最后一个文本框并尝试输入记录时