Update field on main form, only works when manually step thru code

L

laavista

I need help! I have a main form called "f_Credits" and a subform called
"f_sf_CreditsUsed". On the subform, credits are entered in the field
"Credits". I have a label called "txtTotalCredits" that sums the total
credits in the records in the subform. I need to update the field
"CreditsUsed" on the main form with the summed credits in the calculated
field "txtTotalCredits". (I know that storing calculated data is not
optimum, but I really need to do it in this case.)


Code in the AfterUpdate event:

DoCmd.RunCommand acCmdSaveRecord
Forms!f_Credits.CreditsUsed =
Forms!f_Credits!f_sf_CreditsUsed!txtTotalCredits

This works great IF I manually step through the code. If I execute it
normally, it does not update the main form field.

Your help would be greatly appreciated!
 
A

AlienzDDS

Is it the requery that's missing? When you move away from the record and
then back does the CreditsUsed show? If so perhaps you just need:

forms!f_credits!f_sf_creditsused.form.requery
or me.f_sf_creditsused.requery

You can also try replacing your runcommand with me.dirty = false.

If this isn't it then can you let me know which afterupdate event you are
using?
 
L

laavista

Thank you so much for your quick response. I tried me.dirty = false and get
the same result. If I step through the code one line at a time, it works
and updates the field on the main form "CreditsUsesd". If I don't step
through the code line by line and let it run normally, it doesn't update the
CreditsUsed field on the main form. I even tried putting in a time delay,
but that didn't do anything.

I originally tried requery, and it worked, except the cursor went back to
the first record. It didn't stay on the record that was changed. I spent
around 4 hours searching for an easy way to keep the cursor where it was and
trying different things, but with no luck. I got it to go to the NEXT
record on the subform, which was unsatisfactory. Saving the record seemed
to be THE solution, but then I have this odd thing happening.

I'm using the After Update event for the field "credits" in the subform.

Thanks again for your willingness to help!!!
 
A

AlienzDDS

I noticed your original note said you have a label called "txtTotalCredits"
that sums the total credits in the records in the subform. I imagine you
mean a text box not a label? Can you let me know the code in the
txtTotalCredits?

If all else fails you can try something using dsum instead. It might work
on the beforeupdate event of the main form or on the exit event of the
subform. It would be something like:

me.creditsused = dsum("credits", "qryF_SF_CreditsUsed", "OrderID = " &
me.OrderID)

where credits is the field name containing the number of credits to be
summed, QryF_SF_creditsused is the recordsource for the subform and
orderid would be whatever you are using to like master to child fields.

Hope this helps.

Ali
 
L

laavista

Thanks again for your help. I REALLY appreciate it. I will try your
suggestion and will post another reply with the results.

You're right. I did mean a "text box", not a label. I have a text box
with a calculation. First I just had the formula as =sum([credits]). I then
changed it to =sum(nz(credits],0)).

When I searching on the internet for a solution to my problem, I did see
info on dsum, but there was no good summary of the difference between dsum
and sum, so I went with the sum.

Thanks!
 
A

AlienzDDS

Hi Laavista,

The difference between sum and dsum is that sum uses the information in the
above group of the report and adds it up.

Dsum actually goes to the tables or queries to add up the numbers. It is a
less efficient method as it takes more time, but sometimes it is necessary.

You formula in your text box is fine.

Good luck with your changes.

laavista said:
Thanks again for your help. I REALLY appreciate it. I will try your
suggestion and will post another reply with the results.

You're right. I did mean a "text box", not a label. I have a text box
with a calculation. First I just had the formula as =sum([credits]). I then
changed it to =sum(nz(credits],0)).

When I searching on the internet for a solution to my problem, I did see
info on dsum, but there was no good summary of the difference between dsum
and sum, so I went with the sum.

Thanks!


AlienzDDS said:
I noticed your original note said you have a label called "txtTotalCredits"
that sums the total credits in the records in the subform. I imagine you
mean a text box not a label? Can you let me know the code in the
txtTotalCredits?

If all else fails you can try something using dsum instead. It might work
on the beforeupdate event of the main form or on the exit event of the
subform. It would be something like:

me.creditsused = dsum("credits", "qryF_SF_CreditsUsed", "OrderID = " &
me.OrderID)

where credits is the field name containing the number of credits to be
summed, QryF_SF_creditsused is the recordsource for the subform and
orderid would be whatever you are using to like master to child fields.

Hope this helps.

Ali
 
L

laavista

AlienzDDS: Thanks again for all your help. Your explanation of dsum really
helps.


AlienzDDS said:
Hi Laavista,

The difference between sum and dsum is that sum uses the information in the
above group of the report and adds it up.

Dsum actually goes to the tables or queries to add up the numbers. It is a
less efficient method as it takes more time, but sometimes it is necessary.

You formula in your text box is fine.

Good luck with your changes.

laavista said:
Thanks again for your help. I REALLY appreciate it. I will try your
suggestion and will post another reply with the results.

You're right. I did mean a "text box", not a label. I have a text box
with a calculation. First I just had the formula as =sum([credits]). I then
changed it to =sum(nz(credits],0)).

When I searching on the internet for a solution to my problem, I did see
info on dsum, but there was no good summary of the difference between dsum
and sum, so I went with the sum.

Thanks!


AlienzDDS said:
I noticed your original note said you have a label called "txtTotalCredits"
that sums the total credits in the records in the subform. I imagine you
mean a text box not a label? Can you let me know the code in the
txtTotalCredits?

If all else fails you can try something using dsum instead. It might work
on the beforeupdate event of the main form or on the exit event of the
subform. It would be something like:

me.creditsused = dsum("credits", "qryF_SF_CreditsUsed", "OrderID = " &
me.OrderID)

where credits is the field name containing the number of credits to be
summed, QryF_SF_creditsused is the recordsource for the subform and
orderid would be whatever you are using to like master to child fields.

Hope this helps.

Ali

:

Thank you so much for your quick response. I tried me.dirty = false and get
the same result. If I step through the code one line at a time, it works
and updates the field on the main form "CreditsUsesd". If I don't step
through the code line by line and let it run normally, it doesn't update the
CreditsUsed field on the main form. I even tried putting in a time delay,
but that didn't do anything.

I originally tried requery, and it worked, except the cursor went back to
the first record. It didn't stay on the record that was changed. I spent
around 4 hours searching for an easy way to keep the cursor where it was and
trying different things, but with no luck. I got it to go to the NEXT
record on the subform, which was unsatisfactory. Saving the record seemed
to be THE solution, but then I have this odd thing happening.

I'm using the After Update event for the field "credits" in the subform.

Thanks again for your willingness to help!!!








:

Is it the requery that's missing? When you move away from the record and
then back does the CreditsUsed show? If so perhaps you just need:

forms!f_credits!f_sf_creditsused.form.requery
or me.f_sf_creditsused.requery

You can also try replacing your runcommand with me.dirty = false.

If this isn't it then can you let me know which afterupdate event you are
using?

:

I need help! I have a main form called "f_Credits" and a subform called
"f_sf_CreditsUsed". On the subform, credits are entered in the field
"Credits". I have a label called "txtTotalCredits" that sums the total
credits in the records in the subform. I need to update the field
"CreditsUsed" on the main form with the summed credits in the calculated
field "txtTotalCredits". (I know that storing calculated data is not
optimum, but I really need to do it in this case.)


Code in the AfterUpdate event:

DoCmd.RunCommand acCmdSaveRecord
Forms!f_Credits.CreditsUsed =
Forms!f_Credits!f_sf_CreditsUsed!txtTotalCredits

This works great IF I manually step through the code. If I execute it
normally, it does not update the main form field.

Your help would be greatly appreciated!
 

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