Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to access a text box by referencing the index number 2

Status
Not open for further replies.

maupiti

Programmer
Oct 27, 2003
240
US
Access 2003

Run-time error 438. Object does not support this property or method.

///////////////////////////////////////

Public Sub Public_Disable_Text_Boxes(Form_Name, Public_Department_Name)
Dim frm As Form
Dim ctl As Control

Set frm = Form_Name
Select Case Public_Department_Name
Case "Purchasing"
For Each ctl In frm.Controls
If Val(ctl.Index) >= 0 And Val(ctl.Index) <= 40 Then
ctl.Enabled = True
Else
ctl.Enabled = False
End If
Next ctl
End Select
End Sub
 
Code:
For i = 0 to 40
  Frm.Controls.Item(i).Enabled = True
Next

Then you could either use another loop to disable the rest of the controls...
Code:
For j = 41 to Frm.Controls.Count - 1
   Frm.Controls.Item(j).Enabled = False
Next

...or you could disable all of your controls in Design view, and just use the first loop to enable the first 40.
 
Hi!

Just check the controltype:

If cntl.ControlType = acTextBox Then
cntl.Enabled = False
End If

Of course your code will depend on what you are trying to do. This code will disable all text boxes on the form and do nothing to any other control. Your error is probably coming from the fact that your code tries to access the enabled property of any control that comes through the loop and not all controls have an enabled property (labels for instance).

hth


Jeff Bridgham
Purdue University
Graduate School
Data Analyst
 
Hi rjoubert.

I have an error that said "Object Required".
The yellow higlight is at Set frm = Form_Name

////////////////////////////////////

Private Sub Form_Open(Cancel As Integer)

Call Public_Disable_Text_Boxes("Non_Conformance",
Public_Department_Name)
End Sub

///////////////////////////////////

Public Sub Public_Disable_Text_Boxes(Form_Name,
Public_Department_Name)
Dim frm As Form
Dim ctl As Control
Dim I As Integer

Set frm = Form_Name
Select Case Public_Department_Name
Case "System"
For I = 0 To 58
frm.Controls.Item(I).Enabled = True
Next I
End Select
End Sub
 
Hi!

Set frm = Forms(Form_Name) will set the form properly.

hth


Jeff Bridgham
Purdue University
Graduate School
Data Analyst
 
Your Form_Name parameter is probably a String. So you will need to get a reference to your Form by doing something like this...

Set frm = Forms.Item(Form_Name)
 
Hi jebry and rjoubert, and thank you for your help.

First, I use Set frm = Forms.Item(Form_Name), and then I
use Set frm = Forms(Form_Name), and I got the same error
in each code.

"Run-time error 438. Object does not support this
property or method."

All 20 of the controls are text boxes.

////////////////////////////////////

Private Sub Form_Open(Cancel As Integer)

Call Public_Disable_Text_Boxes("Non_Conformance",
Public_Department_Name)
End Sub

///////////////////////////////////

Public Sub Public_Disable_Text_Boxes(Form_Name,
Public_Department_Name)
Dim frm As Form
Dim I As Integer

Set frm = Forms.Item(Form_Name)
'Set frm = Forms(Form_Name)
Select Case Public_Department_Name
Case "System"
For I = 0 To 20
frm.Controls.Item(I).Enabled = True
Next I
End Select
End Sub
 
Have you stepped through the code to see where it is erroring out?
 
Hi!

Again, all you need to do is check the controltype to be sure that you are not accidentally hitting some other type of control. For instance, if the text boxes have there attached labels your For Each loop will hit those too.

hth


Jeff Bridgham
Purdue University
Graduate School
Data Analyst
 
Hi jebry and rjoubert.

I make the code even more simpler and I got this error
"Application-defined or object defined error".

The error is at
frm.Control.Item(3).Enabled

Item(3) is a text box, and I even delete its label and it still gives me this same error. The label has a "Tag" property and not "Tab Index" property like a text-box , so the label was not the problem.

////////////////////////////////////////

Private Sub Set_Disable_Box()
Dim frm As Form

Set frm = Forms!Non_Conformance
frm.Control.Item(3).Enabled <--- ERROR
End Sub
 
Hi!

The collection is called Controls so that might be the problem in the code above. I guess I am confused about how you know that Item 3 is a text box? Did you print it's name at some point? Try this:

Dim frm As Form
Set frm = Forms!Non_Conformance
MsgBox frm.Controls.Item(3).Name

Out of curiosity, what exactly are you trying to accomplish? If you want to disable all of the text boxes on the form, I have already given you code that will do that. If you need to disable only specific text boxes then I can give you better way then the Item property, but you will have to rename the text boxes to be disabled to make them easy to find.

hth


Jeff Bridgham
Purdue University
Graduate School
Data Analyst
 
Hi jebry. I did what you have suggested, and it turns out that MsgBox frm.Controls.Item(3).Name
is a label and not a text box, and this is why it gave me the error. This is a good suggestion from you.

You asked "Out of curiosity, what exactly are you trying to accomplish? "

I have a combination text boxes and check boxes all total up to 63. If a user belongs to a particular department group then it enabled and disabled a number of text boxes.

I have assigned 63 of these controls in the order of 0 to 62 on the "TAB INDEX" of the property of each controls.
The code that you gave me would disabled all of the control.

What I was doing is to disabled only a number of control
(ex: From 0 to 20 and From 30 to 40). This is why I use "TAB INDEX", but apparently the index that you use is built-in index and not "TAB INDEX".

Both, the label and the text boxes, have built-in indexes that are not displayed on the properties sheet. When I mentioned index, I meant "Tab Index". Text boxes have Tab Index, but not labels. It 's my fault for not specifying clearly.

///////////////////////////////////////////

To look at the "TAB INDEX" of a text box, highlight a text box, and
Right click --> Properties --> Other TAB --> "Tab Index"

How would I access the "TAB INDEX" value of the text box
using VBA ?
 
This seems like you are making this really hard

1)Click on all the controls of interest
2)Bring up the properties window and put something in the Tag property. I put the word "Selected" withou quotes
3)
Private Sub Command78_Click()
Dim cntrl As Access.Control
'Ensure that you do not disable a control with focus
Me.Text84.SetFocus
For Each cntrl In Me.Controls
If cntrl.Tag = "Selected" Then
cntrl.Enabled = False
End If
Next cntrl
End Sub
 
Hi!

MajP's suggestion is good. Since you will be enabling and disabling the controls based on the group then put the group name in the Tag property for the controls which will be left enabled for that group, then your code would be:

If cntl.ControlType = acTextBox Then
If cntl.Tag <> "Group Name" then
cntl.Enabled = False
End If
End If

Of course you will need to check that the control is a text box.

hth


Jeff Bridgham
Purdue University
Graduate School
Data Analyst
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top