Avatar of vlvawter
vlvawter

asked on 

Unable to disable controls on loading of form

I need to disable controls (or enable at another time) during runtime.  My very short code is listed.

In the debugger, stopping at "If TypeOf," ctl.Enabled shows as True; yet, when the next line run the error comes up "438, Object doesn't support this property or method."  Yet the Locals window clearly shows ctl with the property of Enabled, as mention at first.

I've searched online, and am giving up.  

Can someone help?
Private Sub Form_Load()
 
  Dim ctl As Control, frm As Form
  
  Set frm = Forms!frmPatientExams!sbfXmlData.Form
  
  For Each ctl In frm
    If TypeOf ctl Is TextBox Then
      ctl.Enabled False
    End If
  Next ctl
 
End Sub

Open in new window

Microsoft Access

Avatar of undefined
Last Comment
harfang
Avatar of vlvawter
vlvawter

ASKER

I forgot to mention that I'm using Access 2007.
Avatar of MikeToole
MikeToole
Flag of United Kingdom of Great Britain and Northern Ireland image

Add an = :

      ctl.Enabled = False

Avatar of vlvawter
vlvawter

ASKER

I had already done it with and without the =, same error.  I retested it.  Same error.
Avatar of DarklingG
DarklingG
Flag of United Kingdom of Great Britain and Northern Ireland image



 Try:

    ctl.Properties("Enabled") = True


 
 Using a generic 'Control' doesn't allow full access to the actual object properties. You could also try declaring a Textbox, then setting it equal to the ctl object. Then you would be able to access all the Textbox properties.

 C


Avatar of vlvawter
vlvawter

ASKER

Darkling,

I tried your ctl.Properties et al, and the error code I get is 2164, Can't disable while it has focus.

I don't understand how to set ctl to a Textbox and then loop through the control on a page.  (There are nearly 100 textboxes on this form.)
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

My 2p is that adding the = should be enough.
Avatar of DarklingG
DarklingG
Flag of United Kingdom of Great Britain and Northern Ireland image


 If the first control used access in your loop has TabIndex = 0, ie. it's the first control the Focus goes to when the form opens, then you will need to change the focus. If you have another control you can set the Focus to, a combobox maybe, then change the focus to your non-textbox object and then run your loop.

 As for the setting the Control to a Textbox see the code below:

Private Sub Form_Load()
Dim ctl As Access.Control
Dim newTextBox As Access.TextBox
 
 
    For Each ctl In Me.Controls
 
        If TypeOf ctl Is Access.TextBox Then
            Set newTextBox = ctl
            newTextBox.Enabled = False
        
        End If
            
    Next
 
End Sub

Open in new window

Avatar of vlvawter
vlvawter

ASKER

Below is my code.  I can comment out either ctl line and the error that now comes up is 2164, You can't disable a control while it has the focus.
Private Sub Form_Load()
 
  Dim ctl As Control, frm As Form
  
  Set frm = Forms!frmPatientExams!sbfXmlData.Form
  
  For Each ctl In frm
    If TypeOf ctl Is TextBox Then
      ctl.Properties("Enabled") = False
      'ctl.Enabled = False
    End If
  Next ctl
 
End Sub

Open in new window

Avatar of DarklingG
DarklingG
Flag of United Kingdom of Great Britain and Northern Ireland image


 The only way round this is to ensure that the Control you are trying to Disable does not have the Focus.

 The easiest way is to do this this is to choose an abitrary control (I prefer a button) and use:

      myButton.SetFocus


 Then you can loop through all your Textboxes and disable to your hearts content.


Avatar of vlvawter
vlvawter

ASKER

Darklin,

The tab I want to disable the controls on is a subform (as you can tell from my snippet).  If I want to disable the textboxes on that subform, do I need to set focus to a control on that page?  (I'll have to create a bogus control to do that.)  I tried setting focus on a control on a separate tab; however, it still would not work, same error.
ASKER CERTIFIED SOLUTION
Avatar of harfang
harfang
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo