访问列表框AfterUpdate在选中时未触发= True

时间:2017-08-30 19:57:07

标签: vba ms-access listbox access-vba

我有一个带有列表框和组合框的Access表单。

Private Sub Form_Open(Cancel As Integer)
   '... misc code
   me.comboBox.RowSource = "sql statement"
   call comboBox_AfterUpdate
End Sub

Private Sub comboBox_AfterUpdate()
   listbox.Selected(0) = true
End Sub

Private Sub listbox_AFterUpdate()
   '.... important code that updates the form
End Sub

在表单加载/打开时,代码以

运行
Form_Open > comboBox_AfterUpdate > listbox_AfterUpdate 

但是在表单加载后,当组合框被更改并且触发了comboBox_AfterUpdate时,不会触发listbox_AfterUpdate()。

我会将listbox_AfterUpdate()中的代码移动到一个单独的子例程,但这会在表单加载时调用代码运行两次。关于为什么会这样的想法?

此处类似的问题: http://www.tek-tips.com/viewthread.cfm?qid=1395160

1 个答案:

答案 0 :(得分:0)

You could use two custom functions and use optional parameters to know the context in which it is called. That way you have full control over when which function does which.

E.g.

public static int factorial(int n){    
      if (n == 0)    
        return 1;    
      else    
        return(n * factorial(n-1));    
     }

Moving all your code gives you a greater degree of customization, and can help you avoid calling the same code twice.

You could possibly set a global variable in System.out.println(factorial(number of your choice)); //direct example System.out.println(factorial(3)); , then trigger function jsonToForm(json) { // populates form elements with JSON data for (var x in json) { // JSON iteration var id = x; // in my original code x has to be processed to match form and object names var element = document.getElementById(id); if (element) { // if JSON key corresponds to a non radio button (unique id) form element element.value = json[x]; // updates form values } else if (document.getElementsByName(id)) { // for radio buttons the name attribute is used as JSON key won't match separate radio button id's. var radios = document.getElementsByName(id); for (var i = 0; i < radios.length; i++) { // radio button iteration if (radios[i].value == json[x]) { radios[i].checked=true; break; } } } }} by changing the listbox, and then reset that global variable again. Numerous solutions are possible.