vba 完成某些字段后,使字段在 MS Access 表单中可见

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20669839/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:26:40  来源:igfitidea点击:

Make fields visible in MS Access form as certain fields are completed

vbams-accessaccess-vbams-access-2010

提问by user2385809

I am building a form In MS Access for users to input data but there are too many possible fields. Most of the time only about half the fields will be used.

我正在 MS Access 中构建一个表单供用户输入数据,但可能的字段太多。大多数时候只有大约一半的字段会被使用。

I thus would like to have certain fields appear only depending on what the user inputted on a prior given field.

因此,我希望某些字段仅根据用户在先前给定字段中输入的内容出现。

Ex: user enters project number, title, then he checks a "yes/no" engineering. since he checked it this means engineering is impacted so a dozen fields that the user will have to fill out appear.

例如:用户输入项目编号、标题,然后他检查“是/否”工程。由于他检查了它,这意味着工程受到影响,因此出现了用户必须填写的十几个字段。

Is this possible:

这可能吗:

1)without VBA

1)没有VBA

2)with VBA

2) 使用 VBA

采纳答案by Nick.McDermaid

Probably not possible without VBA.

没有 VBA 可能是不可能的。

With VBA for example:

以 VBA 为例:

  1. Ensure your form is in Design view
  2. Right click on your Combo Box, Build Event, Code Builder
  1. 确保您的表单在设计视图中
  2. 右键单击您的组合框、构建事件、代码生成器

This opens the code behind your form. It drops you into the default code for the BeforeUpdateevent. We want the Changeevent instead, so at the top right change the drop down from BeforeUpdateto Change. This will give you a bit of code like this:

这将打开表单背后的代码。它会让您进入BeforeUpdate事件的默认代码。我们想要Change事件,所以在右上角将下拉菜单从 更改BeforeUpdateChange。这会给你一些这样的代码:

Private Sub Field1_Change()

End Sub

Inside here, you want to check the value of the combo box and hide fields as required:

在这里,您要检查组合框的值并根据需要隐藏字段:

Assuming the name of your combo box is Field1 (yours of course will be different), you add some code so it looks like this to hide Field2:

假设您的组合框的名称是 Field1(您的当然会有所不同),您添加一些代码,使其看起来像这样隐藏 Field2:

Private Sub Field1_Change()
  If Field1.Value = "Yes" Then
      Me.Field2.Visible = False
  End If
End Sub

Note you need to know the names of all your fields - this is in the Other tab, Name field in the properties box (F4). You should give all of your fields sensible names so you can understand what is going on in the code.

请注意,您需要知道所有字段的名称 - 这在其他选项卡的属性框 (F4) 中的名称字段中。您应该为所有字段提供合理的名称,以便您了解代码中发生的事情。

For a check box, follow exactly the same procedure, but you probably need to use the Clickevent. Just experiment.

对于复选框,请遵循完全相同的过程,但您可能需要使用该Click事件。只是实验。

Sample check box code:

示例复选框代码:

Private Sub Check5_Click()
  ' Note: vbTrue = -1
  If Me.Check5 = vbTrue Then
    MsgBox ("Ticked")
  Else
MsgBox ("Not Ticked")
  End If
End Sub

回答by Melisa L.

I have a form that will show certain fields after a list box value is selected. I use the AfterUpdate function. It has worked so far. My code is below. ProjectName and ProjectNumber are fields you only want displayed if Engineering is selected. OtherName and OtherNumber are fields you only want to show if it is a "NotEngineering" project.

我有一个表单,该表单将在选择列表框值后显示某些字段。我使用 AfterUpdate 功能。到目前为止,它已经奏效了。我的代码如下。ProjectName 和 ProjectNumber 是您只希望在选择 Engineering 时显示的字段。如果它是“NotEngineering”项目,OtherName 和OtherNumber 是您只想显示的字段。

Insert this code by clicking on the Field that selects the project type, go to the Event tab on the property sheet, and click "After Update" and choose code builder and paste in VBA.

通过单击选择项目类型的字段插入此代码,转到属性表上的事件选项卡,然后单击“更新后”并选择代码生成器并粘贴到 VBA 中。

Private Sub ProjectType_AfterUpdate()
If ProjectType.Value = "Engineering" Then
      Me.ProjectName.Visible = True
      Me.ProjectNumber.Visible = True
 Else
      Me.ProjectName.Visible = False
      Me.ProjectNumber.Visible = False
 End If

If ProjectType.Value = "NotEngineering" Then
      Me.OtherName.Visible = True
      Me.OtherNumber.Visible = True
 Else
      Me.OtherName.Visible = False
      Me.OtherNumber.Visible = False
 End If

End Sub

回答by ShadeBlack

There is a way to do not-quite-this without VBA. I'd definitely recommend VBA though, because you can do a lot more with it.

有一种方法可以在没有 VBA 的情况下做到这一点。不过我肯定会推荐 VBA,因为你可以用它做更多的事情。

Rather than hiding, try disabling the unnecessary fields with conditional formatting by expression.

与其隐藏,不如尝试通过表达式使用条件格式禁用不必要的字段。

right-click on the control you want disabled. go down and click on 'Conditional Formatting' Add a new rule Select 'Expression is'

右键单击要禁用的控件。向下并单击“条件格式”添加新规则选择“表达式是”

example: [fld1]="yes"

示例:[fld1]="是"

hit the disabled box click ok click ok

点击禁用框单击确定单击确定

now the control you've selected will disable if field 1 has "yes" selected.

现在,如果字段 1 选择了“是”,则您选择的控件将禁用。

回答by CandleHyman

I have the same problem and I did the following:

我有同样的问题,我做了以下事情:

Private Sub Field1_Click()
If Field1 = "Yes" Then
Me.Field2.Visible = True
Else: Me.Field2.Visible = False
End If
End Sub

but now I have other problem, when I change record, the field that I choosen to be visible in the last record is now visible on the current record, although I have not choosen any option.

但是现在我有其他问题,当我更改记录时,我选择在最后一条记录中可见的字段现在在当前记录中可见,尽管我没有选择任何选项。

Thank you,

谢谢,