0

Okay, so I have a database in Access, and when I have one form open (for a job application), and click a button it runs this code:

Private Sub cmdAddDemo_Click()

DoCmd.OpenForm "sfrmAppDemographics", , , , , , txtApplID.Value

End Sub

Where txtApplID is a text box filled with a numerical value for "application ID" number in a table for applications. I want this value to be passed to the "sfrmAppDemographics" form so it will open the correct demographics information for the user displayed in the previously mentioned form for the application.

So, I did this in the code for the demographics form:

Private Sub Form_Open(Cancel As Integer)

Dim rs As DAO.Recordset
Set rs = Me.Recordset
If rs.RecordCount = 0 Then
    rs.AddNew
    rs!ApplID = Me.OpenArgs
    rs.Update
Else
    MsgBox Me.OpenArgs
End If
Me.Requery

End Sub

So, the idea is, if there's no demographic info, it will create a new one using the ApplID from the passed openargs, and if there IS demographic data for this user, it pops up a message box with the openargs (as a test to see if it's working). I ALWAYS get an error "Run-time error '94': Invalid use of Null" - on the line for the MsgBox. (Because the database DOES have records for demographics). When I take out the Else MsgBox Me.OpenArgs, it just shows the first demographics info in the db, with ApplID 1. Strange. It seems I am not able to pass or access the ApplID through the OpenArgs functionality in the Form_Open code. Any help?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • This doesn't make sense to me. You're trying to set the recordset based on the form's recordset? It might not have even loaded yet - and you're not trapping for OpenArgs unless there's no data in the recordset - it's not even checking the AppID. Put a `Debug.Print Me.OpenArgs` at the top of your code and it should demonstrate that it's being passed to the Form_Open event – dbmitch Aug 02 '16 at 21:28

2 Answers2

1

OpenArgs is a string that is passed to the form, nothing more. If you want to filter the called form, you use the WhereCondition parameter:

Private Sub cmdAddDemo_Click()

    DoCmd.OpenForm "sfrmAppDemographics", WhereCondition:="ApplID = " & txtApplID.Value

End Sub

If you want to create the record if it doesn't exist, it makes sense to pass the ID additionally as OpenArgs:

    DoCmd.OpenForm "sfrmAppDemographics", WhereCondition:="ApplID = " & txtApplID.Value, _
                   OpenArgs:=txtApplID.Value

and then

Private Sub Form_Open(Cancel As Integer)

' No need for a separate recordset here
If Me.RecordsetClone.EOF Then
    ' We are already in a new record, just set the bound ID control to create it
    Me!ApplID = Me.OpenArgs
    ' if you want to save the new record immediately
    Me.Dirty = False
End If

End Sub

P.S. if Me.OpenArgs is Null in your code, then txtApplID was Null. I see no other reason.

Andre
  • 26,751
  • 7
  • 36
  • 80
  • However, it seems to break down when I open a record that does not already have demographic data, it tells me "Run-time error '2448' You can't assign a value to this object" on the line Me!ApplID = Me.OpenArgs. How do I make it create a new, blank demographic datum? – Jacob Jackson Aug 03 '16 at 17:52
  • Do you have a control with the name `ApplID` (and bound to the column ApplID) on the form? If no, create a textbox for it - when everything works, you can set it to `Visible=False`. If yes, is it locked or something? – Andre Aug 03 '16 at 17:58
  • Yes, there's a control named ApplID and it's bound to the column of the same name. It's not locked either. Weird. Me.OpenArgs is indeed passing through the right ApplID number, but it doesn't want to set the textbox value. Is there something more I need to do to create a new record for the demographic data when one doesn't already exist? – Jacob Jackson Aug 03 '16 at 18:06
0

Just needed to add DoCmd.GoToRecord , , acNewRec to the code from Andre and it worked without a hitch...

Private Sub Form_Open(Cancel As Integer)

    If Me.RecordsetClone.EOF Then

        DoCmd.GoToRecord , , acNewRec

        Me!ApplID.Value = Me.OpenArgs

        Me.Dirty = False

    End If

End Sub