在 Access 中打开表单时启用控件

发布于 2024-07-08 17:07:00 字数 567 浏览 19 评论 0原文

我有一个表单,我想根据 ComboBox 控件中的值启用/禁用其控件。 与窗体中的所有其他控件一样,此 ComboBox 控件链接到一个表。 在组合框的 Change 事件中,我放置了启用/禁用其他控件的代码。

我遇到的问题是,当我打开表单时,控件未启用/禁用。 我必须重新选择 ComboBox 值才能启用或禁用所有其他控件。

我注意到的一件事是,ComboBox 内的 RecordSet 控件通常不会更改为 ComboBox value 属性中显示的值。

我尝试使用
combobox.recordset.filter = "Key = " & 组合框.值
但我收到错误
这种类型的对象不支持操作。


更新

我认为我的问题必须更多地解决我如何访问combobox.recordset中的值。 我的印象是,combobox.recordset 保存着从表中收到的值。 但是,它似乎保存着记录源的第一个记录。

我猜测我需要使用另一个记录集对象来搜索我需要的那些值。

I have a form whose controls I want to enable/disable depending on the values in a ComboBox control. This ComboBox control is linked, like all the other controls in the form, to a table. Inside the ComboBox's Change event, I placed the code that enables/disables the other controls.

The problem I have is that when I open the form, the controls are not enabled/disabled. I have to re-choose the ComboBox value to make all other controls enable or disable.

One thing I noticed is that the RecordSet control inside the ComboBox often does not change to the value shown in the value property of the ComboBox.

I tried using
combobox.recordset.filter = "Key = " & combobox.value
but I get the error
Operation is not supported for this type of object.


Update

I think my problem has to do more in how I'm accessing the values in the combobox.recordset. I was under the impression that combobox.recordset held the value received from the table. But, it seems to hold the first record from the recordsource.

I'm guessing that I will need to search those values I need by using another recordset object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

留一抹残留的笑 2024-07-15 17:07:00

大多数访问控制事件不是由对控件的编程更改触发的。 您可能希望调用代码以从表单的加载事件启用控件。

您没有提及您正在使用的 Access 版本,但我不相信任何版本都有组合框的 Recordset 属性。

您是否希望将组合框设置为特定值?

Most Access control events are not triggered by programmatic changes to the control. You may wish to call the code to enable the control(s) from the load event of the form.

You do not mention the version of Access that you are using, but I do not believe that any version has a Recordset property for comboboxes.

Did you wish to set the combobox to a specific value?

壹場煙雨 2024-07-15 17:07:00

执行您在此处尝试的操作的一种方法是将combobox.change() 放入form.current() 方法中。

一旦表单启动并运行,这将就像组合框已被更改一样。

我以前做过类似的事情,但目前我面前没有代码。 一旦我看到它,我就会把它更详细地发布在这里,但我突然相信这就是我所做的。

One way to do what you are trying here is to put the combobox.change() inside the form.current() method.

This will then act as if the combobox had been changed as soon as the form is up and running.

I have done something similar to this before but I dont have the code in front of me at the moment. As soon as I get a look at it I'll post it here in more detail, but off the top of my head I believe this was the way I did it.

旧情别恋 2024-07-15 17:07:00

在评论中,lamcro针对梳状盒盒是否具有记录集的问题进行了评论:

当我闯入表单 VB 代码时
和“添加监视”CB 控件,
记录集属性就在那里。 我可以
甚至进入并看到它自己的
特性。

当我设置监视列表时,我会看到它,但组合框的记录集无法通过代码访问或更改。 为了过滤组合框,您需要使用其行源。

这可以通过以下两种方式之一完成:

  1. 使用事件将新的 Rowsource 动态分配给组合框,或者
  2. 使用对要在其他组合的 Rowsource 的 WHERE 子句中筛选其值的控件的引用盒子。

假设您有 cmbComboBox1,当您在其中选择一个值时,您希望根据 cmbComboBox1 中选择的值过滤 cmbCombBox2 中列出的值。 对于方法 1,您可以使用第一个组合框的 AfterUpdate 来设置第二个组合框的行源:

  Private Sub cmbComboBox1_AfterUpdate()
    Dim strRowsource As String

    strRowsource  = "SELECT * FROM MyTable"
    If Not IsNull(Me!cmbComboBox1) Then
       strRowsource  = strRowsource & " WHERE MyField = " & Me!cmbComboBox1
    End If
    Me!cmbComboBox2.Rowsource = strRowsource
  End Sub

要使用第二种方法,您可以根据测试第一个组合框的值来定义第二个组合框的行源:

SELECT * FROM MyTable 
WHERE (MyField=[Forms]![MyForm]![cmbComboBox1] 
        AND IsNull([Forms]![MyForm]![cmbComboBox1])=False) 
   OR IsNull([Forms]![MyForm]![cmbComboBox1])=True

其作用是,如果第一个组合框有值,则过滤行源;如果有值,则过滤它。 也就是说,您将获得一个未过滤的列表,直到为第一个组合框选择了一个值。

然后,在 cmbComboBox1 的 Afterupdate 事件中,您将重新查询第二个组合框:

  Private Sub cmbComboBox1_AfterUpdate()
    Me!cmbComboBox2.Requery
  End Sub

定义一个参数可能也是一个好主意,以确保对表单控件的引用得到适当解析,因此您的 Rowsource 将是这样的:

PARAMETERS [Forms]![MyForm]![cmbComboBox1] Long;
SELECT * FROM MyTable 
WHERE (MyField=[Forms]![MyForm]![cmbComboBox1] 
        AND IsNull([Forms]![MyForm]![cmbComboBox1])=False) 
   OR IsNull([Forms]![MyForm]![cmbComboBox1])=True

(假设您正在过滤自动编号 PK - 如果数据类型不同,您当然会使用不同的数据类型)

我倾向于使用动态 Rowsource 分配,只是因为我发现它是跨不同 Access 版本的问题较少(即,在所有 Access 版本中,对表单控件的引用的解析方式不同)。

In a comment, lamcro observes in regard to the question of whether or not a comb box box has a Recordset:

When I break into the forms VB code
and "Add Watch" the CB control, the
recordset property is in there. I can
even enter and see it's own
properties.

I see it when I set a watch list, but the recordset of a combo box is not accessible or alterable via code. In order to filter a combo box, you need to work with its Rowsource.

This can be accomplished one of two ways:

  1. use an event to assign a new Rowsource to your combo boxes on the fly, OR
  2. use a reference to the control whose value you want to filter on in the WHERE clause of the Rowsource of your other combo boxes.

Say you have cmbComboBox1 and when you select a value in it, you want the values listed in cmbCombBox2 to be filtered according to the value selected in cmbComboBox1. For method 1, you'd use the first combo box's AfterUpdate to set the rowsource of the second:

  Private Sub cmbComboBox1_AfterUpdate()
    Dim strRowsource As String

    strRowsource  = "SELECT * FROM MyTable"
    If Not IsNull(Me!cmbComboBox1) Then
       strRowsource  = strRowsource & " WHERE MyField = " & Me!cmbComboBox1
    End If
    Me!cmbComboBox2.Rowsource = strRowsource
  End Sub

To use the second method, you'd instead define the Rowsource of the second combo box to be based on testing the value of the first:

SELECT * FROM MyTable 
WHERE (MyField=[Forms]![MyForm]![cmbComboBox1] 
        AND IsNull([Forms]![MyForm]![cmbComboBox1])=False) 
   OR IsNull([Forms]![MyForm]![cmbComboBox1])=True

What this does is filter the rowsource if the first combo box has a value and doesn't filter it if there is a value. That is, you get an unfiltered list until there's a value chosen for the first combo box.

Then, in the Afterupdate event of cmbComboBox1, you'd requery the second combo box:

  Private Sub cmbComboBox1_AfterUpdate()
    Me!cmbComboBox2.Requery
  End Sub

It's also probably a good idea to define a parameter in order to insure that the reference to the form control gets appropriately resolved, so your Rowsource would be this:

PARAMETERS [Forms]![MyForm]![cmbComboBox1] Long;
SELECT * FROM MyTable 
WHERE (MyField=[Forms]![MyForm]![cmbComboBox1] 
        AND IsNull([Forms]![MyForm]![cmbComboBox1])=False) 
   OR IsNull([Forms]![MyForm]![cmbComboBox1])=True

(assuming you're filtering on an Autonumber PK -- if the data type is different, you'd use a different data type, of course)

I would tend to use the dynamic Rowsource assignment, simply because I've found it to be less problematic across different Access versions (i.e., references to controls on forms are not resolved the same way in all versions of Access).

找回味觉 2024-07-15 17:07:00

您能解释一下您拥有什么类型的组合框以及您想要做什么吗? 例如,您是否有一个用于查找表单记录的未绑定组合框,或者是否有一个用于更新表中字段的绑定组合框? 这是由 RowSource 和 ControlSource 属性控制的,组合框没有记录集属性。 当您打开表单时,未绑定的组合框不会有值,当您从一个记录移动到另一个记录时,该值也不会更改,但使用当前事件分配值很容易。

编辑
我仍然不清楚你想做什么。 根据您的评论,您在选择选项时修改控件时是否遇到问题,或者在表单打开时设置组合值时是否遇到问题? 您是否想更改组合的 RowSource,也许如您原始帖子所暗示的那样?

Can you explain what type of combobox you have and what you want to do, please? For example, have you an unbound combobox that is used to find records for your form, or have you a bound combobox that is used to update fields in a table? This is controlled by the RowSource and ControlSource properties, Comboboxes do not have a recordset property. An unbound combobox will not have a value when you open the form, nor will the value change when you move from record to record, but it is easy enough to assign a value using the current event.

EDIT
I am still not clear on what you want to do. Are you having problems modifying the controls when an option is chosen, as per your comment, or are you having problems setting the value of the combo when the form opens? Do you want to change the RowSource of the combo, perhaps, as implied by your original post?

羞稚 2024-07-15 17:07:00

'检查表单属性上的键预览

'在此处调试combobox.value(Ctrl+G 用于调试窗口)

Debug.printcombobox.value

me.filter = "Key = " & combobox.value

'潜在的 me.reload 或 me.refresh 取决于 ms access 版本

'Check your Key Preview on the forms properties

'Debug here for combobox.value (Ctrl+G for debug window )

Debug.print combobox.value

me.filter = "Key = " & combobox.value

'Potential me.reload or me.refresh depending on ms access version

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文