【问题标题】:Changing form from edit mode to new mode run time error 2105将表单从编辑模式更改为新模式运行时错误 2105
【发布时间】: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 因为它们会创建重复值。当我到达最后一个文本框并尝试输入记录时

标签: ms-access ms-access-2010


【解决方案1】:

要么使用表格添加记录:

    lMaxCustNum = DMax("NewCustNum", strTable) + 1
    DoCmd.GoToRecord , , acNewRec
    Me.txtCustNumber = lMaxCustNum
    '...    
    Me.txtFirstName1 = txtSearchFirstName
    Me.txtLastName1 = txtSearchlastName
    Me.txtOrigSignUpDate = Date
    ' Save record.
    Me.Dirty = False

或使用 RecordsetClone:

Dim Records As DAO.Recordset
Set Records = Me.RecordsetClone   

Records.AddNew
Records!CustNumber.Value = lMaxCustNum
' ...
Records!FirstName1.Value = Me!txtSearchFirstName.Value
Records!LastName1.Value = Me!txtSearchlastName.Value
Records!OrigSignUpDate.Value = Date
Records.Update
Records.Close

【讨论】:

  • 谢谢。您的帮助。我仍然收到错误。当我使用第一个解决方案时,我收到运行时错误 3022 您请求的更改不成功,因为它们会创建重复值。
  • 谢谢。您的帮助。我仍然收到错误。当我使用第一个解决方案时,我得到运行时错误 3022 您请求的更改不成功,因为它们会在 Me.Dirty = False 行创建重复值当我尝试第二个解决方案时,我得到错误 3420 对象无效或不再设置在记录中!NewCustNum.Value = lMaxCustNum 行
  • 首先,调试你的值——错误不能是假的。其次,将代码调整为您的实际控件名称和字段名称。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多