SetDefault

R

Rebecca_SUNY

Using Access 2007

I am trying to use setdefault when opening a form from another form and
populate the value of a control (combo box). I have it working with the
following code.

Dim strFormName As String
Dim strSetID As String

strFormName = "frm_Attendance_Add_Launch"

strSetID = Me![ParticipantID]

DoCmd.OpenForm strFormName, acNormal, , , acFormAdd, acWindowNormal

Forms!frm_Attendance_Add_Launch!ParticipantID.DefaultValue = strSetID


Is issue is with this bit of code:

Forms!frm_Attendance_Add_Launch!ParticipantID.DefaultValue = strSetID

I would like to use a variable for the "Forms!frm_Attendance_Add_Launch!"
piece but I am unsure how to set the variable automatically and then use it
in the Defaultvalue code.
 
J

Jack Leach

Using the Expression Service you cannot substitute a hardcoded form name with
a variable. Instead, refer to the Collection directly:

Dim strFormName As String, strControlName As String
strFormName = "frmSomeForm"
strControlName = "ctlSomeControl"

Forms(strFormName).Controls(strControlName).Value = "DefaultValue"


note that using the OpenArgs method to pass this information may prove to be
a bit easier (and safer).

DoCmd.OpenForm strFormName, acNormal, , , acFormAdd, acWindowNormal, strSetID

in the Open event of the form:

Private Sub Form_Open(Cancel As Integer)
If Len(Nz(Me.OpenArgs, "")) <> 0 Then
Me.DefaultValue = Me.OpenArgs
End If
End Sub

This gets rid of the need to reference the form through the Forms collection
(and the possibility that the form may not be open for some reason).

hth

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
R

Rebecca_SUNY

The issue with using the Open event of the form is that the form opens off
of three different forms and each time the strSetID could be different, that
is why I want to set the value and push it to the opening form. Can I do
that in the Open event?


Jack Leach said:
Using the Expression Service you cannot substitute a hardcoded form name with
a variable. Instead, refer to the Collection directly:

Dim strFormName As String, strControlName As String
strFormName = "frmSomeForm"
strControlName = "ctlSomeControl"

Forms(strFormName).Controls(strControlName).Value = "DefaultValue"


note that using the OpenArgs method to pass this information may prove to be
a bit easier (and safer).

DoCmd.OpenForm strFormName, acNormal, , , acFormAdd, acWindowNormal, strSetID

in the Open event of the form:

Private Sub Form_Open(Cancel As Integer)
If Len(Nz(Me.OpenArgs, "")) <> 0 Then
Me.DefaultValue = Me.OpenArgs
End If
End Sub

This gets rid of the need to reference the form through the Forms collection
(and the possibility that the form may not be open for some reason).

hth

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



Rebecca_SUNY said:
Using Access 2007

I am trying to use setdefault when opening a form from another form and
populate the value of a control (combo box). I have it working with the
following code.

Dim strFormName As String
Dim strSetID As String

strFormName = "frm_Attendance_Add_Launch"

strSetID = Me![ParticipantID]

DoCmd.OpenForm strFormName, acNormal, , , acFormAdd, acWindowNormal

Forms!frm_Attendance_Add_Launch!ParticipantID.DefaultValue = strSetID


Is issue is with this bit of code:

Forms!frm_Attendance_Add_Launch!ParticipantID.DefaultValue = strSetID

I would like to use a variable for the "Forms!frm_Attendance_Add_Launch!"
piece but I am unsure how to set the variable automatically and then use it
in the Defaultvalue code.
 
J

Jack Leach

Can I do that in the Open event?

Sure... the OpenArg of that you will reference in the Open event of the form
will have whatever value was passed in that particular instance of opening
the form... so, you can open this form three different times from three
different forms with three different values passed to the OpenArgs, and each
time in the Open even of the form being opened will read the value of the
default that was specific to that particular instance of the opening....
make sense? *scratches head*


--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



Rebecca_SUNY said:
The issue with using the Open event of the form is that the form opens off
of three different forms and each time the strSetID could be different, that
is why I want to set the value and push it to the opening form. Can I do
that in the Open event?


Jack Leach said:
Using the Expression Service you cannot substitute a hardcoded form name with
a variable. Instead, refer to the Collection directly:

Dim strFormName As String, strControlName As String
strFormName = "frmSomeForm"
strControlName = "ctlSomeControl"

Forms(strFormName).Controls(strControlName).Value = "DefaultValue"


note that using the OpenArgs method to pass this information may prove to be
a bit easier (and safer).

DoCmd.OpenForm strFormName, acNormal, , , acFormAdd, acWindowNormal, strSetID

in the Open event of the form:

Private Sub Form_Open(Cancel As Integer)
If Len(Nz(Me.OpenArgs, "")) <> 0 Then
Me.DefaultValue = Me.OpenArgs
End If
End Sub

This gets rid of the need to reference the form through the Forms collection
(and the possibility that the form may not be open for some reason).

hth

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



Rebecca_SUNY said:
Using Access 2007

I am trying to use setdefault when opening a form from another form and
populate the value of a control (combo box). I have it working with the
following code.

Dim strFormName As String
Dim strSetID As String

strFormName = "frm_Attendance_Add_Launch"

strSetID = Me![ParticipantID]

DoCmd.OpenForm strFormName, acNormal, , , acFormAdd, acWindowNormal

Forms!frm_Attendance_Add_Launch!ParticipantID.DefaultValue = strSetID


Is issue is with this bit of code:

Forms!frm_Attendance_Add_Launch!ParticipantID.DefaultValue = strSetID

I would like to use a variable for the "Forms!frm_Attendance_Add_Launch!"
piece but I am unsure how to set the variable automatically and then use it
in the Defaultvalue code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top