Results 1 to 10 of 10

Thread: [RESOLVED] VBA Module help - Passing a form name.

  1. #1

    Thread Starter
    Addicted Member Bazzlad's Avatar
    Join Date
    Jun 2003
    Posts
    227

    [RESOLVED] VBA Module help - Passing a form name.

    Hey all, I want to use a few modules to save repetitive coding in a project:
    such as:

    Code:
    Sub recordposition()
        
        Select Case Me.Recordset.AbsolutePosition
        Case 0
        
        cmdnext.Visible = True
            cmdprevious.Visible = False
        
        Case Else
    
        cmdnext.Visible = True
            cmdprevious.Visible = True
        
        End Select
    
    End Sub
    It crashes on the Me.recordset - how do I pass the form name over to the module?

    Rich
    Last edited by Bazzlad; Nov 8th, 2007 at 09:18 AM.
    *And you'll see, Everything you stand for is fake*
    http://www.rhesusrock.com

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VBA Module help - Passing a form name.

    You don't want to pass the form name (as that would just be a string containing the name), you want to pass the form (which allows you to use the properties etc).

    You would add/use the parameter like this:
    Code:
    Sub recordposition(theForm as Form)
        
        Select Case theForm.Recordset.AbsolutePosition
        Case 0    
            theForm.cmdnext.Visible = True
            theForm.cmdprevious.Visible = False
    ...
    ..and pass the parameter like this:
    Code:
    recordposition Me
    'or:
    Call recordposition (Me)

  3. #3

    Thread Starter
    Addicted Member Bazzlad's Avatar
    Join Date
    Jun 2003
    Posts
    227

    Re: VBA Module help - Passing a form name.

    You're a star Si, thank you!
    *And you'll see, Everything you stand for is fake*
    http://www.rhesusrock.com

  4. #4

    Thread Starter
    Addicted Member Bazzlad's Avatar
    Join Date
    Jun 2003
    Posts
    227

    Re: [RESOLVED] VBA Module help - Passing a form name.

    You still here Si?
    If so, how would I do the same, but reference a DIFFERENT form (not ME!)
    call recordposition(Forms!frmmain) isn't working!!!
    *And you'll see, Everything you stand for is fake*
    http://www.rhesusrock.com

  5. #5

    Thread Starter
    Addicted Member Bazzlad's Avatar
    Join Date
    Jun 2003
    Posts
    227

    Re: [NEARLY RESOLVED] VBA Module help - Passing a form name.

    Here's what I have

    On the form calling it

    Code:
    Call addition(Me, Form_frmsalesbyadd.Form)
    In the module

    Code:
    Sub addition(theform As Form, form2 As Form)
    
        If Not theform.salesnumber.Value = vbNullString Then
        Dim salenumber As String
        salenumber = theform.salesnumber.Value
        Else
        salenumber = ""
        End If
        
        DoCmd.OpenForm form2
        
        form2.txtreturn.Value = salenumber
        
        For Each frm In Forms
            If Not frm.Name = form2 Then
        DoCmd.Close acForm, frm.Name
            
        End If
        Next frm
    
    End Sub
    *And you'll see, Everything you stand for is fake*
    http://www.rhesusrock.com

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Help me! VBA Module help - Passing a form name.

    Did it work for you or did you get errors?

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Help me! VBA Module help - Passing a form name.

    Looks like you are doing this in Access?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Addicted Member Bazzlad's Avatar
    Join Date
    Jun 2003
    Posts
    227

    Re: Help me! VBA Module help - Passing a form name.

    Yes I'm doing this in access,
    and no it doesn't work.

    The first bit works fine - it's when referencing the second form - it just doesn't work, I've tried:

    [forms]![frmsalesbyadd]
    [forms]![frmsalesbyadd].form
    forms!frmsalesbyadd
    forms_frmsalesbyadd

    You name it, I've tried it, so what am I doing wrong?
    *And you'll see, Everything you stand for is fake*
    http://www.rhesusrock.com

  9. #9

    Thread Starter
    Addicted Member Bazzlad's Avatar
    Join Date
    Jun 2003
    Posts
    227

    Re: Help me! VBA Module help - Passing a form name.

    DONE!

    Both forms needed to be open as below:

    Code:
    Private Sub cmdsalesbyadd_Click()
        
        DoCmd.OpenForm ("frmsalesbyadd")
        Call addition(Me, [Forms]!frmsalesbyadd)
        
        For Each frm In Forms
            If Not frm.Name = "frmsalesbyadd" Then
        DoCmd.Close acForm, frm.Name
            
        End If
        Next frm
        
    End Sub


    Code:
    Sub addition(theform As Form, form2 As Form)
        
        If Not theform.salesnumber.Value = vbNullString Then
        Dim salenumber As String
        salenumber = theform.salesnumber.Value
        Else
        salenumber = ""
        End If
        
        form2.txtreturn.Value = salenumber
    
    End Sub
    *And you'll see, Everything you stand for is fake*
    http://www.rhesusrock.com

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] VBA Module help - Passing a form name.

    Yes, the form needs to be open first or it will not be contained in the [Forms] collection. When ever a form is opened Access adds the form to the Forms collection for reference as its in use.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width