473,326 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Arranging new control on form as stacked

I have a form which occasionally requires a new checkbox control to be added (when a new field is created in the relevant table). Is there a way to use VBA to arrange all the checkboxes on the form (including the new one) as stacked?
May 23 '14 #1

✓ answered by jimatqsi

Sure. Try this.
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2. Dim ctl2 As Control
  3. Dim row As Long
  4. Dim ctlMargin As Long
  5. ctlMargin = 0 ' supply any margin you want here between controls
  6. row = Me.Check0.Top + Me.Check0.Height + ctlMargin
  7.  
  8. For Each ctl In Me.Controls
  9.     If ctl.ControlType = acCheckBox Then
  10.       If ctl.Name <> "check0" Then ' assume model is already at the right place
  11.           ctl.Top = row
  12.           ctl.Left = Me.Check0.Left
  13.           For Each ctl2 In Me.Controls
  14.             If ctl2.ControlType = acLabel Then
  15.                 If ctl2.Parent.Name = ctl.Name Then ' find label for this checkbox
  16.                     ctl2.Top = row                  ' use same row for label
  17.                     ctl2.Left = ctl.Left + ctl.Width ' move label to right of checkbox
  18.                 End If
  19.             End If
  20.           Next
  21.           row = row + ctl.Height + ctlMargin ' calc row for next control
  22.       End If
  23.     End If
  24. Next
  25.  
  26. End Sub
The only problem might be that there's some uncertainty about the order the controls will be processed in.

Jim

3 2579
jimatqsi
1,271 Expert 1GB
Sure. Try this.
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2. Dim ctl2 As Control
  3. Dim row As Long
  4. Dim ctlMargin As Long
  5. ctlMargin = 0 ' supply any margin you want here between controls
  6. row = Me.Check0.Top + Me.Check0.Height + ctlMargin
  7.  
  8. For Each ctl In Me.Controls
  9.     If ctl.ControlType = acCheckBox Then
  10.       If ctl.Name <> "check0" Then ' assume model is already at the right place
  11.           ctl.Top = row
  12.           ctl.Left = Me.Check0.Left
  13.           For Each ctl2 In Me.Controls
  14.             If ctl2.ControlType = acLabel Then
  15.                 If ctl2.Parent.Name = ctl.Name Then ' find label for this checkbox
  16.                     ctl2.Top = row                  ' use same row for label
  17.                     ctl2.Left = ctl.Left + ctl.Width ' move label to right of checkbox
  18.                 End If
  19.             End If
  20.           Next
  21.           row = row + ctl.Height + ctlMargin ' calc row for next control
  22.       End If
  23.     End If
  24. Next
  25.  
  26. End Sub
The only problem might be that there's some uncertainty about the order the controls will be processed in.

Jim
May 23 '14 #2
jimatqsi
1,271 Expert 1GB
I've assumed the first, model checkbox is called 'check0'. Supply your own, better name for the checkbox that is positioned where you want the top-most checkbox.

Jim
May 23 '14 #3
Jim,

Thanks, a sweet bit of code, does exactly what I needed,
Thanks,
Mark
May 24 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: deko | last post by:
I have a popup "Tools" form with a tab control that has a different subform on each tab. The problem is that new tabs/subforms continue to be added as users request new features - so the code on...
1
by: 2D Rick | last post by:
I have a form that opens maximized and requires scrolling down to see all the textboxes. When you enter a textbox in the lower portion of the form the Enter event opens a large textbox with a large...
11
by: Trevor | last post by:
Hi, I currently have the following code in a subform. If Not rstPDAddresses.EOF And Not rstPDAddresses.BOF Then With rstPDAddresses txtautoAddressID.ControlSource = "='" & Nz(!autoAddressID,...
0
by: Matt Petteys | last post by:
I'm using 1.1 framework and IE 6.0. A form within a user control generates the following doPostBack method. The control ID of the form is not a valid javascript variable name, ie...
2
by: david.boone | last post by:
Hello, I am trying to enable controls based on the value of a checkbox, i.e. if value = true then enable. I have a tab control form with controls on 4 tabs. I have some code (below) on the...
0
by: Filippo Bettinaglio | last post by:
hi, I have developed a window control form in C sharp 2005, I can use the component in other .exe applications (just keeping the two project in the same solution group file) but I cannot use it...
3
by: Kurtulus | last post by:
Hi; I have problem for very long time. I have one mdiparent form and several mdichild form. I have tried to change one control on the mdi parent form form anther one. However I have encountered an...
2
by: TerryStone | last post by:
I have created a control that displays a list of items. During design mode I fill it with junk data using calls from the constructor. So when I look at a form with the control on, instead of...
0
by: GeoffT | last post by:
I have encountered a problem with the data that is displayed in the list boxes that are located on a tab control access form (2003 version). This form uses several combo boxes as filters (After...
2
by: LeLouch Fenette | last post by:
How to access a Control Form to Another form? For example: In Form1 I have a datagrid named " dgview ". In Form2 I want to access the dgview into Form2.. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.