子窗体阻止Recordset.AddNew(错误3027)

时间:2019-05-08 15:00:03

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

具有一个子窗体来显示记录集中使用的同一表,会使该表变为只读状态(错误3027)。摆脱子窗体可以解决此问题。类似的设置也可以在我的其他表单界面中使用。

我试图删除子窗体,该子窗体解决了可访问性问题。但这会破坏最初的UX目的。

Option Explicit
Public UnitRS As DAO.Recordset
Public ModelRS As DAO.Recordset
Public NameUnitRecords As String
Public NameModelRecords As String

Public Sub Form_Load()
    'Initialization
    NameUnitRecords = "tblBatteriesMainRecordsUnits"
    NameModelRecords = "tblBatteriesRecordsModels"
End Sub

Private Sub SetUnitRecordsets()
    'Set the path to the Battery Records table
    Set UnitRS = CurrentDb.OpenRecordset(NameUnitRecords, dbOpenDynaset)
End Sub

Private Sub txtBatteryID_AfterUpdate()
    'Set the recordset path for unit records
    Call SetUnitRecordsets

    'do a findfirst search for the Battery ID, using value from textbox txtBatteryID
    UnitRS.FindFirst "[Battery ID]=" & txtBatteryID

    'If no matching record, leave the other fields empty
    If UnitRS.NoMatch Then
    'If there is a matching record, then, grab the model number
    Else
        'as there is an existing record with model number, run a search on the model number and grab the model info
    End If
    'close recordset
    UnitRS.Close
    'check if the button can be enabled
End Sub

Private Sub cmbModelNumber_AfterUpdate()
    'Set the recordset path for model records    
    'do a findfirst search for the Model Number, using value from combobox cmbModelNumber
    'If no matching record, leave the other fields empty
    If ModelRS.NoMatch Then
     'If there is a matching record, then, grab the Model Info
    Else
    End If
    'close recordset 
    'check if the button can be enabled
End Sub

Private Sub btnSaveAndCLear_Click()
    Dim Response, strOldModelNumber

    'Set the recordset path for unit records
    Call SetUnitRecordsets
    'Set the recordset path for model records
    'close all related tables, queries and forms
    DoCmd.Close acTable, NameUnitRecords, acSaveYes
    DoCmd.Close acForm, "frmSubBatteriesMainRecordsUnits", acSaveYes

    'If a new model record is required
    ModelRS.FindFirst "[Model Number]='" & cmbModelNumber & "'"
    If ModelRS.NoMatch Then
        'msg box confirm model information
        'If user chooses yes
        If Response = vbYes Then
            'create new model record
            'this block could be done with "With...End" format for less error vulerability?
            'nah, unless it is repetitively called, it's too much work to inplement just the .addnew part
            'requery the two combobox to reflect newest changes
        'User chooses no
        Else
            'popup a message telling the user the battery and model record is not logged, but don't clear the field.
            Exit Sub
        End If
    End If
    'need to find the record first, otherwise it will edit the first one
    UnitRS.FindFirst "[Battery ID]=" & txtBatteryID
    'store the old model number value
    strOldModelNumber = UnitRS("Model Number")
    'New record
    If UnitRS.NoMatch Then
        'create a new battery record
        UnitRS.AddNew
        UnitRS("Battery ID") = Me.txtBatteryID
    'If this is an edit on existing record
    Else
        'if the new value is the same as the old one
        If strOldModelNumber = cmbModelNumber Then
            'msgbox the same value, no change is done to the database
            MsgBox "the data is the same as the old record, no change is made to the record", vbInformation, "Same data"
            Exit Sub
        Else
            'msg box confirm edit
            Response = MsgBox("Please confirm edit on existing record: " & Chr(13) & Chr(10) & "BatteryID: " & txtBatteryID & Chr(13) & Chr(10) & "Model Number: " & cmbModelNumber, vbYesNo, "New Model Record Dectected")
            'If user chooses yes
            If Response = vbYes Then
                'goto edit mode
                UnitRS.Edit
            'if user chooses no
            Else
                'msgbox notify the user nothing is changed in the database
                MsgBox "Battery and Model record not logged, you may retry logging", vbInformation, "Record not logged"
                Exit Sub
            End If
        End If
    End If
    'both changes the model number and comment field anyway
    UnitRS("Model Number") = Me.cmbModelNumber
    UnitRS("Comment") = Me.txtComment
    'commit update
    UnitRS.Update
    UnitRS.Close

    'clear all flieds
    'reset all locks    
    'requery the sub form to reflect the newest changes
    Me.subFrmBatteryRecords.Requery
End Sub

我想要实现的是在窗体界面中显示一个显示实际记录表的内容,以便用户知道表中的内容。

1 个答案:

答案 0 :(得分:0)

我的代码没有错。表单对象中有一个属性选项,称为“记录锁定”,我的某种方式选择了“所有记录”。通过将其设置为“无锁”,可访问性问题就解决了。

Form Property Sheet