Results 1 to 7 of 7

Error 438 with a simple code

  1. #1
    Seiquo's Avatar
    Seiquo is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    May 2017
    Location
    Quebec
    Posts
    23

    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 Expert
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    713
    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
    46,082
    Instead of the locking code, try:

    DoCmd.OpenForm "FmPersonalInfo", , , , acFormReadOnly
    To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression. Attachment Manager is below Advanced editor window, click Go Advanced below Quick Reply window.

  4. #4
    Seiquo's Avatar
    Seiquo is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    May 2017
    Location
    Quebec
    Posts
    23
    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 Expert
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    713
    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 Novice
    Windows 10 Access 2013 32bit
    Join Date
    May 2017
    Location
    Quebec
    Posts
    23
    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 Expert
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    713
    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, 12:22 PM
  2. Simple Code for a Command Button
    By dvgef2 in forum Reports
    Replies: 5
    Last Post: 06-21-2013, 04:04 PM
  3. simple backcolor code ?
    By markjkubicki in forum Forms
    Replies: 7
    Last Post: 09-16-2011, 11:25 AM
  4. Simple code syntax question
    By mseeker22 in forum Programming
    Replies: 1
    Last Post: 07-07-2011, 02:55 AM
  5. simple VB code help
    By avworx in forum Programming
    Replies: 2
    Last Post: 03-29-2011, 06: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
  •  
Tech Forums: Microsoft Office Forums