Results 1 to 5 of 5
  1. #1
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111

    passing a form name to a function woes

    Trying to pass a form name to a function and and not succeeding. Suggestions to code alterations?

    Code behind Main_Menu button on form is as follows:

    Code:
    Private Sub Main_Menu_Click()
    
    
    'passing the form name (fmProducts) when the button is clicked to Module1 function
    
        Module1.returnGUI ("fmProducts") 
    
    End Sub
    Function, called Module1 is as follows:

    Code:
    Public Function returnToMain(theFormName As String)
        Forms!fmInventory.Visible = True
        DoCmd.Close acForm, theFormName
    End Function
    Get a 424 "Object required" Error message in code beneath button.



    What am I doing wrong and why?

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You can't refer to a sub/function like that. Functions usually return a value, so it has to go on the right of equation:

    SomeVariable = returnGUI ("fmProducts")


    Try (as a function)
    Code:
    Private Sub Main_Menu_Click()
    Dim Bln as Boolean
    
    'passing the form name (fmProducts) when the button is clicked to Module1 function
    
        bln = returnGUI ("fmProducts") 
    
    End Sub
    
    
    Public Function returnToMain(theFormName As String)
        Forms!fmInventory.Visible = True
        DoCmd.Close acForm, theFormName
    
        returnToMain = True
    
    End Function

    You can call a subroutine.
    Code:
    Private Sub Main_Menu_Click()
    
    'passing the form name (fmProducts) when the button is clicked to Module1 function
    
      Call returnGUI ("fmProducts") 
    
    End Sub
    
    
    Public Sub returnToMain(theFormName As String)
        Forms!fmInventory.Visible = True
        DoCmd.Close acForm, theFormName
    
    End Sub

  3. #3
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111
    Thanks a million.

    Went with the function. I changed the function name to returnGUI and didn't catch that when I copied and pasted routine from notepad so code ended up looking like this:

    Code:
    Private Sub Main_Menu_Click()
    Dim bln as Boolean
    
    'passing the form name (fmProducts) when the button is clicked to Module1 function
    
        bln = returnGUI ("fmProducts") 
    
    End Sub
    
    
    Public Function returnGUI(theFormName As String)
        Forms!fmInventory.Visible = True
        DoCmd.Close acForm, theFormName
    
        returnGUI = True
    
    End Function
    Just curious, I don't understand how
    Code:
    bln = returnGUI ("fmProducts")
    actually works. Aren't you just making bln equal to something? How is it called? and how does it know what module it is in?

    Got any good on-line refences you can point me to that explain this in simple, easy to understand terms? Trying to learn how to fish, so to speak.

    Also, making Bln as String and returnGUI = "" also worked. Why did you go with Boolean?

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    In doing all of the cutting and pasting I dropped a bit of code.

    The function should be
    Code:
    Public Function returnGUI(theFormName As String) As Boolean
        Forms!fmInventory.Visible = True
        DoCmd.Close acForm, theFormName
    
        returnGUI = True
    
    End Function
    In "Main_Menu" code, you could have error handling code that would check the variable "bln" to determine if the called function completed successfully.

    Since you do not need a return value, I would use a Sub instead of a Function.


    Also, making Bln as String and returnGUI = "" also worked. Why did you go with Boolean?
    However, what happens if "fmInventory" is closed? You get an error. In the error handler of the function "returnGUI", you would set "returnGUI" to FALSE.
    In the "Main_Menu" code, you would check to see if "bln" was True (completed successfully) or False (there was an error) and have code to decide what you wanted to do.


    Or you could change "bln" to an Integer (and change the name). The return (error) code could be:
    Code:
    0 = successful
    1 = form could not be made visible because it was not loaded
    2 = couldn't close "theFormName"
    3 = shoot the programmer and quit. (<< I jokes :p)
    Then code in "Main_Menu" could branch to handle the error in the function.


    Even though this is for Excel, it is a good description
    http://www.excelfunctions.net/VBA-Fu...broutines.html


    The Difference Between Subroutines and Functions
    http://flylib.com/books/en/3.91.1.17/1/


    Be sure and read Post#6 here
    http://www.access-programmers.co.uk/...ad.php?t=70998


    This is about VB6, but still relevant:
    http://en.wikiversity.org/wiki/Visua...nd_Subroutines

  5. #5
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111
    Awesome references. I will try to surf these over the weekend. My brain hurts today.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Simple passing into function question
    By Ruegen in forum Programming
    Replies: 3
    Last Post: 07-24-2014, 11:23 PM
  2. VBA passing rs! into function
    By Ruegen in forum Programming
    Replies: 15
    Last Post: 03-17-2014, 04:01 PM
  3. Replies: 11
    Last Post: 05-17-2013, 06:10 AM
  4. Combo Box Woes
    By tbassngal in forum Forms
    Replies: 2
    Last Post: 09-01-2011, 08:54 AM
  5. Combo box woes...
    By jonbonazza in forum Forms
    Replies: 3
    Last Post: 06-21-2010, 11:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums