Need help w/code that uses collection objects

arage

Registered User.
Local time
Today, 08:00
Joined
Dec 30, 2000
Messages
537
I’d like to be able to do the following but am still rusty in this area. I’d like to refer to all the controls as a group on a control tab, cycle thru them & provide an error message for each null one that I hit. My thinking is like this:

'for each control on the CPR tab:
'read the control
'-provide an error message if it is null...
'read the next control...
‘provide err messages as applicable…
'after reading the last control exit the loop if it is not null...
 
I don’t think so Talismanic as my control names are all different, now I’m thinking maybe if I could use the controls object to access the tab index on the tab control & use that to set my counter.
 
Hi

Judging by your question you wish to refer to a group of controls from one procedure i.e using a control array.

To do this you need to use the Controls Collection. I've done something similar where I want my controls on a subform only to be enabled at certain point. The code is below:

Public Sub EnableControls(frm As Form)

' This module will allow the of enabling / disabling the controls on any form

Dim ctl As Control

For Each ctl In frm.Controls
If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then

ctl.Enabled = True
End If
Next ctl

End Sub

This procedure is called when a user clicks a command button on the main form and the name of the subform is passed to this procedure and is held in the frm variable.

Hopefully this may be a starting point for you. Also because you are using tab controls as you only want to refer the controls on one tab at a time you may want to consider looking at the ActiveControl object i.e set the focus to the relevant tab and assign the tab number to a variable and pass that value to the procedure above. This may or may not be useful.

HTH
Rob
 
Thanks Rob I’ll check it out, I looked at something very similar in the access97 help & that’s more along the lines of what I meant in my initial post. The way I’ve been doing it for now is a lengthy set of if statements, one for each control & so I know there’s got to be a better way.
 
Set the Tag property for the fields that you want to validate and then use

Dim ctl As Control

Dim strctlvalue As String


For Each ctl In Me.Controls

If ctl.Tag = "whatever" Then
strctlvalue = Nz(ctl.Value, 0)
If strctlvalue = "0" Then
ctl.SetFocus
MsgBox "Blah Blah."
Exit Sub
End If
Next ctl

It's so weird how people seem to want to do the same things in the same week. I just wrote this procedure yesterday.

HTH
Charity
 
Above
Hey thanks Charity, I was always wondering when I might end up the tag property for something, now I got my chance, cool idea.

“It's so weird how people seem to want to do the same things in the same week. I just wrote this procedure yesterday.”

That IS weird
smile.gif
 
I modified Charity’s code a little to suit my needs even more but am stuck on my Call to errReqFld(ctl.caption) it seems that ctl is an object that doesn’t support the caption property, but if ctl has been defined as a control shouldn’t it support it? Thanks in advance.

Hey Charity, you were off by one end if in your post (and now the student becomes the teacher, hehe..)

New code:

Dim ctl As control
Dim strctlvalue As String

For Each ctl In Me.tabCPR.Controls

If ctl.Tag = "Required" Then
strctlvalue = Nz(ctl.value)
If strctlvalue = "" Then
Call errReqFld(ctl.caption)
ctl.setFocus
Exit Sub
End If
End If
Next ctl
 
Sorry for the missing end if. I did some modifications on my procedure to simplify it. I guess I took out one line too many. Not all control have the caption property. Part of the code that I took out was an if then to determine the controltype and set some properties from that. (I found that not all controls have the locked property). Now I am using a select case...so my code looks like this

For Each ctl In Me.Controls
If ctl.Tag = strStatus Then
ctl.Locked = False
Else
Select Case ctl.ControlType
Case acLabel, acCommandButton, acTabCtl, acPage
GoTo skip_ctl
Case Else
MsgBox ctl.Name
ctl.Locked = True
End Select
End If
skip_ctl:
Next ctl

I list all of the controltypes on my form that don't support the locked property and then skip it if my current ctl is one of those types.

What might make it easier for you though, is to just not assign the tag for a conrol that doesn't support captions. I'm using the tag for more than one thing, so I don't have that option.

Good Luck
Charity
 

Users who are viewing this thread

Back
Top Bottom