Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    TOPSie is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    101

    Creating and deleting Controls (text boxes)

    Frustrated by the absence of GridView I am trying to make one up using TextBoxes. But I am getting some odd results.
    If I create a couple of TextBoxes:-


    Code:
     
    Set frm = Forms(strForm)    
    wLeft = wFirstLeft
        wTop = wfirsttop
         DoCmd.OpenForm strForm, acDesign
          Set ctl = CreateControl(strForm, acTextBox, acDetail, , , wLeft, wTop, wWidth, wHeight)
            ctl.Name = "txt" & "82001"
        Set ctl = Nothing
        DoCmd.Close ObjectType:=acForm, ObjectName:=strForm, Save:=acSaveYes
        DoCmd.OpenForm strForm, acDesign
        wLeft = wLeft + wWidth
        Set ctl = CreateControl(strForm, acTextBox, acDetail, , , wLeft, wTop, wWidth, wHeight)
        ctl.Name = "txt" & "82002"
        Set ctl = Nothing
        DoCmd.Close ObjectType:=acForm, ObjectName:=strForm, Save:=acSaveYes
    I end up with THREE TextBoxes - txt82001, txt82002 and Textnn (next number allocated by designer

    If I then Delete them
    Code:
      Set frm = Forms(strForm)For Each ctl In frm.Controls
    MsgBox ctl.Name
            If ctl.ControlType = acTextBox Then
            MsgBox ctl.Name & " Deleted"
                DeleteControl frm.Name, ctl.Name
                
             End If
    Next ctl
    Set ctl = Nothing
    Set frm = Nothing
    It only deletes one or two of the three - never all three?????

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    So is it always the last control that is not deleted?
    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
    TOPSie is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    101
    It seems to be the last control. However just noticed something else odd. If you perform the first code snippet (create two TextBoxes) and then pause the code at look at the Form then there are just the expected two TextBoxes.
    If you continue to the Delete Loop and then look again the the mystery Textnn control has now appeared and is in the exact same position as the second (proper) TextBox. After the Delete loop one proper TextBox has gone but the other two are still there

  4. #4
    TOPSie is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    101
    I think I now understand where the mystery TextBox is coming from and it simplifies the issue. The real issue is why is the
    Code:
     Set frm = Forms(strForm)
    For Each ctl In frm.Controls
    MsgBox ctl.Name
            If ctl.ControlType = acTextBox Then
            MsgBox ctl.Name & " Deleted"
                DeleteControl frm.Name, ctl.Name
                
             End If
    Next ctl
    Set ctl = Nothing
    Set frm = Nothing
    loop only finding one TextBox when there two??

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    Well I was just thinking that the form has to have at least one control on it, to be saved?
    I also cannot see where your third textbox comes from?
    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

  6. #6
    TOPSie is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    101
    The third text box appears because I create it - then the ctl.Name line fails as the name already exists - so a default name is used :-)

  7. #7
    TOPSie is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    101
    OK - so I added a Label so that the Form is not empty.
    Then I created SIX TextBoxes. The Delete loop "sees" the Label OK but only sees alternate TextBoxes - it "sees" (and Deletes) 1 3 and 5 but doesn't "see" 2 4 & 6

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    This seems to confirm my suspicions?

    The deletion is moving the control index ?

    https://bytes.com/topic/access/answe...rary-behaviour
    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

  9. #9
    TOPSie is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    101
    Bang On - THANK YOU - off to set up a looping loop until all gone :-)

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,861
    I would probably stick all the names into an array, then process the array, deleting the controls?
    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

  11. #11
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    Does the lifetime limit on form controls still apply?

    https://support.microsoft.com/en-us/...8-98c1025bb47c
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  12. #12
    TOPSie is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2021
    Posts
    101
    Early days in this mini project. I hope to use values from a table - which define the text displayed in the TextBox (also the Name of the TextBox) and other values from the records in the table to define Back and Fore Color etc

  13. #13
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    Number of controls and sections that you can add over the lifetime of the form or report 754
    I'd be more inclined to add the max number you need and move\hide them as needed
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  14. #14
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Might help to see the rest of that code and what calls it, otherwise we have to assume variables are correctly typed and that things happen in the correct order. A form does not have to have any controls on it to be saved (easily proved by creating a new form, saving and closing it). I take it that this code is running on the same form and not from some other object because you Set frm then open said form. That might mean you already have a command button to call this code so perhaps that's what Welshgasman meant. Would that not be where you get your 3rd button?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  15. #15
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    The reference in post#8 deals with ALL the controls on a form. Code below will only delete all textboxes. It counts all textboxes on the form first, then deletes until count = 0.
    The code to add textboxes adds only two as expected, no extra "ghost".
    The code cannot be behind the form containing the textboxes. I put it in a module. The subs require a parameter specifying the target form name (string).

    Code:
    Option Compare Database
    Option Explicit
        Dim wFirstLeft As Long
        Dim wFirsttop As Long
        Dim wLeft As Long
        Dim wTop As Long
        Dim wWidth As Long
        Dim wHeight As Long
        Const tInch = 1440
    Public Sub Create(strForm As String)
        Dim frm As Form, ctl As Control
        DoCmd.OpenForm strForm, acDesign
        Set frm = Forms(strForm)
        'wLeft = wFirstLeft
        'wTop = wFirsttop
        wLeft = tInch
        wTop = tInch
        wWidth = 3 * tInch
        wHeight = 0.4 * tInch
        DoCmd.OpenForm strForm, acDesign
        Set ctl = CreateControl(strForm, acTextBox, acDetail, , , wLeft, wTop, wWidth, wHeight)
        ctl.Name = "txt" & "82001"
        Set ctl = Nothing
        wLeft = wLeft + wWidth + tInch
        Set ctl = CreateControl(strForm, acTextBox, acDetail, , , wLeft, wTop, wWidth, wHeight)
        ctl.Name = "txt" & "82002"
        Set ctl = Nothing
        DoCmd.Close ObjectType:=acForm, ObjectName:=strForm, Save:=acSaveYes
    End Sub
    
    
    Public Sub Delete(strForm As String)
        Dim frm As Form, ctl As Control
        Dim tbCount As Long
        DoCmd.OpenForm strForm, acDesign
        Set frm = Forms(strForm)
        For Each ctl In frm.Controls
            If ctl.ControlType = acTextBox Then tbCount = tbCount + 1
        Next ctl
        Do While tbCount > 0
            For Each ctl In frm.Controls
                If ctl.ControlType = acTextBox Then
                    DeleteControl frm.Name, ctl.Name
                    tbCount = tbCount - 1
                End If
            Next ctl
        Loop
        DoCmd.Close ObjectType:=acForm, ObjectName:=strForm, Save:=acSaveYes
        Set ctl = Nothing
        Set frm = Nothing
    End Sub

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 03-30-2018, 02:31 PM
  2. Deleting Form and Controls in VBA
    By eagerlearner in forum Programming
    Replies: 4
    Last Post: 11-16-2014, 02:59 PM
  3. Replies: 1
    Last Post: 01-31-2013, 02:58 PM
  4. Creating a Form with 2 combo boxes and a text box
    By smorris4804 in forum Access
    Replies: 1
    Last Post: 06-17-2011, 02:06 PM
  5. Creating Report using Text boxes & db fields
    By Nancy J. in forum Reports
    Replies: 13
    Last Post: 07-20-2009, 07:09 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