?
Solved

EVAL calling procedure 3 times

Posted on 2003-03-28
7
Medium Priority
?
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

764 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