Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call function from another form

Status
Not open for further replies.

bobolito

IS-IT--Management
Nov 6, 2003
60
0
0
US
I've researched how to call a function stored in another form in Access, but so far none of the suggestions I've read work for me. I'm not sure what I'm doing wrong.

I've created a form1 where the user selects checkboxes and then they can click on a print button. That print button opens a form2 I created which contains a combo box for the list of printers and another print button.

When the user clicks on the print button in Form2 what should happen is that the printer name selected by the user is passed back to the first form and then the first form will execute a function containing the code to print the report using the selected printer.

One of the suggestions I read was to put the function in a module, but I can't do that because the function is tied to many controls and variables in the form which would not be available in the module. I've already declared the function as public but it is inside the Form1.

In Form2 I have tried Call Form_Form1.FunctionName and also Call Forms!form1.functionName which have been suggested by other people, but I get the "object required" error message at runtime because Access can't find the Function I am referring to.

Any suggestions?
 
Why not have the combob box for printer selection on form1? If realestate is a problem, set the visible property to No,then have the cbo become visible when the Print button is clicked. In the AfterUpdate event of the cbo, start the printing, move focus to another control and make the cbo invisible again.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
I thought about that already, but since I had already created the second form and spent enough time coding everything the way it is now, and since I am like 95% done, all I thought I needed is a simple line to connect that second form to the first. If there's a simple solution I rather do it that way, if not, I'll try your suggestion. Thanks..
 
Have you declared the function as Public so it can be seen outside the form?
Code:
Public Function FunctionName() As String
.
.
.
End Function

Also, I assume you are aware that the first form needs to be open if you want to call any of its subs or functions?

Finally, when calling it from the second form, try this syntax:
Code:
   Forms("Form1").FunctionName

 
Yes, the function is declared as Public and the form1 is also open while the second form is running. In fact, it has to be open because the first form is the one that opens the second form so when the second form wants to call the function in the first form, the first form is already open.

Thanks for the suggestion for the syntax. I will try that and see how it goes.

B.
 
ok....I tried Forms("Form1").FunctionName and it did not work. I still got the same message "Run-time error '424': Object required." and when I click Debug it highlights the line that calls the function in yellow.

Here's the sub that resides in the second form:

Code:
Private Sub cmdPrint_Click()

' Grab the printer object for the selected printer.
Set printerObjName = Application.Printers(Me!cmbPrinter.Value)

Forms("frmSearchForm").printRooms

'Close Form
DoCmd.Close

End Sub

I tried both ways:

Forms("frmSearchForm").printRooms

and

Call Forms("frmSearchForm").printRooms


frmSearchForm is the name of the main form.
printRooms is the name of the function that resides in the main form.

Here's the code for the function in the main form (the one I'm trying to call from the second form):

Code:
Public Function printRooms()
Dim i(13) As Boolean, n As Integer, d(13) As String

Application.Printer = printerObjName

i(0) = chk105
i(1) = chk106
i(2) = chk107
i(3) = chk108
i(4) = chk109
i(5) = chk110
i(6) = chk111
i(7) = chk112
i(8) = chk113
i(9) = chk114
i(10) = chk115
i(11) = chk118
i(12) = chk119
i(13) = chk121

d(0) = "W105"
d(1) = "W106"
d(2) = "W107"
d(3) = "W108"
d(4) = "W109"
d(5) = "W110"
d(6) = "W111"
d(7) = "W112"
d(8) = "W113"
d(9) = "W114"
d(10) = "W115"
d(11) = "W118"
d(12) = "W119"
d(13) = "W121"

For n = 0 To 13 Step 1

    If i(n) Then
    txtRoom = d(n)
    
        If chk121 Then
            DoCmd.OpenReport "rptRoomScheduleITV", acViewNormal
        Else
            DoCmd.OpenReport "rptRoomSchedule", acViewNormal
        End If
        
    txtRoom = ""
    End If

Next n


End Function
 
I was researching about naming conventions and came across something interesting. When declaring a function the initial should be capitalized. I didn't know this would make any difference, but still did not fix my problem.

So now, I am declaring the function as:
Public Function PrintRooms()

instead of
Public Function printRooms()

So I changed the call line in the second form like this:
Call Forms("frmSearchForm").PrintRooms

...so the P is capital and now I get a different error message:

Run-time error '2465':
Application-defined or object-defined error


Haha! I just love those "explicit" error messages.

Now I don't know what to do....

Again, when I click Debug, VB highlights the line that calls the function: Call Forms("frmSearchForm").PrintRooms

I also tried without "Call" and got the same exact error message.
 
Try:

Form_frmSearchForm.PrintRooms

Access is not usually case sensitive.
 
How are ya bobolito . . .

and this:
Code:
[blue]      Forms![purple][b][i]FormName[/i][/b][/purple].[purple][b][i]RoutineName[/i][/b][/purple][/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
Forms!FormName.RoutineName certainly works very well when the form is part of the Forms collection, however Form_frmSearchForm.PrintRooms should work whether or not the form is in the Forms collection.
 
These are the error messages I get:

Form_frmSearchForm.PrintRooms
'424' Object required

Call Form_frmSearchForm.PrintRooms
'424' Object required

Forms!frmSearchForm.PrintRooms
'424' Object required

Call Forms!frmSearchForm.PrintRooms
Compile error: Syntax error

Thanks for all your suggestions, but I don't know why none of them work. Maybe because I'm using Access 2007 and Microsoft changed something they didn't tell us about?
 
Ok I created another function just to test and I figured that Forms!frmSearchForm.Testing works when called from the second form. FINALLY! However, after some testing I discovered there was a piece of code inside the function causing the problem:

Application.Printer = printerObjName

Now I gotta figure out what's up with that.

Thanks for all the help! :)
 
Ok, the problem was I hadn't declared a variable as Global.

I have this module with a variable and I used Dim to declare it instead of Global in the module.

The variable's name is printerObjName and it contains the printer name the user chooses in the second form. That value is then pulled from the main form using the line Application.Printer = printerObjName.

This allows the user to select the printer they want to use before printing a report.

So finally, it's printing ok! Thanks for all your help! :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top