Results 1 to 5 of 5
  1. #1
    uoghk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    149

    error 2465 cannot find field in subform

    I want to disable all textboxes in subform.


    I first loop through form controls to find out which control is a subform.
    Once find the subform, I loop through each textbox to set it disable.
    When it starts setting textbox "price" in the subform, i get an error 2465 saying : Cannot find the field "frmBills.Form.Price"

    In debug mode, i check the values:
    Me(Me.Form.Controls.Item(i).ControlName) = the name of the subform "frmBills"
    n= "price"

    Code:
    For Each Control1 in Me.Form.Controls
    	For i=1 to Me.Form.Controls.Count - 1
    		If Me.Form.Controls.Item(i).ControlType=112 '112=subform
    			For Each Control2 In Me(Me.Form.Controls.Item(i).ControlName).Form.Controls
    				For j = 1 To Me(Me.Form.Controls.Item(i).ControlName).Form.Controls.Count - 1
    					n = Me(Me.Form.Controls.Item(i).ControlName).Form.Controls.Item(j).ControlName
    					If Me(Me.Form.Controls.Item(i).ControlName).Form.Controls.Item(j).ControlType = 109 Then '109=textbox
    						Me(Me.Form.Controls.Item(i).ControlName & ".Form." & n).Enabled = False
    					End If
    				Next
    			Next Control2
    		End If
    	Next
    Next Control1

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    Not sure you should have Me( anywhere?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    No, Welsh, that should be appropriate syntax, except maybe the most inner line. That's where I get error. So I commented it and now error is on N= line which is odd because N does get value but code will not progress to next line.

    Missing Then on the first If.

    Got this to work:
    Code:
    For Each Control1 In Me.Form.Controls
        For i = 1 To Me.Form.Controls.Count - 1
            If Me.Controls.item(i).ControlType = 112 Then '112=subform
                For Each Control2 In Me(Me.Controls.item(i).Name).Form.Controls
                    For j = 1 To Me(Me.Controls.item(i).Name).Form.Controls.Count - 1
                        N = Me(Me.Controls.item(i).Name).Form.Controls.item(j).Name
                        If Me(Me.Controls.item(i).Name).Form.Controls.item(j).ControlType = 109 Then '109=textbox
                            Me(Me.Controls.item(i).Name).Form.Controls(N).Enabled = False
                        End If
                    Next
                Next Control2
            End If
        Next
    Next Control1
    Are there other controls than textboxes you want to leave enabled? If not, could just disable subform container control.

    Consider Conditional Formatting instead of VBA.
    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
    uoghk is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jan 2022
    Posts
    149
    thanks June7.

  5. #5
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,425
    Surely you know the name of the subform control and can reference it without having to "find" it?
    Have never seen that Me(Me syntax so maybe I learned something there, but have to wonder why anyone didn't suggest something other than all those counters and loops

    Code:
    Dim ctl As Control
    For Each ctl In Me.Child4.Controls 'where Child4 is the name of the subform control
       If ctl.ControlType = acTextBox Then ctl.Enabled = False
    Next
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 3
    Last Post: 05-31-2019, 01:52 PM
  2. Replies: 3
    Last Post: 10-10-2014, 06:29 PM
  3. Replies: 1
    Last Post: 12-01-2012, 12:44 PM
  4. Replies: 5
    Last Post: 06-09-2012, 12:37 PM
  5. VBA Referencing Subform Control Error 2465
    By Jester0001 in forum Programming
    Replies: 3
    Last Post: 05-30-2012, 07:31 AM

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