Incomprehensible OpenArgs behavior

N

night_writer

Incomplrehensible to me, at least. I'm hoping wiser heads will offer
advice.

I have a form (frmCirculars) that displays individual records of
correspondence (circulars) in single form view. If the form opens
directly, you can cycle through all 13,000 or so records. Each
circular is associated with a project.

I have a project form (frmProjects) that displays all related
circulars in abbreviated fashion on a subform. For any particular
circular on the subform, there is a "view" button that opens a Single
Form view of the associated circular. I would also like to be able to
add a new circular to a project by opening the main circular form in
add mode, and I would like to pass the project number from frmProjects
to the newly opened frmCirculars.

To that end, I have added a button (btnAddNewCircular) to the
frmProjects subform and tried to pass the ProjectID to the new form.
ProjectID is a long integer. Here is my code:

Dim stDocName As String, lngOpenArgs As Long
stDocName = "frmCirculars"
lngOpenArgs = Me.Parent!ProjectID
DoCmd.OpenForm stDocName, , , , acFormAdd, , lngOpenArgs

I've got two problems. The first time I click the button after opening
frmProjects, no opening arguement is passed. I have attached this code
to the Open event of the called form, and I invariably get "OOPS" (no
argument passed) on the first try:

If Not IsNull(Me.OpenArgs) Then
MsgBox "OK"
MsgBox Me.OpenArgs
Me.ProjectID = Me.OpenArgs ' You can't assign a value to this
object
MsgBox Me.ProjectID.Value
Else
MsgBox "OOPS"
End If

If I then close frmCircular and click the button on frmProjects again,
the argument does pass (MsgBox = "OK") and it is the correct argument
(MsgBox Me.OpenArgs).

And here is my second problem. I can't figure out how to get the
opening arguement into the field I want (ProjectID). I get the error
"You can't assign a value to this object." I suppose it's possible
that the Me.ProjectID (which is the same field name in both the
calling and the opening form) might be messing things up, but I have
tried several ways of referencing the field, and nothing seems to
work.

I might also be going about this in the completely wrong way. The
example code in the help file all has to do with finding an existing
record, and all I want to do is put a value in a field of a new
record.

So, these are my two problems:
1. Why won't the OpenArgs pass to the opening form the first time, but
will pass the second time, and
2. How do I need to reference the frmCirculars ProjectID field so it
can be updated with the OpenArgs info

Any help would be greatly appreciated.
 
D

Dirk Goldgar

night_writer said:
Incomplrehensible to me, at least. I'm hoping wiser heads will offer
advice.

I have a form (frmCirculars) that displays individual records of
correspondence (circulars) in single form view. If the form opens
directly, you can cycle through all 13,000 or so records. Each
circular is associated with a project.

I have a project form (frmProjects) that displays all related
circulars in abbreviated fashion on a subform. For any particular
circular on the subform, there is a "view" button that opens a Single
Form view of the associated circular. I would also like to be able to
add a new circular to a project by opening the main circular form in
add mode, and I would like to pass the project number from frmProjects
to the newly opened frmCirculars.

To that end, I have added a button (btnAddNewCircular) to the
frmProjects subform and tried to pass the ProjectID to the new form.
ProjectID is a long integer. Here is my code:

Dim stDocName As String, lngOpenArgs As Long
stDocName = "frmCirculars"
lngOpenArgs = Me.Parent!ProjectID
DoCmd.OpenForm stDocName, , , , acFormAdd, , lngOpenArgs

I've got two problems. The first time I click the button after opening
frmProjects, no opening arguement is passed. I have attached this code
to the Open event of the called form, and I invariably get "OOPS" (no
argument passed) on the first try:

If Not IsNull(Me.OpenArgs) Then
MsgBox "OK"
MsgBox Me.OpenArgs
Me.ProjectID = Me.OpenArgs ' You can't assign a value to this
object
MsgBox Me.ProjectID.Value
Else
MsgBox "OOPS"
End If

If I then close frmCircular and click the button on frmProjects again,
the argument does pass (MsgBox = "OK") and it is the correct argument
(MsgBox Me.OpenArgs).

And here is my second problem. I can't figure out how to get the
opening arguement into the field I want (ProjectID). I get the error
"You can't assign a value to this object." I suppose it's possible
that the Me.ProjectID (which is the same field name in both the
calling and the opening form) might be messing things up, but I have
tried several ways of referencing the field, and nothing seems to
work.

I might also be going about this in the completely wrong way. The
example code in the help file all has to do with finding an existing
record, and all I want to do is put a value in a field of a new
record.

So, these are my two problems:
1. Why won't the OpenArgs pass to the opening form the first time, but
will pass the second time, and


I'm not completely sure why you're not seeing the ProjectID in OpenArgs the
first time around, but I note that values passed via OpenArgs should be
strings. I'd expect the long integer value you're passing to be converted
automatically to a string, but maybe it will make a difference if you
explicitly convert it to a string:

DoCmd.OpenForm stDocName, _
DataMode:=acFormAdd, _
OpenArgs:=CStr(Me.Parent!ProjectID)
2. How do I need to reference the frmCirculars ProjectID field so it
can be updated with the OpenArgs info

You can't assign the value in the form's Open event, because the form's
recordsource hasn't even been queried yet. I think, though, that you should
be able to assign it in the form's Load event. Try this:

'------ start of suggested code ------
Private Sub Form_Load()

Dim strOpenArgs As String

strOpenArgs = Me.OpenArgs & vbNullString

If Len(strOpenArgs) > 0 Then
MsgBox "OK - OpenArgs = " & strOpenArgs
Me.ProjectID = Clng(strOpenArgs)
Else
MsgBox "OOPS"
' NOTE: Assuming you want to use this form also
' for browsing & editing, you wouldn't really have the
' above line.
End If

End Sub

'------ end of suggested code ------
 
N

night_writer

I'm not completely sure why you're not seeing the ProjectID in OpenArgs the
first time around, but I note that values passed via OpenArgs should be
strings.  I'd expect the long integer value you're passing to be converted
automatically to a string, but maybe it will make a difference if you
explicitly convert it to a string:

    DoCmd.OpenForm stDocName, _
        DataMode:=acFormAdd, _
        OpenArgs:=CStr(Me.Parent!ProjectID)


You can't assign the value in the form's Open event, because the form's
recordsource hasn't even been queried yet.  I think, though, that you should
be able to assign it in the form's Load event.  Try this:

'------ start of suggested code ------
Private Sub Form_Load()

    Dim strOpenArgs As String

    strOpenArgs = Me.OpenArgs & vbNullString

    If Len(strOpenArgs) > 0 Then
        MsgBox "OK - OpenArgs = " & strOpenArgs
        Me.ProjectID = Clng(strOpenArgs)
    Else
        MsgBox "OOPS"
        ' NOTE: Assuming you want to use this form also
        ' for browsing & editing, you wouldn't really have the
        ' above line.
    End If

End Sub

'------ end of suggested code ------

--
Dirk Goldgar, MS Access MVP
Access tips:www.datagnostics.com/tips.html

(please reply to the newsgroup)- Hide quoted text -

- Show quoted text -

Thank you so much! Your fixes solved both of my problems!

Alice
 

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