Reaching a subform through code

G

Guest

I want my subform background to change color if a checkbox is true. The
checkbox is in the subform. I have very minimal experience in vba. Can
someone please provide explicit instructions for this, please? I have gotten
this far:

I placed the following vba in the subform properties, under event - current,
and it works when I open the subform. However; when I open the mainform,
which contains the subform, it no longers works. It gives an error. The
error is listed below the vba statement.

Private Sub Form_Current()

If [Maximo] = True Then
[Forms]![SF-Parts with Vendors subform].Detail.BackColor = 255
Else
[Forms]![SF-Parts with Vendors subform].Detail.BackColor = 12632256
End If

End Sub

Error message is:
Run time error "2450":
Microsoft Access can't find the form "SF-Parts with Vendors subform"
referred to in a macro expression or visual basic code.
 
G

George Nicholson

http://mvps.org/access/forms/frm0031.htm
Refer to Form and Subform Controls and Properties

The name of a subform is useless when trying to establish a reference. A
subform is not considered to be "open", so it won't be found.

You can only "get" to a subform by referencing the Form property of the
Control containing the subform. Form is the default property of Subform
controls, so you don't have to explicitly include it, but it doesn't hurt to
do so until the concept is 2nd nature.

Note that if a subform control is created via drag & drop or a wizard it
*might* be given the exact same name as the subform itself. This can be
good or bad. It does tend to hide the distinction between the control and
the Form it contains, which can be very confusing for novice coders.
Personally, I find it easier to make sure that the 2 names are different.

HTH,
 
G

Guest

Thanks, that is a useful site. I will try that with my code on Monday. Have
a great weekend.

George Nicholson said:
http://mvps.org/access/forms/frm0031.htm
Refer to Form and Subform Controls and Properties

The name of a subform is useless when trying to establish a reference. A
subform is not considered to be "open", so it won't be found.

You can only "get" to a subform by referencing the Form property of the
Control containing the subform. Form is the default property of Subform
controls, so you don't have to explicitly include it, but it doesn't hurt to
do so until the concept is 2nd nature.

Note that if a subform control is created via drag & drop or a wizard it
*might* be given the exact same name as the subform itself. This can be
good or bad. It does tend to hide the distinction between the control and
the Form it contains, which can be very confusing for novice coders.
Personally, I find it easier to make sure that the 2 names are different.

HTH,


terrythomas@isp said:
I want my subform background to change color if a checkbox is true. The
checkbox is in the subform. I have very minimal experience in vba. Can
someone please provide explicit instructions for this, please? I have
gotten
this far:

I placed the following vba in the subform properties, under event -
current,
and it works when I open the subform. However; when I open the mainform,
which contains the subform, it no longers works. It gives an error. The
error is listed below the vba statement.

Private Sub Form_Current()

If [Maximo] = True Then
[Forms]![SF-Parts with Vendors subform].Detail.BackColor = 255
Else
[Forms]![SF-Parts with Vendors subform].Detail.BackColor = 12632256
End If

End Sub

Error message is:
Run time error "2450":
Microsoft Access can't find the form "SF-Parts with Vendors subform"
referred to in a macro expression or visual basic code.
 
G

Guest

I am still doing something wrong. I removed the code I had previously
entered into the subform and now this is the code I have entered into the
mainform properties for afterupdate (I have something else using the
oncurrent):

Private Sub Form_AfterUpdate()
Me!Subform1.Form!Subform2.Form.[Maximo]
If [Maximo] = True Then
Me!Subform1.Form.Detail.BackColor = 235
Else
Me!Subform1.Form.Detail.BackColor = 13434828
End If

Please help......

terrythomas@isp said:
Thanks, that is a useful site. I will try that with my code on Monday. Have
a great weekend.

George Nicholson said:
http://mvps.org/access/forms/frm0031.htm
Refer to Form and Subform Controls and Properties

The name of a subform is useless when trying to establish a reference. A
subform is not considered to be "open", so it won't be found.

You can only "get" to a subform by referencing the Form property of the
Control containing the subform. Form is the default property of Subform
controls, so you don't have to explicitly include it, but it doesn't hurt to
do so until the concept is 2nd nature.

Note that if a subform control is created via drag & drop or a wizard it
*might* be given the exact same name as the subform itself. This can be
good or bad. It does tend to hide the distinction between the control and
the Form it contains, which can be very confusing for novice coders.
Personally, I find it easier to make sure that the 2 names are different.

HTH,


terrythomas@isp said:
I want my subform background to change color if a checkbox is true. The
checkbox is in the subform. I have very minimal experience in vba. Can
someone please provide explicit instructions for this, please? I have
gotten
this far:

I placed the following vba in the subform properties, under event -
current,
and it works when I open the subform. However; when I open the mainform,
which contains the subform, it no longers works. It gives an error. The
error is listed below the vba statement.

Private Sub Form_Current()

If [Maximo] = True Then
[Forms]![SF-Parts with Vendors subform].Detail.BackColor = 255
Else
[Forms]![SF-Parts with Vendors subform].Detail.BackColor = 12632256
End If

End Sub

Error message is:
Run time error "2450":
Microsoft Access can't find the form "SF-Parts with Vendors subform"
referred to in a macro expression or visual basic code.
 
G

George Nicholson

I am still doing something wrong.

Um, not a lot to go on there... Does that mean nothing is happening? It
works sometimes but not others? You get an error message (what is it)?

Anyway, with my guessing hat firmly in place...

Guess1: Assuming Subform1 and Subform2 are the names of the controls
*containing* your subforms, I'd think you want something more like:

Private Sub Form_AfterUpdate()

If Me!Subform1.Form!Subform2.Form.[Maximo] = True Then
Me!Subform1.Form.Detail.BackColor = 235
Else
Me!Subform1.Form.Detail.BackColor = 13434828
End If

Guess2: Keep in mind that AfterUpdate only fires after you have edited or
added a record. It will *not* fire if you are simply surfing through
records, but the Current event will. It is not uncommon to have the same
code being called from both events: one will react to changes in the current
record, the other will "react" to the existing record you've just moved to.


HTH,


terrythomas@isp said:
I am still doing something wrong. I removed the code I had previously
entered into the subform and now this is the code I have entered into the
mainform properties for afterupdate (I have something else using the
oncurrent):

Private Sub Form_AfterUpdate()
Me!Subform1.Form!Subform2.Form.[Maximo]
If [Maximo] = True Then
Me!Subform1.Form.Detail.BackColor = 235
Else
Me!Subform1.Form.Detail.BackColor = 13434828
End If

Please help......

terrythomas@isp said:
Thanks, that is a useful site. I will try that with my code on Monday.
Have
a great weekend.

George Nicholson said:
http://mvps.org/access/forms/frm0031.htm
Refer to Form and Subform Controls and Properties

The name of a subform is useless when trying to establish a reference.
A
subform is not considered to be "open", so it won't be found.

You can only "get" to a subform by referencing the Form property of the
Control containing the subform. Form is the default property of
Subform
controls, so you don't have to explicitly include it, but it doesn't
hurt to
do so until the concept is 2nd nature.

Note that if a subform control is created via drag & drop or a wizard
it
*might* be given the exact same name as the subform itself. This can
be
good or bad. It does tend to hide the distinction between the control
and
the Form it contains, which can be very confusing for novice coders.
Personally, I find it easier to make sure that the 2 names are
different.

HTH,


message I want my subform background to change color if a checkbox is true.
The
checkbox is in the subform. I have very minimal experience in vba.
Can
someone please provide explicit instructions for this, please? I have
gotten
this far:

I placed the following vba in the subform properties, under event -
current,
and it works when I open the subform. However; when I open the
mainform,
which contains the subform, it no longers works. It gives an error.
The
error is listed below the vba statement.

Private Sub Form_Current()

If [Maximo] = True Then
[Forms]![SF-Parts with Vendors subform].Detail.BackColor = 255
Else
[Forms]![SF-Parts with Vendors subform].Detail.BackColor =
12632256
End If

End Sub

Error message is:
Run time error "2450":
Microsoft Access can't find the form "SF-Parts with Vendors subform"
referred to in a macro expression or visual basic code.
 

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