Solved

onclick method

Posted on 2000-03-06
4
240 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
4 Comments
 
LVL 1

Expert Comment

by:North
Comment Utility
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
Comment Utility
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
Comment Utility
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 50 total points
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now