简体   繁体   English

MS Access 从子表单的组合框中选择下拉菜单后,在主表单中填写 ID

[英]MS Access Fill in ID in the Main Form after select drop down from combo box in Subform

I have similar situation with this link: MS Access automatically fill id in main form when data is entered in subform我有这个链接的类似情况: 当数据输入到子表单时,MS Access自动在主表单中填写id

In short, I wanted to be able to create autonumber ID in the main form when I enter data in subform.简而言之,当我在子表单中输入数据时,我希望能够在主表单中创建自动编号 ID。 This is possible with this code inside sub form:使用子表单中的此代码可以做到这一点:

Private Sub Form_Dirty(Cancel As Integer)
With Me.Parent
    If IsNull(!MainFormID) Then
        ' Change any field to create the parent record
        .Description = "Test"
        ' Save changes on the parent form.
        .Dirty = False
    End If
End With
End Sub

I have pasted the VBA Code and create additional field called "Description" which capture changes in the main form.我粘贴了 VBA 代码并创建了名为“描述”的附加字段,用于捕获主表单中的更改。 This works if I fill in a text box in subform.如果我在子表单中填写文本框,这会起作用。 However, if I select an item from combobox it doesn't work.但是,如果我从组合框中选择一个项目,则它不起作用。

Lets say my combobox is cboSelectItem, I also have tried changing the event to below but to no available.假设我的组合框是 cboSelectItem,我也尝试将事件更改为下面但不可用。

Private Sub cboSelectItem_BeforeUpdate(Cancel As Integer)

Anyway to change the code to be able to run for combo box and still retain my selection from the drop down?无论如何更改代码以便能够运行组合框并仍然保留我从下拉列表中的选择?

You can intercept the On Enter event of the sub form frame and check if the "parent" form has an active record.您可以拦截sub form frameOn Enter事件,并检查“父”表单是否有活动记录。 In this way, you can still enfore relationship between parent and sub form when adding new records to the "parent" form.通过这种方式,您仍然可以在向“父”表单添加新记录时确定父表单和子表单之间的关系。

On the main form select the Sub form frame object (where you usually select link master fields \\ link child fields . That fram should have two events, On Enter & On Exit .在主窗体上选择Sub form frame对象(您通常选择link master fields \\ link child fields 。该框架应该有两个事件, On Enter & On Exit

Use the On Enter event to check if the "parent" form has any record.使用On Enter事件检查“父”表单是否有任何记录。 Note, you are already in the parent forms so parent form = current form .请注意,您已经在父表单中,因此parent form = current form

The event should look like this:该事件应如下所示:

'This code should appear in the parent form's code page
Private Sub SubFormName_Enter()
    On Error Resume Next
    If (Me.NewRecord) Then

        Me!Description = "test"
        Me.Dirty       = False
    End If
End Sub

This should work when you enter the sub form and parent form has no records.当您输入子表单并且父表单没有记录时,这应该有效。 If it's good idea doing this way is up to you to decide.如果这样做是个好主意,则由您决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM