Tags

Stemdriller

Registered User.
Local time
Today, 15:23
Joined
May 29, 2008
Messages
187
Hi

I've just been introduced to the wonderful world of 'Tags'

Using the following code on the 'On current' of the form, trying to get the code to check the textboxes, in the form that have the Tag name dAble
I the textbox has a value then disable it, else leave it blank.

But I think i am missing something.

Dim ctl As Control

If Value <> "" Then

For Each ctl In Me.Controls
If ctl.Tag = "dAble" Then
ctl.Enabled = False
End If
Next


End If


End Sub

Any help would be most appreciated.

Thanks

Gareth
 
You would need and Else on the second If that sets them as True.
 
Sorry, thats already in, just omitted it. DOH!

But the code is not checking the form. This form has 30 odd textboxes to check.
 
The problem is Value <> ""
I assume you don't have Option Explicit on the module. (You should.)

Value is taken as an undeclared string variable called Value.
As a new variable it is equal to the Null String ("" or vbNullString)
Consequently your test is always False.

The textbox it should be referred to as:
Me.textboxname

(Value is the default property so you can leave it out.)

Also this value could be Null or vbNullString so use this test:
If Len(Me.textboxname & vbNullString) = 0 Then

Since you have defined that you are checking controls by using the ctl variable then you don't need to specify the Controls collection. It is suffient to say:

For Each ctl In Me
 
The problem is Value <> ""
I assume you don't have Option Explicit on the module. (You should.)

Value is taken as an undeclared string variable called Value.
As a new variable it is equal to the Null String ("" or vbNullString)
Consequently your test is always False.

The textbox it should be referred to as:
Me.textboxname

(Value is the default property so you can leave it out.)

Also this value could be Null or vbNullString so use this test:
If Len(Me.textboxname & vbNullString) = 0 Then

Since you have defined that you are checking controls by using the ctl variable then you don't need to specify the Controls collection. It is suffient to say:

For Each ctl In Me

I see now, but how can I get it to check the other 30 TextBoxes?

Using If Len(Me.Text11 & vbNullString) = 0 'only checks the one textbox.
 
Can you clearly describe which controls you are testing and which contols you are wanting to disable?
 
Can you clearly describe which controls you are testing and which contols you are wanting to disable?

There are about 30 textboxes on each form, the form is linked to a specific table
On this form all textboxes start txtCSP1, txtCSP2 and so on, until txtCSP25.

All textboxes will have a corresponding Control CSP1, CSP2 etc
 
Try this. It set ButtonState to True or False then applies that to the Enabled property of the matching button.

No tags required but it relies on a numbering pattern. You need to number the controls less than ten to 01, 02 etc

Code:
Dim ButtonState As Boolean
Dim ctl as Control
 
For Each ctl In Me
 
   If Left(ctl.Name, 6) = "txtCSP" Then
      ButtonState = (Len(ctl & vbNullString) <> 0)
      ControlNumber = Right(ctl.Name , 2).
   End If
 
   Me.Controls("CSP" & ControlNumber).Enabled = ButtonState
Next
 
Last edited:
stemdriller

the thnig is iterating all the controls on a form is a bit of a blunt instrument. some controls may be visible, others not. there are numerous types of control.

each control type may or may not "possess" certain properties, and certain values may not be permitted for certain controls.

eg - without checking, I am not sure if you can "enable" a rectangle. I am not sure what would happen if you tried to assign an "illegal" value to a control. eg try and enter a space or a zls in a control bound to a date field.

Also, a control's "tag" is a "spare" property that can contain any text you desire - so you could actually have multiple tag settings

so all tehniques to "walk through" all the controls on a form, depend on the developer pre-determining what he wants to achieve, and setting the controls , either by using tags, or by naming them in a particular way, so that his aims can be achieved. It needs some pre-planning.

In practice it may be easier just to do this sort of thing - you only have to set it up once for each form. I am sure it is as easy to do this, as it is to set a tag for each control.

Code:
sub setcontrols(setting as boolean)
control1.enabled = setting
control2.enabled = setting
control3.enabled = setting
control4.enabled = setting
end sub

and then you can call this with either

setcontrols(true)
or
setcontrols(false)
 
Just a quick chime back in - the original code can be fixed by:
Code:
Private Sub Form_Current()
Dim ctl As Control
 
For Each ctl In Me.Controls
If ctl.Tag = "dAble" Then
[B][COLOR=red]  If Len(ctl.Value & vbNullString) > 0 Then[/COLOR][/B]
      ctl.Enabled = False
   [COLOR=red][B]Else[/B][/COLOR]
[COLOR=red][B]     ctl.Enabled = True[/B][/COLOR]
   End If
Next
 
End Sub
 
I am not sure if you can "enable" a rectangle.

Controls that have no Enable property cause an error someting like Property or Method 'Enable' not found.

If you (and Bob) read closely you will see Stemdriller needs to independently disable a control associated with each empty one of thirty textboxes.

Consequently the loop through the textboxes with a name pattern association to the matching command button really is the most appropriate technique.

In practice it may be easier just to do this sort of thing - you only have to set it up once for each form. I am sure it is as easy to do this, as it is to set a tag for each control.

Even if the requirement was to enable of disable thirty controls changing the property individually on each control is extremely inelegant. Why use thirty lines of code when it can be done in a handful?

Moreover if it was to be used on multiple form I certainly would not "set it up once for each form". Best practice would be to use a function and call it from each form providing the form object as an argument and the control naming pattern if it was not consistent across forms.

Notice I did not use the tag in my solution. Naming patterns are quite effective to select controls from a loop. They are especially effective as associating controls.

However the every innovative Stopher recently posted a grouping solution that used an invisible tab control. He had just come up with the idea on a post where we had been discussion the selection of controls using a tag, naming patterns or a Collection.

See post #8 in this thread.
 
Try this:


Code:
Dim i As Integer
For i = 1 to 30
    Me.Controls("CSP" & i).Enabled = (Nz(Me.Controls("txtCSP" & i).Value,"") <> "")
Next i

or if I've got the condition the wrong way round:

Code:
Dim i As Integer
For i = 1 to 30
    Me.Controls("CSP" & i).Enabled = (Nz(Me.Controls("txtCSP" & i).Value,"") = "")
Next i
And if you're not sure how many controls there are then just put On Error Resume Next at the start and increase the number of iterations.
 
Last edited:
I know you can do this based on tags or naming conventions

I just don't think that way is necessarily a better way than explicitly naming all the fields.

if nz(custname,"")="" then custname.enabled = true
if nz(address,"")="" then address.enabled = true
if nz(town,"")="" then town.enabled = true


it's easy to manage, and transparent
it also doesn't need you to go through renaming controls, just so your loop still works.
and then you retain meaningful control names.
you can use this idea to initiallise values based on the control type, or the control itself, which you can't do easily with an "all controls system"

and it only takes a few moments to set the control list up in the first place. Once it's done, it's done, and it's easy to edit afterwards.

of the two proposed solutions though, i think I prefer using the tag idea to iterating all the controls, for this purpose.
 
If meaningful names are needed (and they may not be - not all fields are names and addresses - sometimes numbers do meaningfully describe the field and when they do that's something you want to take advantage of to the max), then enums could be used to show what names the numbers correspond to.

Also I suspect these are unbound controls so don't correspond to fields in a table.

I do think 4 lines of code without individually named controls (controls identified by numbers) is better than 30 lines of code with individually named controls (controls identified by words). (Especially if what you have now is controls identified by numbers - you don't have to rename them all (60 of them!).)
 
Last edited:
of the two proposed solutions though, i think I prefer using the tag idea to iterating all the controls, for this purpose.

But the point is the tag idea won't help because you'd have to have an individual tag for each pair of controls.

Your solution:
CSP1.enabled = (nz(txtCSP1.Value,"")="")
CSP2.enabled = (nz(txtCSP2.Value,"")="")
CSP3.enabled = (nz(txtCSP3.Value,"")="")
...
CSP30.enabled = (nz(txtCSP30.Value,"")="")

(BTW: If ever you find yourself writing If condition Then boolean = True
save ink and write boolean = condition)

(with numbers replaced by 'meaningful names')
would be better than tags.

Surely,

For i = 1 to 30
Me.Controls("CSP" & i).Enabled = (Nz(Me.Controls("txtCSP" & i).Value,"") = "")
Next i

is neat, elegant, easy to adapt and expand upon and doesn't require renaming the existing controls
 
Last edited:
[referring to each control individually] ..... it's easy to manage, and transparent.

Transparent I will accept, but easy to manage????
It is incredibly clumsy and painfully tiresome to code.

Yet another way to do it is include the associated control name in the Tag. It relies on other controls not having a Tag (or using Stopher's transparent Tab control to group them.)

Code:
For Each ctrl In Me
      If ctrl.Tag <> "" Then: Me.Controls(ctrl.Tag).Enabled = (Nz(crtl,"")="")
Next

Dave isn't lazy enough to write concise code. :p :D
 
missinglinq said:
There's ALWAYS more than one way to skin a cat!

tongueout.jpg
 
Hi Everyone

Just a quick note to say a big thanks to all for your input.

I managed the get the form doing as I wanted, but the swines then changed their minds and decided to change what they wanted.

Oh well, at least I've learnt something and can perhaps use it again at some point.

Once again, many thanks

:o
 

Users who are viewing this thread

Back
Top Bottom