Sorry to take so long in getting back to you, the last few days have been a
little hectic. After reading your last posts I believe I understand what you
are trying to do and I also understand why you are having trouble doing it.
You are using OpenArgs to pass the name of the calling form and then using
that name, you are referencing back to a control on calling form. The reason
your code won't work is that a subform is not open by itself and any
references to it or its properties or controls must be made through the
parent form and you must have the name of the subform control to get to the
subform. While possible, it may be more trouble than it is worth.
Instead you might want to consider passing the value (or values) to the
called form using a delimited string which you break apart in the called
form using the Split function. If you want to procede using only the name of
the form then you will need a stategy that allows you to get to the subform.
Because you really need the name of the subform control AND the name of the
parent form the code is a bit more complex. Here is code that creates the
openArgs string with the name of the mainform with the subform control name
when the form is opened as a subform. When the form is not opened a subform
it passes only the form name.
* This goes in the calling form/subform
Dim ctl As Control
Dim strOpenArgs As String
If IsSubForm(Me) Then
For Each ctl In Parent.Controls
If ctl.ControlType = acSubform Then
If ctl.Form.hWnd = Me.hWnd Then
strOpenArgs = Me.Parent.Name & "," & ctl.Name
End If
End If
Next
Else
strOpenArgs = Me.Name
End If
DoCmd.OpenForm "frmTestOpenArgs", , , , , , strOpenArgs
End Sub
* This goes in a standard module
Public Function IsSubForm(pFrm As Form) As Boolean
' Comments :
' Parameters: pFrm -
' Returns : Boolean
' Modified : 07/31/2000 SMD
'
' Is the form referenced in the
' parameter currently loaded as a subform?
' Check its Parent property to find out.
' In:
' frm: a reference to the form in question
' Out:
' Return value: True if the form is a subform
' False if it's a standalone form
Dim strName As String
On Error Resume Next
strName = pFrm.Parent.Name
IsSubForm = (Err = 0)
End Function
Of course the called form has to be able to handle the additional
information so here's how this code should look:
Private Sub Form_Open(Cancel As Integer)
Dim strArgs() As String
dim strSubFormCtlName as string
dim strFormName as string
If Len(Me.OpenArgs & "") > 0 Then
strArgs = Split(Me.OpenArgs, ",")
strFormName = strArgs(0)
If UBound(strArgs) > 0 Then
strSubFormCtlName = strArgs(1)
msgbox "The Custid on the subform is: " &
Forms(Me.txtFormName).Form.Controls(Me.txtSubFormCtlName)!Custid
Else
msgbox "The Custid on the subform is: " &
Forms(Me.txtFormName)!Custid
End If
End If
--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.
I failed to mention the 'Name' is the name of the subform I am trying to
pass. Also, I wonder if it has to do with the fact the subform is unbound
and thus not linked to the main form.
Sandra Daigle said:
Is Name the 'name' of the control on the subform which you are trying to
pass?
If so there are two problems - first you should not have fields or
controls which are named 'Name'. 'Name' a built in property of virtually
every object, including the form object so what you are really passing
is the name of the main form. (Granted, you might be trying to pass the
name of the form)
Second, still assuming that in your statement, me.Name is intended to
supply a value from the subform you must reference the subform through
the subform control which is on the mainform. The syntax is:
me.sfrmMySub.form.MyControl
Where 'sfrmMySub' is the name of the subform control (which is not
necessarily the same as the name of the form object referenced by the
subform control - ie the name you see in the database window and used in
the SourceObject property of the subform Control). To verify that you
have the correct name, click once on the subform control then look under
the Name property. Whatever you find there goes in place of 'sfrmMySub'
above. 'MyControl' should be replaced with the name of the control on
the subform.
Let me know whether this helps,
--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.
Fysh wrote:
Sandra, I have left work for the day and don't have the code with me. I
will post it tomorrow if there is no problem with that. However, I do
want to mentioned that I have tried several different techniques that I
saw here when I searched for OpenArgs, but none of them seem to work.
One of the attempts was something like DoCmd.OpenForm
"frmComments",,,,,,OpenArgs:=Me.Name But it couldn't find my subform
which makes since. I should try to located my main form then my
subform. If I can get that part to work then passing the variables
should be easy would imagine. It just can't locate the subform.
Once again I will post again tomorrow when I get back to work thanks
again. Also, thanks Ken by chance do you know of a location that I could
find that article?
:
I have a pop-up form which I am trying to get to open from either a
subform or another form. Once this form is open I then want to have it
populate 2 fields from either the subform or another form. Both have
the same field names. I can get it to work with the other form.
However, I am stuck trying to get OpenArgs to open a form when clicking
a button on a subform. I have read a lot about OpenArgs today but
nothing really dealing with my situation. Can anyone suggest on how to
go about doing this? Thanks in advance..