Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
| Search | Today's Posts | Mark Forums Read
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 26th, 2005, 10:18 AM
Friend of Wrox
 
Join Date: Aug 2004
Location: , , .
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reaching a subform through code !

Good Easter to everybody !
I've a button that can lock and unlock controls on a form for
data entry. The code has screen.activeform.controls as a way
to do this procedure. I would like to have a subform unlocking
procedure when clicking a button on the parent form. The question is:
would the subform be considered a control on main the form ? how would i have the controls on the subform unlocked when clicking a button on the main form?
regards Penta !


 
Old March 26th, 2005, 11:01 AM
Friend of Wrox
 
Join Date: Nov 2004
Location: Seattle, WA, .
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

The answer to the first question is Yes.

The answer to the second question is:

1) Check if the control type is acSubForm

2) If the control is a subform. You will loop through each control similar to the way you are with the main form. The trick will be how you address the controls in the subform. I [u]think</u> the syntax is:

For Each mainformctl in me.Controls
   For Each subformctl in mainformctl.FORM.Controls

The key being the .FORM. part to point to the controls inside of the subform.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old March 26th, 2005, 11:17 AM
Friend of Wrox
 
Join Date: Aug 2004
Location: , , .
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My problem is:
This code works:
1)For Each ctl In Screen.ActiveForm.Controls
        If TypeOf ctl Is TextBox Or TypeOf ctl Is ComboBox Then
             With ctl
                .Locked = False
                .Enabled = True
            End With
        End If
    Next
This code doesnt works:
2)For Each ctl In Screen.ActiveForm.Controls
     If TypeOf ctl Is Form Then
        If TypeOf ctl Is TextBox Or TypeOf ctl Is ComboBox Then
             With ctl
                .Locked = False
                .Enabled = True
             End With
        End If
      End If
    Next

Any suggestion to make code (2) works ?
Thanks very much for ur care.
rgds P.

 
Old March 26th, 2005, 11:51 AM
Friend of Wrox
 
Join Date: Nov 2004
Location: Seattle, WA, .
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

In your #2 option the problem is (and this is probably my fault) that you don't loop through the sub form

Dim Mctl as Control ' Main form controls
Dim Sctl as Control ' SubForm controls

For Each Mctl In Screen.ActiveForm.Controls
  If TypeOf Mctl Is Form Then
    For Each Sctl in Mctl.Form.Controls
      ' If there is a subform
      ' within your subform you should include
      ' another "For Each" on the sub-subform
      If TypeOf Sctl Is TextBox Or TypeOf Sctl Is ComboBox Then
        With Sctl
          .Locked = False
          .Enabled = True
        End With
      End If
    Next Sclt
  ElseIf TypeOf Mctl is TextBox or Type of Mctl is ComboBox then
    With Mctl
        ...
    End With
  End If
Next Mctl

It's probably my fault because I didn't show the "If ctl is Form then" between the two "For Each" statements in my sample.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old March 26th, 2005, 12:02 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Seattle, WA, .
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

BTW, a simpler way to do this would be to make this a callable routine:

Public Sub SwitchControls(pFrm as Form, pblnEnable as boolean)

Dim ctl as Control

For each ctl in pFrm.Controls
    If TypeOf ctl is Form then
        Call SwitchControls(ctl, pblnEnable)
    ElseIf TypeOf ctl is TextBox or TypeOf ctl is ComboBox
        With ctl
            .Locked = not pblnEnable
            .Visible = pblnEnable
        End With
    Else
        Msgbox "I forgot to handle this type of control!"
    End If
Next ctl

End Sub

To enable controls, in your form make this call from code in your form:

Call SwitchControls(Me,True)

To disable controls:

Call SwitchControls(Me,False)

NOTE: Doing this will automatically handled nested SubForms to any depth allowed by Access.

QED

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old March 26th, 2005, 12:05 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Seattle, WA, .
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

CORRECTION!!!!

The call in the routine should be:

Call SwitchControls(ctl.Form,pblnEnable)

Not:

Call SwitchControls(ctl,pblnEnable)

Sorry.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old March 26th, 2005, 12:07 PM
Friend of Wrox
 
Join Date: Aug 2004
Location: , , .
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks very much !
I'll try.
rgds P.

 
Old March 26th, 2005, 12:39 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Seattle, WA, .
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sorry. All of that won't work. But try this...

In your subform write a "Public Sub EnableControls" routine that does your "For Each" to enable the controls. But instead of using Screen.ActiveForm.Controls use Me.Controls.

In your main form, within your For Each include

   If ctl.Name = "your_subform_control_name" then
      Call Form_your_subform_name.EnableControls

I wish I had been granted the right to be correct all of the time. But I guess I'm destined to be humbled by sometimes being wrong.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old March 27th, 2005, 10:47 AM
Friend of Wrox
 
Join Date: Aug 2004
Location: , , .
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks very much again Randall !
The name of my subform is TabelaMeta_subform and on my If... procedure i could
not have the True so to enter the For Each sctl... in the subform. The code
goes to the End If because it doesn't recognizes the name of the subform ! I
must be doing something wrong !
rgds P.

 
Old March 27th, 2005, 11:21 AM
Friend of Wrox
 
Join Date: Aug 2004
Location: , , .
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Now i have it in the procedure For Each... on the subform. It was my mistake
having the wrong subform name. Unfortunately i'll have to continue tomorrow.
rgds P.





Similar Threads
Thread Thread Starter Forum Replies Last Post
General Naming(reaching) buttons kekec3778 Beginning VB 6 3 January 8th, 2008 05:05 PM
My Comments on reaching the end of the book Nick Y BOOK: Ivor Horton's Beginning Visual C++ 2005 0 August 5th, 2006 07:26 AM
Problem putting code on the right event - SubForm mmcdonal Access VBA 0 November 24th, 2004 01:55 PM
Urgent!! code for no match in Subform based query smercer Access 3 September 10th, 2004 11:15 PM
Filter Subform B Based on Field in Subform A SerranoG Access VBA 3 June 18th, 2004 12:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.