onclick method

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?
willmrsAsked:
Who is Participating?
 
andyclapConnect With a Mentor Commented:
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
 
NorthCommented:
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
 
willmrsAuthor Commented:
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
 
wsh2Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.