Enabling Multiple Fields on a form

Daveyk01

Registered User.
Local time
Today, 11:51
Joined
Jul 3, 2007
Messages
144
I have a form that if the user presses a button, 18 fields should enable. Then after they perform a function, I want to lock those same 18 fields.

I thought this would have worked for the enable:

For Z = 1 To 6
strField = "GainAmp+" & Trim(Str(Z))
Me![strField].Enabled = True
Next Z

Can I refer to a field via a string name?
 
PS - I am already using the TAG parameter to automatically un-enable very control on the form at OPEN. That way, I only enable the fields as needed.
 
Look into the code I have rewritten for you. I don't understand what for the + symbol you used. Assuming it as your attempt to concatenate, I rewrite the code
Code:
Dim strField As String

For Z = 1 To 6

         strField = "GainAmp" & Trim(Str$(Z))
         Me.Controls(strField).Enabled = True

Next Z
 
Last edited:
You can't necessarily use Me.Controls(strField).Enabled = True if the value of strField isn't the TEXT BOX NAME, not the field. If they are the same it may work but it definitely won't work if the field name is used as fields don't get enabled or disabled, the controls do.
 
I don't understand what bob says. It is not a good practice to post without reading the original post. I think he meant the values of 'ControlSource' and 'Name' of the textbox control. If so I am sure that my code will work when they are alike or different, or bound or unbound.
Still I dont understand what he meant by his posting or in which context it will not work. I used to post any code only after a try out in an access db in my computer because I must not mislead others
 
I don't understand what bob says. It is not a good practice to post without reading the original post. I think he meant the values of 'ControlSource' and 'Name' of the textbox control. If so I am sure that my code will work when they are alike or different, or bound or unbound.
Still I dont understand what he meant by his posting or in which context it will not work. I used to post any code only after a try out in an access db in my computer because I must not mislead others

I guess we are stuck on a matter of semantics because the user who posted originally said they wanted to lock fields. The code they posted was not explicit in whether GainAmp was a field name or a text box name.

I tried your code exactly in my computer and if it was referring to a field name, which is what this person had stated, it fails on error saying that it cannot find the control. The control name must be used and not the field name. Now, if the field name and the control name are the same, it is possible that Access will allow that to work because it is looking for a control name and if it finds an existing control name by that name then it should work.

Remember, that posters here will not necessarily follow programming guidelines at first, so you cannot assume that they know that the field name and the control name can be the same (in fact Microsoft's wizards do it that way) and that is not good programming practice. But, if a person adds a text box onto a form by just adding the text box and then binding it afterwards (instead of dragging and dropping it from the field list) the names will be totally different.

So, what I was getting at is just that you must use the CONTROL name in your code, regardless, for it to work.
 
It is the control name I used. It is not my fault that u did not make controls with that name. Make six controls (say text boxes) with names GainAmp1, GainAmp2.......GainAmp6 and a command button on the form. On the OnClick event of the command button paste my code. Then say whether it will work or not.
Still you dont say in which context it will not work. I am sure it is not a matter whether the 'ControlSource' or 'Name' are same or not.

Clearly read what 'Daveyk01' actually wants. I copy it here
"
I thought this would have worked for the enable:

For Z = 1 To 6
strField = "GainAmp+" & Trim(Str(Z))
Me![strField].Enabled = True
Next Z
"

What he says is there are 18 buttons. It may be in three groups 6x3=18. First he wants to enable them when a command button is clicked. His problem may be to refer the controls in code in a For...Next loop and he posted what he knows and he wants to know what is the error OR how it can be corrected. I have answered that question.

But bob says that my code will not work if the 'ControlSource' and 'Name' are different for a control in reference. I argue that it will work in both cases when they are different or alike. Again you say that the code did not work when you used without the controls in your form or controls not having the name specified. Are you very particular that it must not work; for that you will not use the 'Name' specified in the code for the controls? Only thing the code need to work is to give the names of the controls (say text boxes. But it can be combo boxes/command buttons/...... as well) as specified in the code. The code will not bother for the 'ControlSource' (You say it as Field Name... It is an incorrect usage) whether it is the same or different as that of the 'Name'.

So what I say is that your first response ("........If they are the same it may work but it definitely won't work if the field name is used as fields ........") is totally incorrect. Atleast admit that fact bob.

Or you say what is the alternate line of code needed and what is its benefit over mine
 
Last edited:
use the tag property

hi there

use the tag property - as a previous poster suggested.

loop through all controls on form, check each tag property, call appropriate routine

s'easy ...
 
So what I say is that your first response ("........If they are the same it may work but it definitely won't work if the field name is used as fields ........") is totally incorrect. Atleast admit that fact bob.
GeeKay: I TESTED it BEFORE I posted and your code does NOT work if you use the FIELD NAME (name of the column in the table) and not the CONTROL name (name of the text box) if the text box is not the same name as the control. I don't know why you can't see that you are wrong in telling me that statement is wrong. I think you should try it again. Have a field named GainAmp and a control named Text1 and see what happens if you try to use

strField = "GainAmp" & Trim(Str$(Z))
Me.Controls(strField).Enabled = True

Me.Controls references CONTROLS - CONTROLS - CONTROLS and NOT FIELDS.

Also Me!FieldNameHere.Enabled WILL NOT WORK IF THE FIELD NAME IS NOT THE SAME AS THE CONTROL as fields do not have an enabled property - CONTROLS do. If you try to use that syntax, you get an OBJECT OR METHOD NOT FOUND error.
 
davey

For Z = 1 To 6
strField = "GainAmp+" & Trim(Str(Z))
Me![strField].Enabled = True
Next Z

a) this code only deals with 6 fields
b) do you really have the plus sign in the control name


but the syntax ought to be

For Z = 1 To 6
controls("GainAmp" & z).enabled = true
controls("GainAmp" & z).visible = false
Next Z

where GainAmpn is the text box (ie control name) name which may well be the same as the underlying control source
 
Bob is still telling what I did not mean. Please see what I said clearly.

"..........Only thing the code need to work is to give the names of the controls (........) as specified in the code............"

I reapeat; the code will work irrespective of the field name. it may be diiferent or same as that of the name of the control. If you are very particular not to give the name to the control in a different way, I can say it is an argument for the argument sake.
We can awake a person who is sleaping. But it is difficult to make him awake if he is pretending as sleaping. I will not post anymore in this thread.........BYE........
 
Look into the code I have rewritten for you. I don't understand what for the + symbol you used. Assuming it as your attempt to concatenate, I rewrite the code
Code:
Dim strField As String

For Z = 1 To 6

         strField = "GainAmp" & Trim(Str$(Z))
         Me.Controls(strField).Enabled = True

Next Z

The plus symbol was in the field name. Such as:
GainAmp-4
GainAmp-2
GainAmpSet
GainAmp+2
GainAmp+4
GainAmp+6

I think since then I changed the name to:
GainAmp4n
GainAmp2n
GainAmpSet
GainAmp2
GainAmp4
GainAmp6

I also discovered that you can make the TAG line as long as needed (within reason) such as: "INV STEP3 STEP7" ect....

Then just search the TAG parameter with an INSTR to enable disable multiple controls at once in a function like this:
Public Sub HideControls()

Dim MyForm As Form
Dim ctl As Control

Set MyForm = Screen.ActiveForm

' Disable each control on the form that has the Tag property set to True.
For Each ctl In MyForm.Controls
' Only check data entry type controls.
If InStr(ctl.Tag, "STEP3") Then
ctl.Visible = False
End If
Next ctl
End Sub
 

Users who are viewing this thread

Back
Top Bottom