Saving a record in subform

H

hotplate

Hi,

I need to save a record in a subform before trying to close the main
form.

I tried creating a save command in the subform and calling it when
trying to close the main form. Unfortunately I could not figure out
how to do this.

The reason I need to save subform record is because I am adding the
numbers in the subform to see if they equal a number in the main
record, if they don't match the form will not close. I can't even
check for a match if the subform record does not get saved before
closing.

I am using a command button to close the main form.

Thanks
 
J

Jeanette Cunningham

Do both forms have a table or query as their record source?
Are you using the link master and link child fields?

If you are using bound forms, does your save button have code like this?
If Me.Dirty = True Then
Me.Dirty = False
End If


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
H

hotplate

Yes both forms have a table as their record source and they are
linked.

I am using the wizard provided save command, so no I don't have that
code.

I am not sure where this is going.
 
J

Jeanette Cunningham

Thanks for providing the info.
When you are using bound forms and main form and subform are linked using
the link master and link child fields, access does save the subform record
when user clicks out of the subform onto the main form.

I sugest that you remove the save button from the subform - as access will
automatically save the subform data when user clicks something on the main
form.

Put an unbound textbox on the subform and set its control source to
=Sum([FieldName])

To check for a match when on the main form, you can use code like this-->
Private Function CheckForMatch() As Boolean
If Me.[NameOfControl] = Me.[SubformControlName].Form.[NameOf
TheUnboundTextBox] Then
CheckForMatch = True
Else
CheckForMatch = False
End If
End Function

Then on the unload event for the main form, you can go
If Not CheckForMatch = True Then
Cancel = True
End If

The above should prevent the main form closing unless the numbers match.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Yes both forms have a table as their record source and they are
linked.

I am using the wizard provided save command, so no I don't have that
code.

I am not sure where this is going.
 
H

hotplate

I had code very similar to this checking for a match (your code is
better though).

My problem is that if they enter the number in the control of the
subform and try to close the form without first exiting that control,
the sum does not update before trying to close. This would only be a
problem if the control in the subform is the very last control the
user enters a value.



Thanks for providing the info.
When you are using bound forms and main form and subform are linked using
the link master and link child fields, access does save the subform record
when user clicks out of the subform onto the main form.

I sugest that you remove the save button from the subform - as access will
automatically save the subform data when user clicks something on the main
form.

Put an unbound textbox on the subform and set its control source to
=Sum([FieldName])

To check for a match when on the main form, you can use code like this-->
Private Function CheckForMatch() As Boolean
If Me.[NameOfControl] = Me.[SubformControlName].Form.[NameOf
TheUnboundTextBox] Then
    CheckForMatch = True
Else
    CheckForMatch = False
End If
End Function

Then on the unload event for the main form, you can go
If Not CheckForMatch = True Then
    Cancel = True
End If

The above should prevent the main form closing unless the numbers match.

Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Yes both forms have a table as their record source and they are
linked.

I am using the wizard provided save command, so no I don't have that
code.

I am not sure where this is going.

Do both forms have a table or query as their record source?
Are you using the link master and link child fields?
If you are using bound forms, does your save button have code like this?
If Me.Dirty = True Then
Me.Dirty = False
End If
Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
J

Jeanette Cunningham

You can also use recalc like this-->
Me.Recalc
You could try using recalc on the subform in its after update event.
Access gives a lower priority to doing sums than running much of the form's
code.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia

I had code very similar to this checking for a match (your code is
better though).

My problem is that if they enter the number in the control of the
subform and try to close the form without first exiting that control,
the sum does not update before trying to close. This would only be a
problem if the control in the subform is the very last control the
user enters a value.



Thanks for providing the info.
When you are using bound forms and main form and subform are linked using
the link master and link child fields, access does save the subform record
when user clicks out of the subform onto the main form.

I sugest that you remove the save button from the subform - as access will
automatically save the subform data when user clicks something on the main
form.

Put an unbound textbox on the subform and set its control source to
=Sum([FieldName])

To check for a match when on the main form, you can use code like this-->
Private Function CheckForMatch() As Boolean
If Me.[NameOfControl] = Me.[SubformControlName].Form.[NameOf
TheUnboundTextBox] Then
CheckForMatch = True
Else
CheckForMatch = False
End If
End Function

Then on the unload event for the main form, you can go
If Not CheckForMatch = True Then
Cancel = True
End If

The above should prevent the main form closing unless the numbers match.

Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia


Yes both forms have a table as their record source and they are
linked.

I am using the wizard provided save command, so no I don't have that
code.

I am not sure where this is going.

Do both forms have a table or query as their record source?
Are you using the link master and link child fields?
If you are using bound forms, does your save button have code like this?
If Me.Dirty = True Then
Me.Dirty = False
End If
Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 

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