Call a Function?

Gannet

Registered User.
Local time
Yesterday, 23:55
Joined
Sep 21, 2006
Messages
55
I'm looking to take code I have in the OnClick event of a button and make it into a Function that can be used by several different subforms.

I have a button on a subform that currently calls a function.
Code:
private sub test_button_Click()

   Call example(test_button)

end sub



public function example(control_name as control)

   control_name.caption = "whatever"

   dim ctl as control
   
   for each ctl In Me.Controls
        msgbox ctl.Name
   next


end function

The code above is an over simplified version of what I'm trying to do. When I run the Call from the click event I was getting an error everytime it saw "Me." anything. When I took them all out I now get an error "Object Required". What do I need to do to allow me to pass parameters to this function that relate to the subform the button is located on? Is a function what I should be creating?
 
Me can only be used in a forms class module. You need to reference the form and then the control. [Forms]![Name of Form]![Name of Control]
 
Me can only be used in a forms class module. You need to reference the form and then the control. [Forms]![Name of Form]![Name of Control]

Or, in the case of variables (although you can use it with explicit names too) you would use:

Forms(strFormName).Controls(strYourNameOfControl)
 
problem with subform

I tried making the changes and now I have an error that the subform cannot be found.
 

Attachments

  • error.jpg
    error.jpg
    11.9 KB · Views: 130
I tried making the changes and now I have an error that the subform cannot be found.

Two possibilities for this.

1. Your Subform Container is not named the same as your subform (check that out)

2. Your code is not in the correct place. You put it in the event PROPERTY in the Properties Dialog Box and not in the VBA Code Window.

If #1 is true then you can just fix it by selecting the subform container and use the correct name (see here on how to do that):
(check the very first screenshot for this, ignoring the others)
http://www.btabdevelopment.com/main/LinkClick.aspx?link=68&tabid=55&mid=385


If #2 is true then see here:
http://www.btabdevelopment.com/main/LinkClick.aspx?link=72&tabid=55&mid=385
 
Has got to be the way I reference

Here's an excerpt from my code. The first part is the beginning of the function. The second part is the call on the button. I've also attached a screenshot of the DB.

Code:
Public Function edit_record_function(strFormName As String, strNameOfLockControl As String, table_from As String, edit_button As control)
            
    If Forms(strFormName).Controls(strNameOfLockControl).Locked = True Then
     ....
    End If
-----------------------------------------------------------------------

Private Sub edit_baseline_rec_Click()
On Error GoTo Err_edit_baseline_rec_Click
    
    
 Call edit_record_function("sbfrmBaseline", "Category", "tblBaseline", edit_baseline_rec)
 

Attachments

  • subform.JPG
    subform.JPG
    97.2 KB · Views: 135
I think it's that this isn't referencing correctly:
Code:
If Forms(strFormName).Controls(strNameOfLockControl).Locked = True Then

SHOULD BE:
Code:
If Forms(YourMainFormNameHere).Controls(strFormName).Form.Controls(strNameOfLockControl).Locked = True Then
 
Eureka!!

That did it ! Thanks you Bob and Keith for all your help!
 
Happy to help. In fact, I'm putting together a mini-tutorial on an easy way to remember how to reference using that exact type of syntax. Once you have a couple of details memorized, it is really easy to use.
 

Users who are viewing this thread

Back
Top Bottom