DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Click button programmatically in VBA

  1. #1
    Join Date
    Oct 2007
    Posts
    5

    Click button programmatically in VBA

    Hi all of you.
    I want some vba code (in access) to click a button dynamically. I thought something like
    Code:
    Form_FrmCheckMailing.Controls("cmdReplace").Value = True
    or
    Code:
    Form_FrmCheckMailing.Controls("cmdReplace").Click
    would work, but it doesn't. How should I do this? A static solution like
    Code:
    select case strButton
    case "cmdReplace"
        cmdReplace_Click
    case else
        cmdElse_Click
    end select
    won't do since buttons get generated during runtime.
    I'm using MSAccess 2000 on XP SP2.
    Thanks for any help

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Welcome to DevX

    How are you generating them at runtime?

  3. #3
    Join Date
    Oct 2007
    Posts
    5
    Sorry, my mistake. The command buttons in the concerning part of the code are not generated at runtime. It's just a lot of them and being able to click them using their name as parameter to some method would make code much more readable.
    However I don't see how the way the buttons have been generated (at design/run time) would affect they way the click event code should be called from code. Can you explain that?

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Well, the simplest way to do this is to take all of the code out of the click events and put it into a Sub routine. Have the button call the Sub Routine.

    Now, since the code is in a Sub, you can also call from anywhere else in your program that you want without having to concern yourself with button clicking.

  5. #5
    Join Date
    Oct 2007
    Posts
    5
    Indeed that is the easiest way. But that would mean putting some 12000 lines of code into 1 procedure/function and I don't really like that idea. So I was hoping there was an alternative.
    In vb6 this is perfectly possible:
    Code:
    Private Sub Command1_Click()
        Form1.Controls("Command2").Value = True
    End Sub
    
    Private Sub Command2_Click()
        MsgBox "test ok"
    End Sub
    I'm looking for a way to do the same in vba.

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    For an Access form it would be the same as calling it in VB 6. If you are behind the same form that the button is on then its ...
    Code:
    Command15_Click
    But if you are in another form or module then you need to make sure the form is loaded/open and call it like ...
    Code:
    Forms![SomeForm].Command15_Click

  7. #7
    Join Date
    Oct 2007
    Posts
    5
    Yes, but now you've hardcoded the name of the button you're clicking on again.
    In terms of my VB example I'm doing:
    Code:
    Form1.Controls("Command2").Value = True
    while you're doing:
    Code:
    Form1.Command2_click
    The way I do it I can easily replace the string "Command2" by a variable passed to a function. They way you do it, you can't.

    Hardcoding the clickevent is what I'm doing now. I have a function which looks like this (error handling removed for easier reading):
    Code:
    public function clickButton(btn as string) as boolean
      select case btn 
      case "Command1"
        Command1_Click
      case "Command2"
        Command2_Click
      end select
    end function
    Having over 6000 cases I would like to rewrite that function like this:
    Code:
    public function clickButton(btn as string) as boolean
      Me.Controls(btn).value = true
    end function
    This works in VB but apparently not in Access. So I'm loking for the Access way to do this.

  8. #8
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    As string for sure won't work. Try As Control
    Code:
    public function clickButton(btn as Control) as boolean
      Me.Controls(btn).value = true
    end function

  9. #9
    Join Date
    Oct 2007
    Posts
    5
    You can't use a Control as index to the controls collection. Only a string or an index number.
    Passing a control to the function would work like this:
    Code:
    public function clickButton(btn as Control) as boolean
      btn.value = true
    end function
    But that won't do either since the value property on a commandbutton (which Me.Controls("cmdTest") is referring to also) doesn't seem to exist.

Similar Threads

  1. Replies: 8
    Last Post: 05-23-2007, 12:31 PM
  2. Replies: 1
    Last Post: 11-30-2005, 04:00 AM
  3. Simulating button click event
    By Rich Golem in forum .NET
    Replies: 2
    Last Post: 04-14-2002, 10:07 PM
  4. programmatically click a html submit button from VB6
    By James Wegman in forum VB Classic
    Replies: 0
    Last Post: 05-15-2001, 02:49 AM
  5. Connecting to DB2 using VB 6.0
    By Randy in forum VB Classic
    Replies: 5
    Last Post: 03-30-2001, 10:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center