简体   繁体   中英

MS Access: Pass Value from Form Textbox to Another Form Combo Box on a New Record?

Okay, bare with me, this is a really hard question to describe, and I'm fairly new to VBA.

I have a 'Customer Form' with a Command Button to create a New Order in an 'Order Form'. I can get the button to Open the 'Order Form' to a New Record, but I can't get it to pass the 'CustomerID' to the 'Order Form' Combo Box 'CustomerID'. I need the 'Order Form' to auto-populate with information from the 'Customer Form'.

Customer Form:
客户表格

Order Form:
订单
The Customer Combo Box shows the Customer Full Name, but is bound to the CustomerID

Here's the VBA Code I have tried:

Private Sub BtnNewOrder_Click()
    DoCmd.OpenForm FormName:="Order Form", View:=acNormal, OpenArgs:=Me.CustomerID.Value
    DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub Form_Open(Cancel As Integer)
    If Not IsNull(Me.OpenArgs) Then
        Me.CustomerID = Me.OpenArgs
    End If
End Sub

I get the following error:
错误信息

I've been browsing the web for days without any luck. I'm sure this question has been asked before, but I haven't been able to find the right solution. Feel free to simply point me in the right direction.

You have to use the forms on-load event. The on open event is too soon.

On Open event - allows testing of controls and values - you can set cancel = true and form will not load. So, on open is for checking/testing if you want to let the form open, based on custom code that checks things. If you don't want the form to load - you can set Cancel = true.

On Load event: Here is where your setup code, set variables, possible set controls etc. is to occur. So you can't modify controls in on-open - too soon of a event, but you CAN modify the forms controls and values in the on-load event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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