Default control values - something or something else

andy_25

Registered User.
Local time
Today, 23:47
Joined
Jan 27, 2009
Messages
86
I have got a form and depending on what form triggered it to open I want it to take on that primary key.

In the default value of a control on the form that is opened at the moment I have got:

=[Forms]![frmJobs]![JobNo]

I need the same to happen but from a different form, so instead of recreating the same form I was wondering if it was possible to do something like this:

Default value:
=[Forms]![frmJobs]![JobNo] or =[Forms]![frmJobsSearch]![JobNo]

I know that the above code will not work, and I guess I will get the question; What if both forms are loaded at the same time? (they will not be).

Any suggestions guys?
 
You could use the OpenArgs option of DoCmd.OpenForm and pass along the formname and use the OnOpen event of you form to evaluate OpenArgs and set the defaulValue to your control.

DoCmd.OpenFrom "FormName", OpenArgs:= "frmJobs"

Code:
Private Sub Form_Open(Cancel As Integer)
If Me.OpenArgs <> "" Then
  Select Case Me.OpenArgs
            Case "frmJobs"
                  Me.NameOfControl.DefaultValue = "=[Forms]![frmJobs]![JobNo]"
            Case "frmJobSearch"
                   Me.NameOfControl.DefaultValue = "=[Forms]![frmJobsSearch]![JobNo]"
            Case Else
                'Do nothing
    End Select
End If
End Sub

JR
 
Exactly what I was looking for. OpenArgs is something I need to start getting my head around.

Cheers JR
 
Could you have a look at that code again JR and see if it is correct. Something is causing an error and I cannot find what it is.
 
should be nothing wrong in the code, here is the testcode I used in the second form where you want to set the defaultvalue.

Code:
Private Sub Form_Open(Cancel As Integer)
If Me.OpenArgs <> "" Then
    Select Case Me.OpenArgs
        Case "frmJobs"
            Me.[COLOR=red]txtDefault[/COLOR].DefaultValue = "=[Forms]![frmJobs]![JobNo]"
        Case "frmJobsSearch"
            Me.[COLOR=red]txtDefault[/COLOR].DefaultValue = "=[Forms]![frmJobsSearch]![JobNo]"
        Case Else
            MsgBox "wrong"
    End Select
End If
End Sub

txtDefault is the name of the control on this form.

and the calling code from another from to open this form:

Code:
Private Sub Kommando7_Click()
    DoCmd.OpenForm "frmStart", OpenArgs:="frmJobs"
End Sub

When I click on the commandbutton frmStart opens and sets the defaultvalue to the control txtDefault.

Make sure you named which contol you want to manipulate correctly.

JR
 
One thing thou. The Default property only has effect on new records so you should probably add acFormAdd to the opening statement.

Code:
Private Sub Kommando7_Click()
    DoCmd.OpenForm "frmStart", , , , acFormAdd, OpenArgs:="frmJobs"
End Sub

JR
 

Users who are viewing this thread

Back
Top Bottom