Results 1 to 7 of 7
  1. #1
    Seiquo's Avatar
    Seiquo is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    May 2017
    Location
    Quebec
    Posts
    44

    Error 438 with a simple code

    Hello Forum,



    I am trying to open a form and lock all controls in said form. There are also two subforms in this form and I would like their controls to be locked too.

    I'm trying to work around the following code but my skills in VBA being what they are, I am stuck...

    Code:
    Private Sub CmdOpenPersonal_Click()
    Dim ctrl As Control
    
    DoCmd.Close 'closes the main "welcome" form where CmdOpenPersonal button is
    DoCmd.OpenForm "FmPersonalInfo"
    
    For Each ctrl In Forms("FmPersonalInfo").Controls
         ctrl.Locked = True
    Next ctrl
    End Sub
    Each time I click on CmdOpenGeneral, I get the "Error 438 - object doesn't support this property or method" and VBA points to ctrl.Locked = True

    Is it because two of the controls in my form are subforms?

    Your help would be much appreciated... And thank you in advance.

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    You are getting that probably because your code is trying to lock a label or other control (line, rectangle) that has no value property. You can add On Error Resume Next at the beginning of the sub or check the control type (https://docs.microsoft.com/en-us/off...ox.controltype).
    if ctrl.Controltype=actextbox or ctrl.Controltype=acCombobox .... then
    Cheers,
    Vlad

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,899
    Instead of the locking code, try:

    DoCmd.OpenForm "FmPersonalInfo", , , , acFormReadOnly
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Seiquo's Avatar
    Seiquo is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    May 2017
    Location
    Quebec
    Posts
    44
    Thank you Gicu and June 7,

    Gicu, isn't the purpose of

    Code:
    dim ctrl as control
    preventing the kind of problem you are writing about?? I am confused...

    June7,
    In this form, I have a combobox that allows users to pick a name and refresh the query in order to display the appropriate information. I cannot use the acFormReadOnly option unfortunately.

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    A label is also a control and you cannot lock a label....

    You can set the form's recordset type to snapshot and that should allow your combobox to still work.

    Cheers,
    Vlad

  6. #6
    Seiquo's Avatar
    Seiquo is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    May 2017
    Location
    Quebec
    Posts
    44
    Thank you Gicu!

    I didn't know a label was also a control, speaking of forms. You taught me something!

    Then I suppose I should add this to my code:

    Code:
    For Each ctrl In Me.Controls
      If TypeOf ctrl Is TextBox Then
       ctrl.Locked = True
      End If
    My problem is I also have checkboxes and combos. I want everything locked except the unbound combo that allows to filter the information).

    How can I do this?

    Should it be too complicated, my last option is to reference the controls one by one in my code...

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    if ctrl.ControlType=acTextBox Or ctrl.ControlType=acComboBox Or ctrl.ControlType=acCheckBox Or ctrl.ControlType=acListBox Then Ctrl.Locked=True

    Have you tried to see if changing the recordsettype of the form works for you? Forms!YourFormName.Form.RecordsetType = 2 'snapshot (or Me.RecordsetType =2 if the code is on the same form)

    Cheers,
    Vlad

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Setting Code to Simple Numeric Age
    By wes9659 in forum Forms
    Replies: 6
    Last Post: 08-14-2014, 01:22 PM
  2. Simple Code for a Command Button
    By dvgef2 in forum Reports
    Replies: 5
    Last Post: 06-21-2013, 05:04 PM
  3. simple backcolor code ?
    By markjkubicki in forum Forms
    Replies: 7
    Last Post: 09-16-2011, 12:25 PM
  4. Simple code syntax question
    By mseeker22 in forum Programming
    Replies: 1
    Last Post: 07-07-2011, 03:55 AM
  5. simple VB code help
    By avworx in forum Programming
    Replies: 2
    Last Post: 03-29-2011, 07:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums