Solved Move a control to other section with vba (1 Viewer)

Babycat

Member
Local time
Today, 22:28
Joined
Mar 31, 2020
Messages
275
Dear all

Can anyone help to move control Txt_ID from Header to Footer section with VBA code of button Btn_MoveCtrl?
I tried with Move method but it is only moving within a section
Code:
Private Sub Btn_MoveCtrl_Click()
Txt_ID.Move
End Sub

1686846589478.png


Thank you
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:28
Joined
Feb 28, 2001
Messages
27,191
I tried with Move method but it is only moving within a section


IF you read the description of the .Move method for text boxes, it allows you to provide coordinates for your destination. HOWEVER, normally your move's coordinates are section-relative, which means you cannot move outside the boundaries of the section where the box resides. Which is exactly what you discovered.

Since the button would have focus at that time (rather than the text box), you COULD place copies of the text box in both places but only enable (and make visible) one of them at a time. (Doing this to the button itself would cause you to try to disable a control that has focus at the time, which you cannot do.) If you wanted to preserve contents, you would have to do that in the button-click code as well. Or bind the text box to the same field.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 16:28
Joined
Feb 19, 2013
Messages
16,618
only way is to put the form in design view, delete the header control and recreate in the footer. Not something I would recommend for an app being used.

You can mimic the requirement by only havng one section (e.g. the detail section) and placing your control there. If the form is continuous, use a subform to display the continuous data.

Have to wonder why you would want to do this
 

Babycat

Member
Local time
Today, 22:28
Joined
Mar 31, 2020
Messages
275
only way is to put the form in design view, delete the header control and recreate in the footer. Not something I would recommend for an app being used.

You can mimic the requirement by only havng one section (e.g. the detail section) and placing your control there. If the form is continuous, use a subform to display the continuous data.

Have to wonder why you would want to do this
I have a standalone form with buttons on footer section. I would like to use it as subform too. So when it plays role of subform, these buttons will be move up to its header. it becauses the mainform already has some buttons at bottom of screen while I dont want many button at screen bottom.
I might go with DocMan's sugesstion. Dupplicate bottons and show/hide them accordingly.
 

GPGeorge

Grover Park George
Local time
Today, 08:28
Joined
Nov 25, 2004
Messages
1,875
I have a standalone form with buttons on footer section. I would like to use it as subform too. So when it plays role of subform, these buttons will be move up to its header. it becauses the mainform already has some buttons at bottom of screen while I dont want many button at screen bottom.
I might go with DocMan's sugesstion. Dupplicate bottons and show/hide them accordingly.
Out of curiousity, what's the use case for altering forms on the fly?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:28
Joined
Feb 28, 2001
Messages
27,191
I might go with DocMan's sugesstion. Dupplicate bottons and show/hide them accordingly.

Be VERY careful. Moving the textbox from a button would work just fine because button will have focus. HOWEVER, you just said "duplicate buttons" and THERE, you have a pitfall. The button that triggers the "swap" between duplicate but differently placed text boxes will work just fine. But you have a problem if you wanted to move the buttons, too - because you cannot disable something that currently has focus. The button cannot move and disable itself. It can only move other things. That which currently has focus is stuck where (and how) it is. You COULD move the button's location within the section - but you cannot disable it from its own _Click routine.
 

Babycat

Member
Local time
Today, 22:28
Joined
Mar 31, 2020
Messages
275
Be VERY careful. Moving the textbox from a button would work just fine because button will have focus. HOWEVER, you just said "duplicate buttons" and THERE, you have a pitfall. The button that triggers the "swap" between duplicate but differently placed text boxes will work just fine. But you have a problem if you wanted to move the buttons, too - because you cannot disable something that currently has focus. The button cannot move and disable itself. It can only move other things. That which currently has focus is stuck where (and how) it is. You COULD move the button's location within the section - but you cannot disable it from its own _Click routine.
Sorry, my typo, I was meaning "duplicate textbox". I have just done my code with your idea, it works as expected.
 

sxschech

Registered User.
Local time
Today, 08:28
Joined
Mar 2, 2010
Messages
793
Not sure if already doing this...you could insert code to test if form is opened stand alone and have vba adjust settings based on that condition, then wouldn't need to click the button to move the buttons between different parts of form as could happen automatically by your code.


There were a few variations in the thread and this is the one I had used.
If Not CurrentProject.AllForms("someFormName").IsLoaded Then
 

Users who are viewing this thread

Top Bottom