How to activate code within a new record

J

John

How do I run code when entering a new record?

I have the following code in my form twice. Once in the Form_Current event
and again in the field "ResultMeasureID"_Lost focus event.

If Me!tbxMeasureYesNo = -1 Then
Me![tbxMeasureNumDesc].Visible = False
Me![tbxMeasureDenDesc].Visible = False
Me![ResultNum].Visible = False
Me![ResultDen].Visible = False
Me![tbxGoodTarget].Visible = False
Me![tbxResult].Visible = False
Me![ResultYesNo].Visible = True
Else
Me![tbxMeasureNumDesc].Visible = True
Me![tbxMeasureDenDesc].Visible = True
Me![ResultNum].Visible = True
Me![ResultDen].Visible = True
Me![tbxGoodTarget].Visible = True
Me![tbxResult].Visible = True
Me![ResultYesNo].Visible = False
End If

Know that the field "tbxMeasureYesNo" is being automatically populated once
the ResultMeasureID field is populated with an existing current measure
number.

The code above basically sets numerator and denominator fields visible or
not based on their applicability to a particular measure (e.g. if a measure
is yes/no, there is no need for numerator and denominator fields).

It works fine when I open the form and cycle through all the records, but
does not work when I add a new results record. I need it to run after
entering data in the ResultMeasureID field.
 
A

Al Campagna

John,
The OnCurrent code seems right, and by your account works.
when browsing from record to record.

As far as performing the same operation on a New record,
use exactly the same code, on the AfterUpdate of ResultMeasureID.
(OR... whatever else might effect the value of tbxMeasureYesNo )
--
hth
Al Campagna
Microsoft Access MVP
http://home.comcast.net/~cccsolutions/index.html

"Find a job that you love... and you'll never work a day in your life."


John said:
How do I run code when entering a new record?

I have the following code in my form twice. Once in the Form_Current
event
and again in the field "ResultMeasureID"_Lost focus event.

If Me!tbxMeasureYesNo = -1 Then
Me![tbxMeasureNumDesc].Visible = False
Me![tbxMeasureDenDesc].Visible = False
Me![ResultNum].Visible = False
Me![ResultDen].Visible = False
Me![tbxGoodTarget].Visible = False
Me![tbxResult].Visible = False
Me![ResultYesNo].Visible = True
Else
Me![tbxMeasureNumDesc].Visible = True
Me![tbxMeasureDenDesc].Visible = True
Me![ResultNum].Visible = True
Me![ResultDen].Visible = True
Me![tbxGoodTarget].Visible = True
Me![tbxResult].Visible = True
Me![ResultYesNo].Visible = False
End If

Know that the field "tbxMeasureYesNo" is being automatically populated
once
the ResultMeasureID field is populated with an existing current measure
number.

The code above basically sets numerator and denominator fields visible or
not based on their applicability to a particular measure (e.g. if a
measure
is yes/no, there is no need for numerator and denominator fields).

It works fine when I open the form and cycle through all the records, but
does not work when I add a new results record. I need it to run after
entering data in the ResultMeasureID field.
 
K

Klatuu

All has given you some good info and I would like to add to it.
The Lost Focus event would not be the correct event. It fires every time
you move away from the control regardless of whether you made a change to the
value of the control. To run the code when you enter a value, always use the
After Update event.

Also, if you want it to run only for new records, you want to test to see
that it is a new record. You do that with:

If Me.NewRecord then
'Do Stuff
End If

Or if you want to run it only when the value in the control is changed:

If Me.txtSomeControl <> Me.txtSomeControl.OldValue Then
'Do Stuff
End If.

And one last suggestion. You can reduce the code you write with techniques
lke this:

With Me
![tbxMeasureNumDesc].Visible = Not !tbxMeasureYesNo
![tbxMeasureDenDesc].Visible = Not !tbxMeasureYesNo
![ResultNum].Visible = Not !tbxMeasureYesNo
![ResultDen].Visible = Not !tbxMeasureYesNo
![tbxGoodTarget].Visible = Not !tbxMeasureYesNo
![tbxResult].Visible = Not !tbxMeasureYesNo
![ResultYesNo].Visible = !tbxMeasureYesNo
End With

It does the same thing as your current code.
 
J

John

Thanks for the new code - I understand it and see the logic. I was able to
get this to work with your and Al's help. I had tried the AfterUpdate event
before posting but it didn't work. After getting your and Al's confirmation
that it should, I scrutinized my code and realized my code was calling this
action before updating the "measureyesno" field. This was the root of my
problem.

--
QWERTY


Klatuu said:
All has given you some good info and I would like to add to it.
The Lost Focus event would not be the correct event. It fires every time
you move away from the control regardless of whether you made a change to the
value of the control. To run the code when you enter a value, always use the
After Update event.

Also, if you want it to run only for new records, you want to test to see
that it is a new record. You do that with:

If Me.NewRecord then
'Do Stuff
End If

Or if you want to run it only when the value in the control is changed:

If Me.txtSomeControl <> Me.txtSomeControl.OldValue Then
'Do Stuff
End If.

And one last suggestion. You can reduce the code you write with techniques
lke this:

With Me
![tbxMeasureNumDesc].Visible = Not !tbxMeasureYesNo
![tbxMeasureDenDesc].Visible = Not !tbxMeasureYesNo
![ResultNum].Visible = Not !tbxMeasureYesNo
![ResultDen].Visible = Not !tbxMeasureYesNo
![tbxGoodTarget].Visible = Not !tbxMeasureYesNo
![tbxResult].Visible = Not !tbxMeasureYesNo
![ResultYesNo].Visible = !tbxMeasureYesNo
End With

It does the same thing as your current code.
--
Dave Hargis, Microsoft Access MVP


John said:
How do I run code when entering a new record?

I have the following code in my form twice. Once in the Form_Current event
and again in the field "ResultMeasureID"_Lost focus event.

If Me!tbxMeasureYesNo = -1 Then
Me![tbxMeasureNumDesc].Visible = False
Me![tbxMeasureDenDesc].Visible = False
Me![ResultNum].Visible = False
Me![ResultDen].Visible = False
Me![tbxGoodTarget].Visible = False
Me![tbxResult].Visible = False
Me![ResultYesNo].Visible = True
Else
Me![tbxMeasureNumDesc].Visible = True
Me![tbxMeasureDenDesc].Visible = True
Me![ResultNum].Visible = True
Me![ResultDen].Visible = True
Me![tbxGoodTarget].Visible = True
Me![tbxResult].Visible = True
Me![ResultYesNo].Visible = False
End If

Know that the field "tbxMeasureYesNo" is being automatically populated once
the ResultMeasureID field is populated with an existing current measure
number.

The code above basically sets numerator and denominator fields visible or
not based on their applicability to a particular measure (e.g. if a measure
is yes/no, there is no need for numerator and denominator fields).

It works fine when I open the form and cycle through all the records, but
does not work when I add a new results record. I need it to run after
entering data in the ResultMeasureID field.
 

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