Using Eval to call a control event

C

Carl Rapson

I'm programmatically setting a combo box value using a DLookUp, and I would
like to simulate the combo box's AfterUpdate event. The problem is, I'm not
working with the control directly -- I'm iterating through the Controls
collection and setting each control, something like:

Dim ctl as Control
For Each ctl In Me.Controls
If (ctl.ControlType = acTextBox) Or (ctl.ControlType = acComboBox)
Then
ctl.Value = DLookup("[Default Name]", "[CSM Defaults]",
"[BA]='" & strBA & "' AND [Office]='" & strOffice & "'")
Eval ctl.Name & "_AfterUpdate()"
End If
End If
Next

The control value gets set OK, but I get an error 2425 on the Eval statement
(the database can't find the function name). I know the control events are
Subs, not Functions; is that the problem? Does Eval only work with
Functions? Is there another way to do what I want, using either Eval or
another method?

Thanks for any information,

Carl Rapson
 
B

Brendan Reynolds

If you change the scope of the event procedures from Private to Public, you
can do it with CallByName ...

Public Sub Combo0_AfterUpdate()
MsgBox "Combo0 AfterUpdate"
End Sub

Public Sub Combo2_AfterUpdate()
MsgBox "Combo2 AfterUpdate"
End Sub

Private Sub Command4_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acComboBox Then
CallByName Me, ctl.Name & "_AfterUpdate", VbMethod
End If
Next ctl
End Sub
 
C

Carl Rapson

Thank very much, Brendan, it works like a charm. That's the first time I've
come across CallByName. It definitely goes in my reference file.

Carl Rapson

Brendan Reynolds said:
If you change the scope of the event procedures from Private to Public,
you can do it with CallByName ...

Public Sub Combo0_AfterUpdate()
MsgBox "Combo0 AfterUpdate"
End Sub

Public Sub Combo2_AfterUpdate()
MsgBox "Combo2 AfterUpdate"
End Sub

Private Sub Command4_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acComboBox Then
CallByName Me, ctl.Name & "_AfterUpdate", VbMethod
End If
Next ctl
End Sub

--
Brendan Reynolds (MVP)

Carl Rapson said:
I'm programmatically setting a combo box value using a DLookUp, and I
would like to simulate the combo box's AfterUpdate event. The problem is,
I'm not working with the control directly -- I'm iterating through the
Controls collection and setting each control, something like:

Dim ctl as Control
For Each ctl In Me.Controls
If (ctl.ControlType = acTextBox) Or (ctl.ControlType = acComboBox)
Then
ctl.Value = DLookup("[Default Name]", "[CSM Defaults]",
"[BA]='" & strBA & "' AND [Office]='" & strOffice & "'")
Eval ctl.Name & "_AfterUpdate()"
End If
End If
Next

The control value gets set OK, but I get an error 2425 on the Eval
statement (the database can't find the function name). I know the control
events are Subs, not Functions; is that the problem? Does Eval only work
with Functions? Is there another way to do what I want, using either Eval
or another method?

Thanks for any information,

Carl Rapson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top