Microsoft Access - Record Form Load

2019-07-21 16:27发布

问题:

I am curious whether it is possible that when i load a record in one of my forms and i choose to add a quote for that record selected, so i choose add quote (button) which takes me to my quote page. i would then like the form to auto load the record i had previously selected in the other form.

Client Form:

Quote Form: Here is the data flow: Record Selected ("Add Job") > Click "Add Items" button > "Items List" loads > the record i previously selected in "Add Job" is then auto loaded.

the feild that will need loading are "Project ID" & "Client name"

回答1:

Use OpenArgs argument of the DoCmd.OpenForm method


  • When you click Add Quotes button to open Quotes form, send the details/linked ID via openArgs parameter.
  • In Quotes form load event, you can get the details passed using Me.OpenArgs

See below sample code


On Add Quotes button click

Private Sub AddQuotes_Click()

    DoCmd.OpenForm "frmQuotes", OpenArgs:=me.ClientID

End Sub

Quotes form

Private Sub Form_Load()
    Dim varArgs

    varArgs = Me.OpenArgs

    'Fill the controls with recordset data
    If Not IsNull(varArgs) Then
        With CurrentDb.OpenRecordset("SELECT * FROM tblClients WHERE ClientID = " & varArgs, dbOpenForwardOnly )
                Me.ClientID = !ClientID
                Me.ClientName = !ClientName
                Me.ClientAddress = !ClientAddress
                Me.ClientPhone = !ClientPhone
                Me.ClientEmail = !ClientEmail
            .Close
        End With
    End If

End Sub