Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • Last Modified:

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
0
AAlpha1
Asked:
AAlpha1
  • 3
  • 3
  • 2
2 Solutions
 
Rey Obrero (Capricorn1)Commented:
test this

Private Sub RefreshTest_Click()

    Call FormRefreshTest(Me)
   
End Sub


' -------------------------------------------------------------------
' Refresh Form Test
'
' -------------------------------------------------------------------
Function FormRefreshTest(FormName As Form)
   
    Dim ctlObject As Form
   
    Set ctlObject = FormName            ' Define Form to refresh
    ctlObject.Refresh                    ' Requery source of data for Form.

        MsgBox ("Test Done")
   
End Function
0
 
thenelsonCommented:
Cap's answer or pass the form name as a string:

Function FormRefreshTest(FormName As String)
    Forms(FormName).Refresh                          
        MsgBox ("Test Done")  
End Function

Do you want to refresh or requery the form?
0
 
AAlpha1Author Commented:
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
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
Rey Obrero (Capricorn1)Commented:
use this

Public Sub FormRefreshTest2()

    Call FormRefreshTest(Forms("fExperts"))

End Sub
0
 
AAlpha1Author Commented:
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.
0
 
thenelsonCommented:
<I want to refresh. >
Just wondering since you had "requery" in the remark:
    ctlObject.Refresh                  ' Requery source of data for Form.
0
 
AAlpha1Author Commented:
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
0
 
thenelsonCommented:
You're welcome.  Glad to help and thank you very much for the points with "A" grade!

Happy Computing!

Nelson
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now