Looping thru 2 differetn CtrlTypes

Colby

New member
Local time
Yesterday, 18:39
Joined
Dec 13, 2005
Messages
105
I have a finished application except the fine tuning and this is where
you (whoever is reading this) comes in.
I have one form with 3 pages (tabsheets) on it. On each sheet are two
columns of labels and text boxes. I used the same naming convention on
both (Label200, Text200). What I am trying to do is...

if Label200 does not have any text then I want Text200 to go to
a transparent background. the reverse would apply also, if L200 has
text then don't change anything on T200. I have this format of coding
so far, but am stuck. The red in the code is where
I am stuck; please help. Want to get this one off the to-do list.

Function Set_Date_Box()

For Each ctrl In Forms("frm_Checklist").Controls
If ctrl.ControlSource = acTextBox Then
i = ctrl.Name 'gets the textbox name
here is where my problem lies. I think I need to loop thru my labels until I get a name match. once i get the name match check the len of the label and set the properties.
End If
End If
End If
Next ctrl

End Function
 
Is this something that you want to check once each time you go to the next record? I am not really sure this is what you are talking about but this would run a command to hide the textbox if the label is blank and would recheck and hide/unhide textboxes each time you change to a different record.

If this is the case then you could code the command to run on an event.

The event you would want to use would the the On Current Event for the Form Properties. (See Attachment)

Private Sub Form_Current()
If IsNull(Me.Label200) = True Then
'MsgBox ("Label200 is Blank")
Me.Text200.Visible = False
Else
'MsgBox ("Label200 Has Data!!")
Me.Text200.Visible = True
End If

End Sub
 

Attachments

  • oncur.jpg
    oncur.jpg
    48.4 KB · Views: 129
Chris, thanks for the reply. It helped me look at things a different way.
I am now looking at
For Each ctrl In Forms("frm_Checklist").Controls
If ctrl.ControlType = acLabel And ctrl.Name Like "Label*" Then
l = Len(Trim(ctrl.Caption)) 'gets the length
If l < 1 Then 'if the label was blank
i = Right(ctrl.Name, Len(ctrl.Name) - 5) 'rtns the numerical id of the label
MsgBox i
Eval ("Forms(""frm_Checklist"")![Text" & i & "].BackStyle = 0")
Else
'MsgBox ("Label200 Has Data!!")
'Me.Text200.Visible = True
End If
End If
Next ctrl


the issue now is that I can not get Access to recognize the textbox. Any ideas.
 
Observation: If your control names have been so formatted you gain some efficiency using a For...Next loop rather than For...Each...Next, which traverses the entire controls collection of the form.

Code:
Dim i As Integer
[COLOR="Green"]'traverse only numbered labels like "Label?00"[/COLOR]
For i = 100 To 500 step 100
[COLOR="Green"]  'shows textbox "Text?00" if "Label?00" has a caption, otherwise hides it[/COLOR]
  Me.Controls("Text" & i).Visible = (Trim(Me.Controls("Label" & i).Caption) <> "")
Next i

Cheers
 
Lagbolt, that is perfect! Thanks so much. You took 5 hrs of work and
condensed it into about 60 seconds of typing. Live, learn and re-use in
the next app.

Thanks again.
 

Users who are viewing this thread

Back
Top Bottom