I want to set a value of a a new record form field in event procedure

不打扰是莪最后的温柔 提交于 2019-12-14 04:22:27

问题


I want a command button to open a new record of a form with one of its fields to have a value of a combo box from another form.

Inventory is the form name while PRODUCT_CODE is a field name in the form Combo_Product_number is a combobox name of another form.

So I want the form to openwith a product_code value field to have been filled with the value of the combobox.

while the rest of the fields are empty.

I am pretty new to access

Private Sub Command5_Click()

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "FRM_Inventory_A03"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

    DoCmd.GoToRecord , , acNewRec
    Forms!FRM_Inventory_A03!PRODUCT_CODE='& Me!Combo_Product_number & "'"

End Sub

回答1:


I made up 2 forms. In frm1 there is a combobox named Combo_Product_number.

Got a second form named FRM_Inventory_A03.

In frm1 there is a combobox with numbers, and a button named Command5. When clicked, the button will open the form FRM_Inventory_A03 in acFormAdd mode

In FRM_Inventory_A03 there are 3 fields, but one of them is named PRODUCT_CODE

The code of the button is:

Private Sub Command5_Click()
DoCmd.OpenForm "FRM_Inventory_A03", acNormal, , , acFormAdd, acDialog
End Sub

Now, when you open the form FRM_Inventory_A03 to add a new record, you want to assign the value of the combobox Combo_Product_number to control PRODUCT_CODE.

To do this, you need to open the form FRM_Inventory_A03 in Design Mode, select control PRODUCT_CODE and in the properties, data tab, in property DefaultValue, type this:

=[Forms]![frm1]![Combo_Product_number]

This will pass the value of the combobox Combo_Product_number to control PRODUCT_CODE.

Hope you can adapt this to your needs.




回答2:


You can pass the product number to the form's OpenArgs parameter. It's the very last parameter of Variant type and it's optional.

Passing the parameter:

DoCmd.OpenForm stDocName, , , stLinkCriteria, , , Me.Combo_Product_number.Value

Handling the parameter is the Form's Load event. You just need to check for Null to avoid errors.

Private Sub Form_Load()
    If Not IsNull(OpenArgs) Then PRODUCT_CODE.Value = OpenArgs
End Sub


来源:https://stackoverflow.com/questions/57904927/i-want-to-set-a-value-of-a-a-new-record-form-field-in-event-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!