Link to home
Start Free TrialLog in
Avatar of AAlpha1
AAlpha1

asked on

Refresh Form - Runtime error 424 Object Required

I use a function to refresh a form. It can be accessed from other modules or from the Form itself.
The form requires the form name to be explicitly defined as shown in the "Code that works"

I would like to have a more general function where I pass the form name to the function as an argument. This is shown in the "Code that is not working". The name of the form is defined in the sub that's calling the function. It generates a runtime error 424 Object required.

Is what I'm trying to do achievable?

Thanks


' Code that works:
Function FormRefresh()
    Dim ctlObject As Form
    Set ctlObject = Form_fExperts      ' EXPLICITLY Define Form to refresh
    ctlObject.Refresh                  ' Requery source of data for Form.
            MsgBox ("Done")
End Function
'
' Code that is not working
Function FormRefreshTest(FormName)
    Dim ctlObject As Form, vFormName   
    vFormName = ("Form_" & FormName)
    Set ctlObject = vFormName             ' Define Form to refresh
    ctlObject.Refresh                            ' Requery source of data for Form.
        MsgBox ("Test Done")  
End Function

Open in new window

Test1-mdb.mdb
SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AAlpha1
AAlpha1

ASKER

Brilliant. It works when called from the form. But, it does not work when called from a module (invalid use of Me key word) . On another form, that form is refreshed but not any other one.
Test1-mdb.mdb
use this

Public Sub FormRefreshTest2()

    Call FormRefreshTest(Forms("fExperts"))

End Sub
Avatar of AAlpha1

ASKER

Capricorn1: My response (07/15/09 02:00 PM, ID: 24864129) was for your proposal. I apologize for not addressing it properly.

the Nelson: I want to refresh. I suppose if I wanted to requery, I would replace the .Refresh with .Requery. I am now testing your proposal.
<I want to refresh. >
Just wondering since you had "requery" in the remark:
    ctlObject.Refresh                  ' Requery source of data for Form.
Avatar of AAlpha1

ASKER

Capricorn1 "Me" method is great provided it's used locally in the form calling the Refresh action.
thenelson (string) approach is more versatile and "idiot proof" for people like me.

Thank you both very much.
Albert
Test1-mdb.mdb
You're welcome.  Glad to help and thank you very much for the points with "A" grade!

Happy Computing!

Nelson