?
Solved

onclick method

Posted on 2000-03-06
4
Medium Priority
?
276 Views
Last Modified: 2013-11-26
In Access you have 2 forms both with command buttons. I can specify the onclick event for CommandButton2 on Form2 from Form1 CommandButton1.

Example

Private Sub CommandButton1_Click()

DoCmd.OpenForm Form2

Form2!CommandButton2.OnClick = "=addcompanies()"

End Sub

addcompanies is a function in a module.

How can i do this in VB?
0
Comment
Question by:willmrs
[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
4 Comments
 
LVL 1

Expert Comment

by:North
ID: 2588463
Use this code in Form1

Private Sub Command1_Click()

  Screen.MousePointer = vbHourglass

  Load Form2

  Screen.MousePointer = vbDefault

  Form2.Show vbModeless, Me

  Call Form2.Command2_Click

End Sub

The only things you have to do is: in form2, where you declare Command2_Click(), change the "Private" keyword to "Public", this says that other forms can call that procedure.
And when showing form2 you must use "vbModeless", if you open it using "vbModal" then the call will not happen until you close down form2.

Hope this helps.  Paul.
0
 

Author Comment

by:willmrs
ID: 2588582
Paul, I probably didn't write my question well. I don't have a click procedure written in Form2 for CommandButton2. In Access, you can write the click event in a module and just specify it like:
Form2!CommandButton2.OnClick = "=addcompanies()"
The reason is that I utilize the same form (Form2) and command buttons for several different procedures. When converting this base from Access to VB, VB does not have the OnClick method. I looked through my MSDN disks and MS site but couldn't find a solution. Any ideas?
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2588630
Form2!CommandButton2.OnClick = "=addcompanies()"

OnClick is an event and you are assigning a function to it?

Anyhow..

Private Sub CommandButton1_Click()

   ' If you want to display Form2
   Form2.Show
   
   ' If you want to execute the Command2_click procedure  
   Form2.Command2_Click()

   ' If you just want to execute the function (without the Command2_Click and/or without the Form2 Display)
   dim varReturn as Variant
   varReturn =  Form2.AddCompanies()"

End Sub

You can do any or all of these (your question really should be more specific).

0
 
LVL 6

Accepted Solution

by:
andyclap earned 150 total points
ID: 2593967
I think what you're asking is can you dynamically assign a function name to an object's event?
You can't easily assign it straight to the event (although possible using subclassing), but you can make a call in the event handler to a named function in a instance of a class module or form using CallByName.
eg
form1:

private msOnclick as string

public property let OnClick(byval sOnclick as string)

  msOnclick=sOnclick

end property

private sub command1_click

if msonclick<>"" then
  CallByName Form2,msOnclick,vbMethod
end if

end sub

form2:

public sub Bong
  msgbox "bong"
end sub

private sub command1_click
  form1.onclick="Bong"
end sub

0

Featured Post

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!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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 Month11 days, 18 hours left to enroll

752 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