Link to home
Start Free TrialLog in
Avatar of TheBlakeE
TheBlakeE

asked on

EVAL calling procedure 3 times

Hi.  This is my first question, so forgive me if I don't do it right.

I'm alternating between Access 2000 and Access 97, and have the same problem in both versions.  I have a form with several checkboxes (check1, check2, and check3 for testing), and I want to call a corresponding procedure programmatically if the checkbox is checked.  Through research, I discovered that I should use the "Eval" function.  Here is the code I put together:

Private Sub Command0_Click()
Dim ctlName As Control
    Debug.Print "***Start***"
    For Each ctlName In Me.Controls
        If TypeOf ctlName Is CheckBox Then
            If ctlName.Value = -1 Then
                Eval ("Forms!Form1." & Mid(ctlName.Name, 4))
            End If
        End If
    Next ctlName
    Debug.Print "***End***"
End Sub

Public Sub ck1()
Debug.Print "Check1"
End Sub

Public Sub ck2()
Debug.Print "Check2"
End Sub

Public Sub ck3()
Debug.Print "Check3"
End Sub


The only problem with this code is that it runs each procedure 3 times.  Here are my results when boxes 1 and 3 are checked:

***Start***
Check1
Check1
Check1
Check3
Check3
Check3
***End***

When stepping through the program, it does not return to the Command0_Click procedure before repeating the ck# procedure.  When it is done with ck1, it goes directly from "End Sub" to "Public Sub ck1()" again.

I would ask "can anyone help?", but I KNOW that someone at Experts-Exchange can. :)

Thanks,
Blake
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

where did you get the idea that you should be using the Eval function?

since you KNOW that names of the procedures that are supposed to be executed for each of the CheckBoxes:

            If ctlName.Value = -1 Then
               Select Case ctlName.Name
                  Case "Check1"
                   call chk1
                  case "Check2"
                    call chk2
                  Case "Check3"
                    call chk3
                  Case Else
                    MsgBox "Some INVALID check Box was clicked", VBokOnly
               End Select
           End If

not very pretty but that is the 'cleanest' way to accomplish what you are trying to do.

Aw
ASKER CERTIFIED SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well, Arthur did best.
Avatar of TheBlakeE
TheBlakeE

ASKER

Arthur, that's basically the way I was doing it originally, but in the end I'm going to have several hundred checkboxes on my form, so I got tired of having to add another little piece of code for each box.

I searched and searched.  I can't even remember all the commands I tried.  I remember "Run", and "Call", and "RunCommand", "CallbyName", and... something related to "Execute".  Some of them gave me errors, but most just didn't do anything.  If you could give me code for "CallbyName", I'd be happy to try it again.

Eventually, I stumbled across "Eval" when looking for something else.  Here's the thread:

https://www.experts-exchange.com/questions/10158330/Calling-public-procedure-from-form.html

And it actually worked, so I was thrilled... but I later discovered that it's repeating each procedure 3 times.

Thanks,
Blake
"....but in the end I'm going to have several hundred checkboxes ...."
Then, create an array of checkboxes control and

Dim i As Integer
For i = 0 To Check1.Count - 1
    If Check1(i).Value = 1 Then
        MsgBox Check1(i).Caption' or run your sub
    End If
Next i
What kind of application requires several hundred checkboxes?  As A user, I would find that extremely annoying, as well as being VERY confusing.  But then that is just me, I'm kinda funny that way.

AW
It checks for updates on hundreds of different websites.  You can check a whole category or can check just certain individual sites, depending on what needs updating.

Richie, I went ahead and tried CallbyName again:

CallByName Me, "Form1!" & Mid(ctlName.Name, 4), VbMethod

I also tried

CallByName Me, "Forms!Form1." & Mid(ctlName.Name, 4), VbMethod

but both gave me "Application-defined or object-defined error"

---Blake

P.S.  but then I kept persisting with CallByName, and eventually I did it without reference to the Form, as in:

CallByName Me, Mid(ctlName.Name, 4), VbMethod

And it WORKED!  Yay!

Thanks, Richie. :)

---Blake