vba 如何在运行时更改 Access 表单上字段的宽度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/942476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How might I change the width of a field on an Access form at runtime?
提问by Andrew Scagnelli
I'm working to devise a custom layout system for my MS-Access forms. So far so good - I've had the idea! But now comes the tricky bit: resizing individual fields such that their width matches in an attractive manner.
我正在为我的 MS-Access 表单设计一个自定义布局系统。到目前为止一切顺利 - 我有这个想法!但现在棘手的一点是:调整单个字段的大小,使其宽度以有吸引力的方式匹配。
Sad to say, I'm a bit stuck here - surely there must be some function, property or method call that will allow me to specify an exact width for a given field... But if so, I've been unable to find it.
可悲的是,我有点卡在这里 - 当然必须有一些函数,属性或方法调用可以让我为给定的字段指定一个确切的宽度......但如果是这样,我一直无法找到它。
Can someone here provide me with the information I so desperately need? Or better yet, suggest an appropriate Google query or reference site that could provide me with an answer to my query?
这里有人可以向我提供我非常需要的信息吗?或者更好的是,建议一个合适的谷歌查询或参考网站,可以为我的查询提供答案?
回答by David-W-Fenton
In code, at runtime, this is what you do:
在代码中,在运行时,这就是你要做的:
Me!MyControl.Width = 1.5 * 1440
我!MyControl.Width = 1.5 * 1440
The 1.5 is 1.5" and the 1440 is something called TWIPs, which are relative measurement units that are sized according to screen resolution and your Windows base font size.
1.5 是 1.5",1440 是所谓的 TWIP,它们是相对测量单位,根据屏幕分辨率和 Windows 基本字体大小调整大小。
Now, where do you do this?
现在,你在哪里做这件事?
If you have different records in the same table that need different layouts, you'd do it in the OnCurrent event. That way, as you navigate from one record to another, the OnCurrent event would check whatever data controls which widths you use and set the widths. If there are only two layouts, an If/Then/Else is best. If there are more then two, then a SELECT CASE is better.
如果同一个表中有不同的记录需要不同的布局,则可以在 OnCurrent 事件中进行。这样,当您从一个记录导航到另一个记录时,OnCurrent 事件将检查任何数据控制您使用的宽度并设置宽度。如果只有两种布局,则最好使用 If/Then/Else。如果有两个以上,那么 SELECT CASE 更好。
Also, if you are sizing and aligning colums, it would be helpful to assign module-level constants, something like this (this is pulled from an app that I first created in 1997 that has 15 different record types and 3 basic layouts with 8 or 9 minor variations):
此外,如果您正在调整和对齐列的大小,那么分配模块级常量会很有帮助,就像这样(这是从我在 1997 年首次创建的应用程序中提取的,该应用程序具有 15 种不同的记录类型和 3 种基本布局,其中 8 个或9个小变化):
Const Height = 0.1583 * 1440
Const row1Top = 1.0208 * 1440 ' top of first row
Const row2Top = row1Top + 0.2083 * 1440
Const row3Top = row2Top + 0.2083 * 1440
Const row4Top = row3Top + 0.2083 * 1440
Const row5Top = row4Top + 0.2083 * 1440
Const row6Top = row5Top + 0.2083 * 1440
Const row7Top = row6Top + 0.2083 * 1440
Const Logic0Top = 1.6833 * 1440
Const logic1Top = Logic0Top + 0.1667 * 1440
Const logic2Top = logic1Top + 0.1667 * 1440
Const logic3Top = logic2Top + 0.1667 * 1440
Const logic4Top = logic3Top + 0.1667 * 1440
Const lblLogic0Top = Logic0Top - 0.0208 * 1440
Const lblLogic1Top = lblLogic0Top + 0.1667 * 1440
Const lblLogic2Top = lblLogic1Top + 0.1667 * 1440
Const lblLogic3Top = lblLogic2Top + 0.1667 * 1440
Const lblLogic4Top = lblLogic3Top + 0.1667 * 1440
Const DateWidth = 0.7833 * 1440
Const lWidth = 2.0167 * 1440
Const rWidth = 2.7771 * 1440
Const lMemoWidth = 4.8771 * 1440
Const commtWidth = (2.745 * 1440)
Const lCommt = 3.775 * 1440
Const CommtHeight = 1.5 * 1440
Const memoHeightOffset = 0.94 '0.8354
Const bksCommtHeight = ((memoHeightOffset + 0.5) * 1440) + 250
Const ConditionWidth = 4.2417 * 1440 - 15
Const memo3Tall = ((memoHeightOffset + 1.4146) * 1440) + 160
Const memo4Tall = ((memoHeightOffset + 1.2063) * 1440) + 160
Const memo5Tall = ((memoHeightOffset + 0.9979) * 1440) + 160
Const memo6Tall = ((memoHeightOffset + 0.7896) * 1440) + 160
Const memoShort = (1.5208 * 1440) + 160 ' Height of memo
Const lblOffset = -0.3291 ' adjusts memo label top for new layout
Const lblMemoTop3 = ((lblOffset + 1.9167) * 1440) - 480 - 100
Const lblMemoTop4 = ((lblOffset + 2.125) * 1440) - 480 - 55
Const lblMemoTop5 = ((lblOffset + 2.3333) * 1440) - 480 - 100
Const lblMemoTop6 = ((lblOffset + 2.5417) * 1440) - 480 - 100
Const lblMemoTop7 = ((lblOffset + 2.75) * 1440) - 480 - 100
Const lblBib0 = (2.5833 * 1440) - 480
Const Bib1 = 2.4674 * 1440
Const Bib2 = 2.6257 * 1440
Const Bib3 = 2.784 * 1440
Const Bib4 = 2.9424 * 1440
Very complicated because the form is very complicated (the OnCurrent event got so big that I had to split it into two different subroutines -- I can't remember the exact maximum size of a subroutine, but it might be 64K. Yes, that's how bad it was, and yes, I regret implenting things in that fashion. If I had it to do over, I'd use a tab control with invisible tabs and allocate one tab per record type. But I didn't think of that way back then.
非常复杂,因为表单非常复杂(OnCurrent 事件变得如此之大,以至于我不得不将其拆分为两个不同的子例程——我不记得子例程的确切最大大小,但可能是 64K。是的,就是这样很糟糕,是的,我很遗憾以这种方式实现事物。如果让我重来,我会使用带有不可见选项卡的选项卡控件,并为每种记录类型分配一个选项卡。但我没有这么想那时候。
If you don't need to move or resize things vertically you might be able to get by with a handful of constants:
如果您不需要垂直移动或调整事物的大小,您可能可以使用一些常量:
Col1Left
Col2LeftA
Col2LeftB
Col1WidthA
Col1WidthB
Col2WidthA
Col2WidthB
That would allow you to have two columns of controls with two different horizontal positions for the second column and two different widths for each column. I would recommend very transparent names for these constants so that your code is more-or-less self-explanatory.
这将允许您拥有两列控件,第二列有两个不同的水平位置,每列有两个不同的宽度。我会为这些常量推荐非常透明的名称,以便您的代码或多或少是不言自明的。
回答by Tony Toews
All the properties of any given control can be referenced in VBA code by using me.CtlName.PropertyName or Forms!CtlLName.PropertyName. In your case that would be me.CtlName.Width.
通过使用 me.CtlName.PropertyName 或 Forms!CtlLName.PropertyName,可以在 VBA 代码中引用任何给定控件的所有属性。在你的情况下,这将是 me.CtlName.Width。
Note that the sizes of properties are in twips. And there are 1440 twips per inch. If you are using Metric I have no idea how many twips in cm.
请注意,属性的大小以缇为单位。每英寸有 1440 缇。如果您使用公制,我不知道厘米有多少缇。
That said I size my forms to match 1024x768 unless a client requestes otherwise.
也就是说,除非客户另有要求,否则我将表单的大小调整为 1024x768。
回答by onedaywhen
how to change the fields width [question]
如何更改字段宽度 [问题]
Using SQL DDL:
使用 SQL DDL:
ALTER TABLE MyTable ALTER MyField VARCHAR(20);
This will not affect the column's other properties e.g. it would remain NOT NULL if originally created that way and the default value (if any) would be the same.
这不会影响列的其他属性,例如,如果最初以这种方式创建,它将保持 NOT NULL 并且默认值(如果有)将相同。
P.S. I'm answering the OP's question, not the controversial edited.
PS我正在回答OP的问题,而不是有争议的编辑。
回答by Andrew Scagnelli
Why do you want the field widths to change at runtime? Behavior like this falls outside the typical Windows UI conventions, and will probably through your users for a loop when things start changing size after the form has been displayed. Just give the fields a wide margin and leave the sizes alone after the form has been displayed.
为什么要在运行时更改字段宽度?像这样的行为不属于典型的 Windows UI 约定,并且可能会通过您的用户进行循环,当表单显示后开始更改大小时。只需为字段留出较大的边距,并在显示表单后保留大小。
For a problem like this, just think gloves.
对于这样的问题,想想手套。