简体   繁体   English

访问 VBA - 触发组合框更改事件

[英]Access VBA - Trigger combobox change event

I have been playing around with form designs and now I have constructed a form, which is almost complete, just one problem left.我一直在玩表单设计,现在我已经构建了一个几乎完整的表单,只剩下一个问题。 Let me explain first:我先解释一下:

Form is bound to a "Join Table", which has only 2 fields - ID from "Table1" and ID from "Table2".表单绑定到一个“加入表”,它只有 2 个字段 - 来自“Table1”的 ID 和来自“Table2”的 ID。 Based on those two fields I have added fields from "Table1" & "Table2", on same form.基于这两个字段,我在同一表单上添加了来自“Table1”和“Table2”的字段。 Then I have added 2 option buttons, 1 Combobox and 2 Subforms.然后我添加了 2 个选项按钮、1 个组合框和 2 个子表单。

This allows me to watch records from two different tables that are joined, from each point of view - "Table1" or "Table2" view.这使我可以从每个角度(“Table1”或“Table2”视图)观察两个连接的不同表中的记录。 I am selecting this views with Option buttons, which changes Combobox rowsource, so you can navigate to records from Combobox.我正在使用选项按钮选择此视图,这会更改 Combobox 行源,因此您可以从 Combobox 导航到记录。

Here is code :这是代码:

Private Sub OptButton0_Click()
        If Me.OptButtonO.Value = True Then

        Me.OptButton1.Value = False

       Me.Cmbox.RowSource = "SELECT [Table1].[Field1], [Table1].[Field2], [Table1].[Field3] FROM Table1 ORDER BY [Field1];"

      Me.Cmbox.SetFocus
      Me.Cmbox = Me.Cmbox.ItemData(0)

        End If

 End Sub

 Private Sub Cmbox_AfterUpdate()   
   If Me.OptButton0.Value = True Then

    If IsNull(Me!Cmbox) Then Exit Sub

    With Me.RecordsetClone
      .FindFirst "[Field1] = " & Me!Cmbox
      If Not .NoMatch Then
         If Me.Dirty Then Me.Dirty = False
         Me.MySubform.Width = 8280
         Me.MySubform.SourceObject = "MySubform"
         Me.Bookmark = .Bookmark
      Else
         Me.MySubform.Width = 8000
         Me.MySubform.SourceObject = ""
      End If

    End With

Me.Cmbox.SetFocus
DoCmd.Requery

End If

End Sub

This posted code is only for one Option button, second one is same, just opposite.此贴出的代码仅用于一个 Option 按钮,第二个相同,正好相反。 Now what is problem ?现在有什么问题?

Problem is that when I navigate through record via Combobox, click second Option button for another view AND THEN RETURN to same view, my subform results stays same as they were when I clicked another Option button, although Combobox listIndex is 0. If I select combobox Listindex from Combobox, code works again.问题是,当我通过 Combobox 浏览记录时,单击另一个视图的第二个 Option 按钮,然后返回到同一视图,我的子表单结果与单击另一个 Option 按钮时的结果保持相同,尽管 Combobox listIndex 为 0。如果我选​​择组合框来自 Combobox 的 Listindex,代码再次工作。

SO BASICALLY - I NEED CODE THAT WILL TRIGGER COMBOBOX CHANGE WHEN OPTION BUTTONS ARE CLICKED.所以基本上 - 我需要代码,当点击选项按钮时会触发组合框更改。 It works when you're clicking in Combobox, but not when clicking in Option button.它在您单击组合框时有效,但在单击选项按钮时无效。

I know It's complicated to understand, please take a look at code, and ask anything.我知道理解起来很复杂,请看一下代码,并提出任何问题。 Any help appreciated.任何帮助表示赞赏。

Another option: I had a similar issue: with an unbound combobox.另一种选择:我有一个类似的问题:使用未绑定的组合框。 In the '*_Change' event in a combobox, if I pick a value from the dropdown, the value is there;在组合框中的“*_Change”事件中,如果我从下拉列表中选择一个值,则该值就在那里; but if I delete the existing value, the prior value still shows up.但如果我删除现有值,先前的值仍会显示。

... ...

If Me.series_filter > "" Then
    lstrMetric = lstrMetric & "and X.series_name = '" & Me.series_filter & "' "
End If

... ...

Me.Filter = Mid(lstrMetric, 5)

... ...

I have a dropdown for a filter on the form: picking a value is meant to set (or clear) the filter.我在表单上有一个过滤器的下拉列表:选择一个值是为了设置(或清除)过滤器。 It worked when I picked a value, but would not clear if I delete it.当我选择一个值时它起作用,但如果我删除它就不清楚了。

I added a line to the start of the code to commit updates:我在代码的开头添加了一行以提交更新:

Me.dirty = false 

The code now recognizes the null value when the combobox is cleared.当组合框被清除时,代码现在识别空值。 This works in my case - of course it would be a problem if you didn't want any updated fields written to the database.这在我的情况下有效 - 当然,如果您不希望将任何更新的字段写入数据库,这将是一个问题。

call the the combobox afterupdate event in the option button click event:在选项按钮单击事件中调用组合框 afterupdate 事件:

private sub optbutton0_click()
    ...
    cmbox_afterupdate()
end sub

PS: Rather than having events for option buttons directly, you should put them in a frame, (even if you then have to make the frame transparent to stop it from appearing) and use the afterupdate or click events of the frame, whereby you can get the selected option button by option value: PS:与其直接为选项按钮设置事件,不如将它们放在一个框架中,(即使您必须使框架透明以阻止它出现)并使用框架的 afterupdate 或单击事件,您可以通过选项值获取选定的选项按钮:

private sub frame0_click()
    select case frame0
        case 0 'option button 0 is selected
            ...
        case 1 
        ...
    end select
end sub

In Access, controls can be funny.在 Access 中,控件可能很有趣。 I mean, when you change the option, you see it change, it changes on the screen... but the control doesn't always change its .value right away... meaning it doesn't Update .我的意思是,当您更改选项时,您会看到它发生变化,它在屏幕上发生变化……但控件并不总是立即更改其.value ……这意味着它不会Update For a simpler illustration of this principle, create a textbox, type some stuff into it, and look at the textbox.value in the immediate window.为了更简单地说明这一原则,创建一个文本框,在其中输入一些内容,然后查看直接窗口中的textbox.value Then, with the cursor still in the textbox, backspace some of it.然后,当光标仍在文本框中时,退格其中一些。 Check the value again.再次检查该值。 Do that a few more times.多做几次。 Start including textbox.text in your tests.开始在测试中包含textbox.text There are multiple ways around this, but it is most important to understand when controls actually update, so you can understand better when and which work arounds to use.有多种方法可以解决此问题,但最重要的是了解控件何时实际更新,以便您可以更好地了解何时使用以及使用哪些变通方法。

Now, in this situation, you typed 'click' every time when you referenced selecting an option.现在,在这种情况下,每次引用选择选项时都键入“单击”。 I think I will hold you to that.我想我会坚持你的。 Personally, I use Tab and the arrow keys sometimes, which would be a slightly more complicated answer, but one you will be better equipped to solve yourself, after understanding the textbox example above.就我个人而言,我有时会使用Tab和箭头键,这会是一个稍微复杂的答案,但是在理解了上面的文本框示例之后,您将有能力更好地解决自己的问题。 But, if you change AfterUpdate above to Click , you should be good.但是,如果您将上面的AfterUpdate更改为Click ,您应该会很好。

That said, there are events besides mouse clicks that can activate the Click event.也就是说,除了鼠标点击之外,还有一些事件可以激活Click事件。 Usually not a problem, but since you are changing the look of your form, specifically the width, you may want to be aware that the subform width may flash back and forth sometimes while using your tool.通常不是问题,但由于您正在更改表单的外观,特别是宽度,您可能需要注意子表单宽度在使用工具时有时可能会来回闪烁。 Not often, probably won't be too much of an annoyance, but I might rethink why I needed to change width at all, or if there might be better triggers for it.不经常,可能不会太烦人,但我可能会重新思考为什么我需要改变宽度,或者是否有更好的触发器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM