On Current Event - explanation

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

Guest

Could someone please explain to me what exactly this is for? From what I
read, I thought that you could put statements here that you wanted to happen
each time a new form was generated. I must be confused about this because
somethings I can put there and there's not a problem, but other statements
that work fine in other areas cause errors when I place them in the On
Current Event. Could someone shed some light on how to use this?

Thanks,
RandyM
 
WC,

The OnCurrent event fires when you move to a different record. A common use
of it is when you want the RowSource of a combo box to be filtered by another
combo box.

Sprinks
 
WCDoan said:
Could someone please explain to me what exactly this is for? From
what I read, I thought that you could put statements here that you
wanted to happen each time a new form was generated. I must be
confused about this because somethings I can put there and there's
not a problem, but other statements that work fine in other areas
cause errors when I place them in the On Current Event. Could someone
shed some light on how to use this?

Thanks,
RandyM

I'm not sure what you mean when you say "...each time a new form was
generated."

OnCurrent fires whenever the form displays a record and this includes when
the form is first opened.

So if I open the form to "record 1 of 10" then the current event fires when
that first record is displayed. If I then navigate to a different record
*by any means* then the event fires after each different record appears. It
would also fire if I stayed on record 1 and refreshed or requeried the form.

EXAMPLE:
I want to hide a certain control on the form when a different bound control
on the form contains a certain value. I would need to run that code every
time a different record is navigated to because each record could
potentially have a different value in the bound control being tested.
 
Thanks Rick for the explanation. I kinda thought that was what it did, but
I'm having a problem with this and I haven't a clue what to do about it. I've
read many of your answers to mine and other's questions and from that I know
you're extremely knowledgeable about Access. So, maybe you can answer this. I
have a main form that enters info on an owner. They then press a button to go
to subform to enter info on an animal they have. What I'm attempting to do is
keep a running count of the number of animals and display it on the main
form. It works fine, except that if you enter a new owner on the main form,
use the command button to go to the animal sub-form and enter the animal
info, when you return to the main form it's not reflecting this newly entered
animal. The count still is 0. If you go out of the main form, and come back
for this owner the correct number is there. I'm using this code:
CountOfAnimals = DCount("*", "tblAnimals", "[OwnerID_AnimalTbl] = " &
Me.OwnerID)
in a sub routine called ToggleLink_Click that I think was created when I
used the wizard to create the main form and subform and this code works fine
there. But, if I put this same code in the On Current Event, I get this error
message:

Syntax error (missing operator) in query expression '[OwnerID_AnimalTbl] = ".

I don't understand why the code works fine in one place and in another it
generates an error message. And, that's not even a query expression, is it?
While I've learned a lot over the past few months using Access and looking
thru this discussion group for answers, I'm still learning and when things
like this happen I haven't a clue where to look or what to look for. Would
you please enlighten me on why this works in one spot and not in the other?

Thank you for your time, and especially, your patience dealing with people
like me who are forever asking yall questions. Believe me when I say that I
am very appreciative of yall sharing your knowledge with us.

Thanks,
RandyM
 
In design view of your subform, view form header/footer.
In the footer add a text control with a control source of =Count([AnyField])
(suggest using the subform's key field for the 'AnyField', although probably
any field would do). Call this control txtAnimalCount.
On the main form, set the control source of the counter text box to
=[SubformControlName]![txtAnimalCount]

With this set up, you do not need any code to calculate the count as it is
bound to a control that dynamically updates as you add new animal records.

WCDoan said:
Thanks Rick for the explanation. I kinda thought that was what it did, but
I'm having a problem with this and I haven't a clue what to do about it. I've
read many of your answers to mine and other's questions and from that I know
you're extremely knowledgeable about Access. So, maybe you can answer this. I
have a main form that enters info on an owner. They then press a button to go
to subform to enter info on an animal they have. What I'm attempting to do is
keep a running count of the number of animals and display it on the main
form. It works fine, except that if you enter a new owner on the main form,
use the command button to go to the animal sub-form and enter the animal
info, when you return to the main form it's not reflecting this newly entered
animal. The count still is 0. If you go out of the main form, and come back
for this owner the correct number is there. I'm using this code:
CountOfAnimals = DCount("*", "tblAnimals", "[OwnerID_AnimalTbl] = " &
Me.OwnerID)
in a sub routine called ToggleLink_Click that I think was created when I
used the wizard to create the main form and subform and this code works fine
there. But, if I put this same code in the On Current Event, I get this error
message:

Syntax error (missing operator) in query expression '[OwnerID_AnimalTbl] = ".

I don't understand why the code works fine in one place and in another it
generates an error message. And, that's not even a query expression, is it?
While I've learned a lot over the past few months using Access and looking
thru this discussion group for answers, I'm still learning and when things
like this happen I haven't a clue where to look or what to look for. Would
you please enlighten me on why this works in one spot and not in the other?

Thank you for your time, and especially, your patience dealing with people
like me who are forever asking yall questions. Believe me when I say that I
am very appreciative of yall sharing your knowledge with us.

Thanks,
RandyM


Rick Brandt said:
I'm not sure what you mean when you say "...each time a new form was
generated."

OnCurrent fires whenever the form displays a record and this includes when
the form is first opened.

So if I open the form to "record 1 of 10" then the current event fires when
that first record is displayed. If I then navigate to a different record
*by any means* then the event fires after each different record appears. It
would also fire if I stayed on record 1 and refreshed or requeried the form.

EXAMPLE:
I want to hide a certain control on the form when a different bound control
on the form contains a certain value. I would need to run that code every
time a different record is navigated to because each record could
potentially have a different value in the bound control being tested.
 
Jon, thanks for replying. I tried what you suggested, but I'm getting the
infamous #Name error on the main form. In the control source on the main form
to reference the control source on the subform I'm using the following:

Forms![frmOwners]![frmAnimals].Form![txtAnimalCount]

Any ideas as to what's wrong.
Main form is frmOwners
Subform is frmAnimals
Control name on subform is txtAnimalCount

Thanks,
RandyM

Jon Ley said:
In design view of your subform, view form header/footer.
In the footer add a text control with a control source of =Count([AnyField])
(suggest using the subform's key field for the 'AnyField', although probably
any field would do). Call this control txtAnimalCount.
On the main fom, set the control source of the counter text box to
=[SubformControlName]![txtAnimalCount]

With this set up, you do not need any code to calculate the count as it is
bound to a control that dynamically updates as you add new animal records.

WCDoan said:
Thanks Rick for the explanation. I kinda thought that was what it did, but
I'm having a problem with this and I haven't a clue what to do about it. I've
read many of your answers to mine and other's questions and from that I know
you're extremely knowledgeable about Access. So, maybe you can answer this. I
have a main form that enters info on an owner. They then press a button to go
to subform to enter info on an animal they have. What I'm attempting to do is
keep a running count of the number of animals and display it on the main
form. It works fine, except that if you enter a new owner on the main form,
use the command button to go to the animal sub-form and enter the animal
info, when you return to the main form it's not reflecting this newly entered
animal. The count still is 0. If you go out of the main form, and come back
for this owner the correct number is there. I'm using this code:
CountOfAnimals = DCount("*", "tblAnimals", "[OwnerID_AnimalTbl] = " &
Me.OwnerID)
in a sub routine called ToggleLink_Click that I think was created when I
used the wizard to create the main form and subform and this code works fine
there. But, if I put this same code in the On Current Event, I get this error
message:

Syntax error (missing operator) in query expression '[OwnerID_AnimalTbl] = ".

I don't understand why the code works fine in one place and in another it
generates an error message. And, that's not even a query expression, is it?
While I've learned a lot over the past few months using Access and looking
thru this discussion group for answers, I'm still learning and when things
like this happen I haven't a clue where to look or what to look for. Would
you please enlighten me on why this works in one spot and not in the other?

Thank you for your time, and especially, your patience dealing with people
like me who are forever asking yall questions. Believe me when I say that I
am very appreciative of yall sharing your knowledge with us.

Thanks,
RandyM


Rick Brandt said:
WCDoan wrote:
Could someone please explain to me what exactly this is for? From
what I read, I thought that you could put statements here that you
wanted to happen each time a new form was generated. I must be
confused about this because somethings I can put there and there's
not a problem, but other statements that work fine in other areas
cause errors when I place them in the On Current Event. Could someone
shed some light on how to use this?

Thanks,
RandyM

I'm not sure what you mean when you say "...each time a new form was
generated."

OnCurrent fires whenever the form displays a record and this includes when
the form is first opened.

So if I open the form to "record 1 of 10" then the current event fires when
that first record is displayed. If I then navigate to a different record
*by any means* then the event fires after each different record appears. It
would also fire if I stayed on record 1 and refreshed or requeried the form.

EXAMPLE:
I want to hide a certain control on the form when a different bound control
on the form contains a certain value. I would need to run that code every
time a different record is navigated to because each record could
potentially have a different value in the bound control being tested.
 
I would have gone with

=[frmAnimals]![txtAnimalCount]

with the note that [frmAnimals] is the name of the subform control (this
could potentially be different to the actual name of the subform). Also note
that you need the "=" sign at the beginning. This seemed to work for me on a
very simple form/subform set up, although I will admit that I do get
frustrated with Access when you try and do the same thing in a very slightly
different scenario and it doesn't work. There's always a reason for it, but
it can be a real sod to work out what the reason is.

WCDoan said:
Jon, thanks for replying. I tried what you suggested, but I'm getting the
infamous #Name error on the main form. In the control source on the main form
to reference the control source on the subform I'm using the following:

Forms![frmOwners]![frmAnimals].Form![txtAnimalCount]

Any ideas as to what's wrong.
Main form is frmOwners
Subform is frmAnimals
Control name on subform is txtAnimalCount

Thanks,
RandyM

Jon Ley said:
In design view of your subform, view form header/footer.
In the footer add a text control with a control source of =Count([AnyField])
(suggest using the subform's key field for the 'AnyField', although probably
any field would do). Call this control txtAnimalCount.
On the main fom, set the control source of the counter text box to
=[SubformControlName]![txtAnimalCount]

With this set up, you do not need any code to calculate the count as it is
bound to a control that dynamically updates as you add new animal records.
 
Back
Top