Form Control Name: Use in Variable in Form

  • Thread starter Thread starter Larry R Harrison Jr
  • Start date Start date
L

Larry R Harrison Jr

I have Access 97. I am trying to reference a form control's name in code. I
have this:

Dim sFr as Form
Set sFr=Forms![frmSchedule]

Then I have a multi-dimensional variable name which references certain text
boxes on the form. Yes, there is a reason I do this.

If a user has checked a check box on the form, the text box next to the
check box needs to change fore colors. The code I have looks like this:

For x = 1 To 7
For n = 1 To 3
If sFr.Controls(stDay(x, n, 1)).Value = True Then ' This line works just
fine
sFr.Controls(stDay(x, n, 2)).BackColor = 128
sFr.Controls(stDay(x, n, 2)).BackStyle = "Normal" ' This line-and possibly
the one above it--are the ones giving me trouble
'MsgBox "control " & stDay(x, n, 1) & "is a sample"
End If
Next n
Next x


The if..then does actually return proper conditions, as I checked it with
the shown MsgBox (commented out). Basically, the code works fine for
checking to see if the code box was checked, the problem is in setting the
corresponding text boxes' backstyle to normal rather than transparent.

Tips?

LRH
 
Try breaking the line down a bit to make it easier for Access to follow,
e.g.:
Dim strName As String
Dim ctl As Control

strName = stDay(x, n, 1)
Set ctl = sFr.Controls(strName)
ctl.BackColor = 128
 
I tried your code, I get a "object doesn't support this property or method"
error message


Allen Browne said:
Try breaking the line down a bit to make it easier for Access to follow,
e.g.:
Dim strName As String
Dim ctl As Control

strName = stDay(x, n, 1)
Set ctl = sFr.Controls(strName)
ctl.BackColor = 128

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Larry R Harrison Jr said:
I have Access 97. I am trying to reference a form control's name in
code.
I
have this:

Dim sFr as Form
Set sFr=Forms![frmSchedule]

Then I have a multi-dimensional variable name which references certain text
boxes on the form. Yes, there is a reason I do this.

If a user has checked a check box on the form, the text box next to the
check box needs to change fore colors. The code I have looks like this:

For x = 1 To 7
For n = 1 To 3
If sFr.Controls(stDay(x, n, 1)).Value = True Then ' This line works just
fine
sFr.Controls(stDay(x, n, 2)).BackColor = 128
sFr.Controls(stDay(x, n, 2)).BackStyle = "Normal" ' This line-and possibly
the one above it--are the ones giving me trouble
'MsgBox "control " & stDay(x, n, 1) & "is a sample"
End If
Next n
Next x


The if..then does actually return proper conditions, as I checked it with
the shown MsgBox (commented out). Basically, the code works fine for
checking to see if the code box was checked, the problem is in setting the
corresponding text boxes' backstyle to normal rather than transparent.

Tips?

LRH
 
I see why now. The flaw wasn't your code, but MINE (big surprise--you are
the MVP after all).

the strName should be set to stDay(x,n,2) not (x,n,1). See, #2 is the name
of the text box, #1 the name of the check box. Obviously you can't change
the colour of a checkbox.

Your code worked great, thanks.


Larry R Harrison Jr said:
I tried your code, I get a "object doesn't support this property or method"
error message


Allen Browne said:
Try breaking the line down a bit to make it easier for Access to follow,
e.g.:
Dim strName As String
Dim ctl As Control

strName = stDay(x, n, 1)
Set ctl = sFr.Controls(strName)
ctl.BackColor = 128

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Larry R Harrison Jr said:
I have Access 97. I am trying to reference a form control's name in
code.
I
have this:

Dim sFr as Form
Set sFr=Forms![frmSchedule]

Then I have a multi-dimensional variable name which references certain text
boxes on the form. Yes, there is a reason I do this.

If a user has checked a check box on the form, the text box next to the
check box needs to change fore colors. The code I have looks like this:

For x = 1 To 7
For n = 1 To 3
If sFr.Controls(stDay(x, n, 1)).Value = True Then ' This line works just
fine
sFr.Controls(stDay(x, n, 2)).BackColor = 128
sFr.Controls(stDay(x, n, 2)).BackStyle = "Normal" ' This line-and possibly
the one above it--are the ones giving me trouble
'MsgBox "control " & stDay(x, n, 1) & "is a sample"
End If
Next n
Next x


The if..then does actually return proper conditions, as I checked it with
the shown MsgBox (commented out). Basically, the code works fine for
checking to see if the code box was checked, the problem is in setting the
corresponding text boxes' backstyle to normal rather than transparent.

Tips?

LRH
 
Back
Top