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
Solved

onclick method

Posted on 2000-03-06
4
259 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
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 50 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON Response and request in VB6 application 11 585
Excel Automation VBA 19 89
which modules are active in VB6 project? 6 42
Error with a code discussed on this page 5 32
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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

837 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