Dennis said:
The main record is keyed using an Auto Assigned Number.
Do you mean an autonumber field (generated by the database engine), or are
you talking about some sort of number you generate yourself in code?
The auto assigned number is displayed in an unbound field
on the main form. That field is blank on a new record when I go to the
sub-form.
I'm a little confused by what you've written, so please pardon me if I take
a moment to clarify terminology. We need to make a distinction here between
*fields*, which are part of the form's recordset, and *controls*, which are
user-interface objects on the form. (Yes, I'm aware that Microsoft doesn't
always make this distinction clear in its help files.) A control may be
"bound" to a field by having the name of a field in its ControlSource
property, or it may be "calculated" by having an expression as its
ControlSource, or it may be "unbound", having a blank ControlSource.
If you have an autonumber field, then it will be blank (Null) when you first
go to a new record, but will be assigned a value on the instant you modify
that record in any way; for example, by editing the value of any bound
control.
A subform is normally linked to the main form by the Link Master Fields and
Link Child Fields properties of the subform control. The Link Master Fields
property gives the name(s) of one or more fields *or controls* on the main
form whose values should determine which records are shown on the subform.
The Link Child Fields property gives the name(s) of corresponding
field(s) -- *not* controls -- on the subform whose values should correspond
to those of the Link Master Fields.
A Link Master Field would not usually be an unbound control. Usually it
would be a bound control or a field, or in some cases a calculated field. I
can conceive of a reason to use an unbound control for a Link Master Field,
but it doesn't sound like you have any reason to be doing so here.
From the sub-form, I access the key field using
form!frmWorkOrder!cboWorkOrderNo.
1. This is incorrect syntax, so I hope you are not reporting it accurately.
2. If your subform is properly linked via Link Master/Child Fields, it's
unlikely that you have any reason to attempt to access the main form's key
field.
3. An unbound control *cannot* be a key field. At best, it may display the
value of the key field. Remember, a key field is a field in a table. If
your control is unbound, it is not essentially linked to the table at all.
Only your code provides any connection between the unbound control and the
table's key field. It sounds like here you are experencing a problem
related to this disconnection.
The key field is null when I'm in my sub-form on a new record.
Although Acces will always save the main form record, if it is dirty, when
you move from the main form to a subform, it is possible to move to a
subform when you are on a new, unmodified main record. In that case, the
main form's key (Link Master) field may be Null. If that happens, Access
will happily let you fill out the subform, and only raise an error when you
try to save it, telling you that the record can't be saved because the key
field that relates it to the parent record is blank. It sounds to me as if
this is at least partly the problem you are trying to avoid. If so, you can
prevent it by using one or more events of the main form to only enable the
subform if the main form's key field is not Null.
For example, if your subform is on a tab page, you can disable that page
whenever the main form's primary key field is Null:
'------ start of example code #1 ------
Private Sub Form_Current()
Me.PageWithSubform.Enabled = Not IsNull(Me.MainID)
End Sub
Private Sub Form_Dirty(Cancel As Integer)
Me.PageWithSubform.Enabled = Not IsNull(Me.MainID)
End Sub
'------ end of example code #1 ------
Or you can use the tab control's Change event to prevent the user from
moving to the tab page with the subform unless the main form's primary key
field has been filled out:
'------ start of example code #2 ------
Private Sub Form_Current()
' Force user to begin new records on first tab page.
If Me.NewRecord Then
Me.tabMyTab = 0
End If
End Sub
Private Sub tabMyTab_Change()
' Suppose that the 4th page has the subform.
If Me.tabMyTab = 3 Then
If IsNull(Me.MainID) Then
Me.tabMyTab = 0
End If
End If
End Sub
'------ end of example code #2 ------
Let me ask, when is the key field assigned - after the main form's "On
Insert" event? If that is the case, I can update my key field on the
form.
If this is an autonumber field, not one that you generate in code, then it
is created and assigned the instant you modify any bound control, or
otherwise dirty the form. If you do that via the user interface, not by
code, then the form's Dirty event will fire.
I use the automatically assigned key in an SQL source statement
for one of the combo boxes on the sub-forms.
Then probably you're going to need to requery that combo box in the
subform's Current event, or else in the main form's Current event *and* its
AfterInsert event.