Monday, April 14, 2014

What are Layouts and When Should I Use Them: The Synergy of Layouts and Anchors

With Access 2007, Microsoft introduced Layouts. You apply a layout under the Arrange tab, in the Table group. Layouts in Access are an emulation of HTML tables, which may explain why the Access team refers to them as Layouts but put them in a ribbon group called Table. And let's not even discuss the confusion between a Layout and Layout View.

A layout provides a grouping method for controls on a form or report. The two default layout types are Tabular (labels in form header, controls in detail), and Stacked (labels to the left, controls to the right, all in detail). Regardless of which layout you choose, you can edit it afterward to work any way you please.

If you are building an Access Web app to post to a Sharepoint server,  your forms must use a layout, according to Microsoft's Access 2010 Help page for controls and layouts (http://office.microsoft.com/en-us/access-help/introduction-to-controls-HA010352440.aspx#BMunderstandcontrollayouts). This makes sense, since the layouts create HTML tables that Sharepoint server can use in its data-driven model.

When it comes to a desktop database, however, Microsoft gives no compelling reason for using the layouts, noting only that "layouts are optional if you are building a desktop database that will only be opened by using Access" and suggesting that they are useful in creating a uniform look for your form. Microsoft completely fails to note the power of a layout coupled with the Anchor properties in a sizable form.

Anchors are one of the more recent innovations in Access. Until Access 2007, a control's position was set by the Top and Left measurements and remained static throughout the life of the form, unless changed in code. With the introduction of Anchors, the developer could choose how a control moves when a form is resized. Controls are anchored both vertically and horizontally, so a Close button at the bottom-right corner of a form can remain always in the bottom-right corner, while the header stays in its customary position at the top left. Anchors can also make a control grow and shrink with the form, by setting the Anchor properties to Both. With the anchor at Both, the control holds its initial Top and Left settings, but increases or decreases in size as the form is resized, holding the distance between the control and the bottom and/or right sides of the form static.

This is great, but then when the developer sets an Anchor property to Both for two controls on the same level, there's a problem;  if the user stretches the form, the controls overlap each other as each holds to both its constraints. The way to solve this problem? Layouts!

When you put your controls into a layout, they are constrained within the table outlined by the layout. This means that, no matter how many controls you have lined up top-to-bottom and left-to-right, you can select the entire layout and set the Anchor properties to Both without any overlap. As the controls shrink and grow within the layout, the layout shrinks and grows with them, justifying each control to the table. Et Voila! Thanks to your layout, you now have a form that resizes at the user's demand, without any complex coding on your part.