Conditional macro to set default value of field in a form

  • Thread starter Thread starter Lyn
  • Start date Start date
L

Lyn

I'm having trouble with creating this type of macro.
Currently, the macro is set up as follows:

Condition: [Sponsor]="XXX"
Action: SetValue

Item: [Forms]![PatientProtocol]![PatientID].[DefaultValue]
Expression: =[Forms]![AllPts]![XXX#]

The macro is set to run On Enter when XXX is entered into
the Sponsor field of the PatientProtocol form. My desire
is that when XXX is entered into the Sponsor field, the
macro will set the PatientID (also in the PatientProtocol
form) to equal the XXX# from the AllPts form (Note:
PatientProtocol is a subform of AllPts). However, when I
enter XXX into the sponsor field, I get an Access error
message that the database can't find the form
PatientProtocol, followed by an ActionFailed message.

Any suggestions?
 
Lyn,

There are a number of problems with what you have done so far...
1. The PatientProtocol form is not open, as the error message
indicates, so you can't refer to it in the macro. You need to refer to
the subform control.
2. The DefaultValue property of a control only applies in the case of a
new record, at the point where the new record is started, so in this
case you have already started the creation of the record, so
DefaultValue is not appropriate.
3. The On Enter event refers to when the focus moves to the control.
In this case, this event has already occurred, before you put the XXX
into the Sponsor textbox. The After Update event would probably be more
suitable.
4. I am not sure about this, without knowing more about what you are
doing, but you probably have an incorrect design to your form/subform.
It could be that the PatientID should be one of the fields listed in the
Link MasterFields and Link Child Fields properties of the subform, so it
is automatically entered into the subform records.

Anyway, try it like this...
Condition: [Sponsor]="XXX"
Action: SetValue
Item: [PatientID]
Expression: [Parent]![XXX#]
 
Thanks Steve - you are absolutely right RE 1-3; As for 4,
all records have an XXX# on the main form, but this will
only serve as the PatientID on the PatientProtocol subform
where XXX is the sponsor. This is why I wasn't able to
make this a linked master-child field.

Your solution works perfectly - thanks again.

-----Original Message-----
Lyn,

There are a number of problems with what you have done so far...
1. The PatientProtocol form is not open, as the error message
indicates, so you can't refer to it in the macro. You need to refer to
the subform control.
2. The DefaultValue property of a control only applies in the case of a
new record, at the point where the new record is started, so in this
case you have already started the creation of the record, so
DefaultValue is not appropriate.
3. The On Enter event refers to when the focus moves to the control.
In this case, this event has already occurred, before you put the XXX
into the Sponsor textbox. The After Update event would probably be more
suitable.
4. I am not sure about this, without knowing more about what you are
doing, but you probably have an incorrect design to your form/subform.
It could be that the PatientID should be one of the fields listed in the
Link MasterFields and Link Child Fields properties of the subform, so it
is automatically entered into the subform records.

Anyway, try it like this...
Condition: [Sponsor]="XXX"
Action: SetValue
Item: [PatientID]
Expression: [Parent]![XXX#]

--
Steve Schapel, Microsoft Access MVP
I'm having trouble with creating this type of macro.
Currently, the macro is set up as follows:

Condition: [Sponsor]="XXX"
Action: SetValue

Item: [Forms]![PatientProtocol]![PatientID]. [DefaultValue]
Expression: =[Forms]![AllPts]![XXX#]

The macro is set to run On Enter when XXX is entered into
the Sponsor field of the PatientProtocol form. My desire
is that when XXX is entered into the Sponsor field, the
macro will set the PatientID (also in the PatientProtocol
form) to equal the XXX# from the AllPts form (Note:
PatientProtocol is a subform of AllPts). However, when I
enter XXX into the sponsor field, I get an Access error
message that the database can't find the form
PatientProtocol, followed by an ActionFailed message.

Any suggestions?
.
 
Well - works perfectly except when I now go to close the
record I get the "can't save record at this time" error
message. (However, when I do close and reopen the record,
the data is still there).
-----Original Message-----
Lyn,

There are a number of problems with what you have done so far...
1. The PatientProtocol form is not open, as the error message
indicates, so you can't refer to it in the macro. You need to refer to
the subform control.
2. The DefaultValue property of a control only applies in the case of a
new record, at the point where the new record is started, so in this
case you have already started the creation of the record, so
DefaultValue is not appropriate.
3. The On Enter event refers to when the focus moves to the control.
In this case, this event has already occurred, before you put the XXX
into the Sponsor textbox. The After Update event would probably be more
suitable.
4. I am not sure about this, without knowing more about what you are
doing, but you probably have an incorrect design to your form/subform.
It could be that the PatientID should be one of the fields listed in the
Link MasterFields and Link Child Fields properties of the subform, so it
is automatically entered into the subform records.

Anyway, try it like this...
Condition: [Sponsor]="XXX"
Action: SetValue
Item: [PatientID]
Expression: [Parent]![XXX#]

--
Steve Schapel, Microsoft Access MVP
I'm having trouble with creating this type of macro.
Currently, the macro is set up as follows:

Condition: [Sponsor]="XXX"
Action: SetValue

Item: [Forms]![PatientProtocol]![PatientID]. [DefaultValue]
Expression: =[Forms]![AllPts]![XXX#]

The macro is set to run On Enter when XXX is entered into
the Sponsor field of the PatientProtocol form. My desire
is that when XXX is entered into the Sponsor field, the
macro will set the PatientID (also in the PatientProtocol
form) to equal the XXX# from the AllPts form (Note:
PatientProtocol is a subform of AllPts). However, when I
enter XXX into the sponsor field, I get an Access error
message that the database can't find the form
PatientProtocol, followed by an ActionFailed message.

Any suggestions?
.
 
Back
Top