Macro/Require Data Entry - Subform

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I have created a form with a subform. I would like to not be able to go to
the next record in the form unless there have been entries made in the
subform. The field name inside the subform is called "Date Incurred". The
after update Macro that I tried to use has a condition of [Date Incurred] is
Null and an Action of CancelEvent. With a GoToControl to return to "Date
Incurred". When I try this it does not work. By the way the subforms
controlsource is a table that is linked to the table that is the source of
the form by using an autonumber. Referential integrity is activated with
cascaded deletes.

Maybe this has something to do with automation objects?

I would greatly appreciate any help
thanks
 
Jzachar,

I think you would need your macro onthe Before Update event of the
subform. After Update is too late!

A better approach, however, might be to go to design of the subform's
table, and set th Validation Rule property of the Date Incurred field to
Is Not Null, and also the Validation Text property to the mesage you
want the user to see if there is no entry for the date.
 
thanks for the help...

I changed the validate rule to Is Not Null. The one problem with this is if
the user never enters or bypasses the subform they can go to a new record on
the form without having ever input any data into the subform. This change
only works if the individual using the form actually enters the subform and
tries to exit without entering data.

How can I make it so that if they have not entered info into the subform
they cannot go to the next record in the related form.

thanks,
jzachar

Steve Schapel said:
Jzachar,

I think you would need your macro onthe Before Update event of the
subform. After Update is too late!

A better approach, however, might be to go to design of the subform's
table, and set th Validation Rule property of the Date Incurred field to
Is Not Null, and also the Validation Text property to the mesage you
want the user to see if there is no entry for the date.

--
Steve Schapel, Microsoft Access MVP
Hi

I have created a form with a subform. I would like to not be able to go to
the next record in the form unless there have been entries made in the
subform. The field name inside the subform is called "Date Incurred". The
after update Macro that I tried to use has a condition of [Date Incurred] is
Null and an Action of CancelEvent. With a GoToControl to return to "Date
Incurred". When I try this it does not work. By the way the subforms
controlsource is a table that is linked to the table that is the source of
the form by using an autonumber. Referential integrity is activated with
cascaded deletes.

Maybe this has something to do with automation objects?

I would greatly appreciate any help
thanks
 
Jzachar,

Ok, I made a wrong assumption. What is the relationship between the
data in the main form and the data in the subform? Is the subform a
continuous view form that allows the entry of more than one related
record for each main form record? Are there more than one field in the
subform's record source? And if so, is it just one of these fields that
you want to check there is an entry in? Or are you really wanting to
ensure that there exists a (at least one?) subform record related to the
main form rexcord? What is the means by which the user is moving to
another record on the main form... a command button controls this, or
the built-in navigation buttons, or something else?
 
jzachar said:
Hi

I have created a form with a subform. I would like to not be able to go
to
the next record in the form unless there have been entries made in the
subform. The field name inside the subform is called "Date Incurred".
The
after update Macro that I tried to use has a condition of [Date Incurred]
is
Null and an Action of CancelEvent. With a GoToControl to return to "Date
Incurred". When I try this it does not work. By the way the subforms
controlsource is a table that is linked to the table that is the source of
the form by using an autonumber. Referential integrity is activated with
cascaded deletes.

Maybe this has something to do with automation objects?

I would greatly appreciate any help
thanks
 
my fault,

The relationship between the subform and form tables is a one to many
relationship using an autonumber ID with referential integrity enabled. The
user can always see the subform therefore I would assume that it is
continuous view. The subform allows multiple record entries that relate to
one record on the form. There are three fields in the subform and its table:
[description], [Date Incurred], and [Amount Incurred]. I want the user to be
required to enter data in both [Date Incurred] and [Amount Incurred], but
they won't have to enter anything in the [Description] field. For instance,
the form has a field that is [Amount], which holds the total amount for an
invoice, while the subform allows the user to enter multiple records which
break out the amount across multiple dates. An example would be an [Amount]
of $1000 in the form and two records related to this in the subform that
would show in which months these amounts were incurred. These two records in
the subform could be $500 in April and $500 in May. The user navigates
between records on both the form and subform by user the built-in navigation
buttons.

please let me know if you need more...

thanks again for the help
jzachar
 
Jzachar,

The way I read it, there are a number of related questions here. You
need to check that for every subform record there is an entry in both
the [Date Incurred] and [Amount Incurred] fields. You need to check
that for every main form record, there is at least one subform record.
And I suppose you also want to check that the subform records total to
the Amount on the main form... though this concept is actually problematic.

Well, the first is taken care of pretty easily, either via the
Validation Rule property of the fields as discussed earlier, or via some
simple code on the subform's Before Update event.

The second is not going to be easy, if you are using the built-in
navigation buttons, as there is no event really available that you can
use to do your validation. Depends a bit how tightly you want to
enforce it. You could, for example, put a big red label at the bottom
of the main form, near the navigation buttons, and use code to hide or
show it, depending on whether there is a subform record or not. Or you
could go a step further, and use the Current event of the main form to
hide the navigation buttons, if there is no subform record, and the only
way to make them available again is via code on the subform's Exit
event. But then the user could still close the form, with no subform
records, which is not what you want either. You could remove the
navigation buttons and close button, so that the only way the user can
move from the current main form record is by using command buttons that
you place on the form, in which case you can use the Click event of the
command buttons to check the data. Or you could forget the idea of
checking the records one by one as they are entered, and instead make a
procedure that runs every time you open the database, and throws an
message box if it finds any records in the main form's table with no
corresponding records in the subform's table. Sorry to be non-specific
at this stage, but I think first we can clarify the general approach
that might be most suitable.

As for the Amount field, it is unnecessary, and probably an unwise idea,
to store this value. You have the Amount Incurred in the subform's
table, and the invoice amount will always be the sum of these, so you
can always very easily calculate the Amount when needed.

--
Steve Schapel, Microsoft Access MVP

my fault,

The relationship between the subform and form tables is a one to many
relationship using an autonumber ID with referential integrity enabled. The
user can always see the subform therefore I would assume that it is
continuous view. The subform allows multiple record entries that relate to
one record on the form. There are three fields in the subform and its table:
[description], [Date Incurred], and [Amount Incurred]. I want the user to be
required to enter data in both [Date Incurred] and [Amount Incurred], but
they won't have to enter anything in the [Description] field. For instance,
the form has a field that is [Amount], which holds the total amount for an
invoice, while the subform allows the user to enter multiple records which
break out the amount across multiple dates. An example would be an [Amount]
of $1000 in the form and two records related to this in the subform that
would show in which months these amounts were incurred. These two records in
the subform could be $500 in April and $500 in May. The user navigates
between records on both the form and subform by user the built-in navigation
buttons.

please let me know if you need more...

thanks again for the help
jzachar
 
thanks

I am creating command buttons that the user will use to navigate. The
button that will be used to go to the next record is called Next Record1.
Below is the syntax that it currently displays when I go to the Click event.
What do I need to add to this to get it to check the [Date Incurred] and
[Amount Incurred] fields in the subform before it allows the user to scroll
to the next record? Or is there a way to do it without code? When I click
on the Click Event it goes straight to VBA. Sorry about the multiple
questions but I am not very experience with Access.

jzachar

Private Sub Next_Record1_Click()
On Error GoTo Err_Next_Record1_Click


DoCmd.GoToRecord , , acNext

Exit_Next_Record1_Click:
Exit Sub

Err_Next_Record1_Click:
MsgBox Err.Description
Resume Exit_Next_Record1_Click

End Sub

Steve Schapel said:
Jzachar,

The way I read it, there are a number of related questions here. You
need to check that for every subform record there is an entry in both
the [Date Incurred] and [Amount Incurred] fields. You need to check
that for every main form record, there is at least one subform record.
And I suppose you also want to check that the subform records total to
the Amount on the main form... though this concept is actually problematic.

Well, the first is taken care of pretty easily, either via the
Validation Rule property of the fields as discussed earlier, or via some
simple code on the subform's Before Update event.

The second is not going to be easy, if you are using the built-in
navigation buttons, as there is no event really available that you can
use to do your validation. Depends a bit how tightly you want to
enforce it. You could, for example, put a big red label at the bottom
of the main form, near the navigation buttons, and use code to hide or
show it, depending on whether there is a subform record or not. Or you
could go a step further, and use the Current event of the main form to
hide the navigation buttons, if there is no subform record, and the only
way to make them available again is via code on the subform's Exit
event. But then the user could still close the form, with no subform
records, which is not what you want either. You could remove the
navigation buttons and close button, so that the only way the user can
move from the current main form record is by using command buttons that
you place on the form, in which case you can use the Click event of the
command buttons to check the data. Or you could forget the idea of
checking the records one by one as they are entered, and instead make a
procedure that runs every time you open the database, and throws an
message box if it finds any records in the main form's table with no
corresponding records in the subform's table. Sorry to be non-specific
at this stage, but I think first we can clarify the general approach
that might be most suitable.

As for the Amount field, it is unnecessary, and probably an unwise idea,
to store this value. You have the Amount Incurred in the subform's
table, and the invoice amount will always be the sum of these, so you
can always very easily calculate the Amount when needed.

--
Steve Schapel, Microsoft Access MVP

my fault,

The relationship between the subform and form tables is a one to many
relationship using an autonumber ID with referential integrity enabled. The
user can always see the subform therefore I would assume that it is
continuous view. The subform allows multiple record entries that relate to
one record on the form. There are three fields in the subform and its table:
[description], [Date Incurred], and [Amount Incurred]. I want the user to be
required to enter data in both [Date Incurred] and [Amount Incurred], but
they won't have to enter anything in the [Description] field. For instance,
the form has a field that is [Amount], which holds the total amount for an
invoice, while the subform allows the user to enter multiple records which
break out the amount across multiple dates. An example would be an [Amount]
of $1000 in the form and two records related to this in the subform that
would show in which months these amounts were incurred. These two records in
the subform could be $500 in April and $500 in May. The user navigates
between records on both the form and subform by user the built-in navigation
buttons.

please let me know if you need more...

thanks again for the help
jzachar
 
Jzachar,

If it was me, I would still use the Validation Rule of the [Date
Incurred] and [Amount Incurred] fields, a we discussed earlier, to check
that these have been completed in a subform record. The thing that we
are trying to tackle is something else again, i.e. to determine that
there has been a subform record added, i.e. that the user hasn't just
entered a main form record, and try to move on without entering the
invoice details. Am I right?

Try it something like this...

If DCount("*","YourSubformTable","[ID]=" & Me.ID)=0 Then
MsgBox "Enter the invoice details.", , "Not finished"
Else
DoCmd.GoToRecord , , acNext
End If
 
hi Steve, i have a sidebar question, if you have a minute: would checking
the RecordCount of the subform's RecordSet work? if so, how do the two
methods compare? thx, tina :)


Steve Schapel said:
Jzachar,

If it was me, I would still use the Validation Rule of the [Date
Incurred] and [Amount Incurred] fields, a we discussed earlier, to check
that these have been completed in a subform record. The thing that we
are trying to tackle is something else again, i.e. to determine that
there has been a subform record added, i.e. that the user hasn't just
entered a main form record, and try to move on without entering the
invoice details. Am I right?

Try it something like this...

If DCount("*","YourSubformTable","[ID]=" & Me.ID)=0 Then
MsgBox "Enter the invoice details.", , "Not finished"
Else
DoCmd.GoToRecord , , acNext
End If

--
Steve Schapel, Microsoft Access MVP

thanks

I am creating command buttons that the user will use to navigate. The
button that will be used to go to the next record is called Next Record1.
Below is the syntax that it currently displays when I go to the Click event.
What do I need to add to this to get it to check the [Date Incurred] and
[Amount Incurred] fields in the subform before it allows the user to scroll
to the next record? Or is there a way to do it without code? When I click
on the Click Event it goes straight to VBA. Sorry about the multiple
questions but I am not very experience with Access.

jzachar

Private Sub Next_Record1_Click()
On Error GoTo Err_Next_Record1_Click


DoCmd.GoToRecord , , acNext

Exit_Next_Record1_Click:
Exit Sub

Err_Next_Record1_Click:
MsgBox Err.Description
Resume Exit_Next_Record1_Click

End Sub
 
Hi Tina,

Yes, you could use...
If Me.NameOfSubform.Form.RecordsetClone.RecordCount = 0 Then ...etc

In practice, there would not be a difference in performance or overhead.
I suspect the DCount is probably more efficient, but I'm not sure
about that, and don't really care :-) The main reason I chose the
DCount option in my reply to Jzachar is because it seems to me easier
for a newbie to digest... but I might be wrong about that too! ;-) In
fact, if it was mine, to be honest, I would not do it either of these
ways. I would put an unbound textbox on the subform's footer, with
Control Source =Count(*) and refer to this in my code. But then you
need a UDF to cater to there being no subform records, so this starts to
get more complicated to explain in the context of the thread with
Jzachar's question.
 
okay, "either/or" for performance considerations, thanks.
i wasn't questioning your choice of answer for jzachar, though, just trying
to learn something extra for myself from my tangental question. hopefully
jzachar saw my "sidebar" disclaimer, and just ignored this thread offshoot!
:)
 
Back
Top