MS Access: Enter a specific text in a form using c

2019-07-24 15:34发布

I would like to ask for your help on the following issue in MS Access.

I had created a form "CustomerListF", filled with command buttons for each client. For each button, I had created the following code:

Private Sub cmd_outlets_ABC_Click()
DoCmd.OpenForm "OrderFormF"
Forms!OrderFormF!Outlets = "ABC"
End Sub 

The button would then open another form "OrderFormF" and enter "ABC" in the textbox named "Outlets". However, I realized the second line of code (Forms!OrderFormF!Outlets = "ABC") would always create a phantom record in my subform, which is located in "OrderFormF", and this record would travel to other clients' forms. This phantom record is usually created when the commandbutton is clicked twice (double clicks or subsequent clicks). It is a headache when the record starts to shift around.

enter image description here

I would like to seek your advice for vba code to edit the second line of code.

Thank you.

1条回答
欢心
2楼-- · 2019-07-24 16:30

This approach for filling the field in opened form is not good. OrderFormF initialization and second line of your code with assigning value to the field may be executed in different sequence, they are not synchronized. In order to avoid this, you should pass the argument to OrderFormF by another way. I would recommend to use OpenArgs parameter:

DoCmd.OpenForm "OrderFormF",,,,,,"ABC"

And then in Load event of OrderFormF assign the value to textbox:

Me.Outlets = Me.OpenArgs

In this case the "ABC" value will be assigned to first row in the form or to new record, if form is empty. Before assigning you can select the row if you need to assign value not to the first row. Also this way will protect you against double click on button, Load event executed only once during first form opening

查看更多
登录 后发表回答