Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

EVAL calling procedure 3 times

Posted on 2003-03-28
7
Medium Priority
?
160 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:TheBlakeE
  • 3
  • 2
  • 2
7 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8227344
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
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 150 total points
ID: 8227346
did you try the callbyname function instead of eval?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8227354
well, Arthur did best.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:TheBlakeE
ID: 8227570
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:

http://www.experts-exchange.com/Databases/MS_Access/Q_10158330.html

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

Thanks,
Blake
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8227886
"....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
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8229389
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
0
 

Author Comment

by:TheBlakeE
ID: 8241382
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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question