Passing data between forms OpenArgs? (1 Viewer)

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:03
Joined
Feb 19, 2002
Messages
42,981
The designers of the Access object model for forms were masterful. We can all be masters of our universe if we only just study the available events and use them as MS intended.

To populate a foreign key, the CORRECT event to use is the Form's BeforeInsert event. You don't need any If's to determine if you are on a new record because this event runs ONLY for a new record and it runs only once and only AFTER the user has first started typing. That means that you won't inadvertently dirty a record with your own code as you are currently doing. Since the OpenArgs is provided by a form that YOU coded, I wouldn't even bother to validate that either. Although you could if you don't trust your own code.

Also, to avoid saving incomplete records, you should always have validation code in your form's BeforeUpdate event to ensure that required fields are present and other fields are rational. For example, DOB can't be >= Date(). It also shouldn't be more than about 120 years ago or less if this is an employee database. If you want to hire people in their 80's ( and I think you should :)) set a high limit and also allow the user to override the question - is this date valid? On the other hand, if you are working with an ancestry database, then obviously the DOB's could be very old but probably 1/1/202 still won't be valid so you can check that particular typo by limiting the year to something rational for your data.

Code:
      strOpenArgs = Split(Me.OpenArgs, ";")
      Me.TbJobId = strOpenArgs(0)
 

tucker61

Registered User.
Local time
Today, 05:03
Joined
Jan 13, 2008
Messages
321
Just playing round with the validation code - should something like this work on the tabbed forms ?
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Nz(cboxOption, "") = "" Then
       Cancel = True
    Else
    End If
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:03
Joined
Sep 21, 2011
Messages
14,051
Why are you using Else all the time, when there is no code for that logic?
 

tucker61

Registered User.
Local time
Today, 05:03
Joined
Jan 13, 2008
Messages
321
The designers of the Access object model for forms were masterful. We can all be masters of our universe if we only just study the available events and use them as MS intended.

To populate a foreign key, the CORRECT event to use is the Form's BeforeInsert event. You don't need any If's to determine if you are on a new record because this event runs ONLY for a new record and it runs only once and only AFTER the user has first started typing. That means that you won't inadvertently dirty a record with your own code as you are currently doing. Since the OpenArgs is provided by a form that YOU coded, I wouldn't even bother to validate that either. Although you could if you don't trust your own code.

Also, to avoid saving incomplete records, you should always have validation code in your form's BeforeUpdate event to ensure that required fields are present and other fields are rational. For example, DOB can't be >= Date(). It also shouldn't be more than about 120 years ago or less if this is an employee database. If you want to hire people in their 80's ( and I think you should :)) set a high limit and also allow the user to override the question - is this date valid? On the other hand, if you are working with an ancestry database, then obviously the DOB's could be very old but probably 1/1/202 still won't be valid so you can check that particular typo by limiting the year to something rational for your data.

Code:
      strOpenArgs = Split(Me.OpenArgs, ";")
      Me.TbJobId = strOpenArgs(0)
This was the original code that i had in the Beforeupdate event, are you saying that this way would still work ?
Code:
    If Nz(cboxOption, "") = "" Then
        MsgBox "You have to select an option !", vbExclamation + vbOKOnly, "Missing"
        Cancel = True
        ESCMessage
    Else
        On Error Resume Next
        tbJobID = Forms!frmMain.tbJobID
        If Nz(tbComments, "") <> "" Then
            tbComments.SetFocus
            tbComments.SelStart = 0
            tbComments.SelLength = Len(tbComments)
            DoCmd.SetWarnings False
            DoCmd.RunCommand acCmdSpelling
            DoCmd.SetWarnings True
        End If
        'On Error GoTo 0
    End If

Why are you using Else all the time, when there is no code for that logic?
Because i removed the statements after the else as it was not relevant.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:03
Joined
Feb 19, 2002
Messages
42,981
should something like this work on the tabbed forms ?
Tabs don't change how forms work. The tab control is essentially invisible when you are writing code. No other objects are subordinate to it.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:03
Joined
Feb 19, 2002
Messages
42,981
This was the original code that i had in the Beforeupdate event, are you saying that this way would still work ?
If that is the BeforeUpdate event of the pop up form, and tblJobID is the name of the FK, Yes.

PS, it is best practice to disambiguate your control references by using "Me.". This also gives you intellisense.
 

Users who are viewing this thread

Top Bottom