Results 1 to 5 of 5
  1. #1
    EonsTimE is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Posts
    12

    Question Passing arguments to a public sub

    Hi, I'm learning how to pass arguments to public functions and subs when pressing a button.


    For my function this works fine, but when I try to execute a sub, I get error message:

    Compile error: Argument not optional.

    I know the problem lies in button call, because going through the sub with F8 executes it properly.
    I've tried putting some arguments in the button call, but then I get a syntax error.
    Halp!



    This is the code for buttons:

    Code:
    Private Sub Command25_Click()
    Module1.fIntroFunction
    End Sub
    
    
    Private Sub Command26_Click()
    Module1.Starter
    End Sub
    Module1 contents:

    Code:
    Option Compare Database
    
    Public Function fIntroFunction()
    Dim lngLength As Long
    Dim lngWidth As Long
    Dim lngHeight As Long
     
    lngLength = 150
    lngWidth = 75
    lngHeight = 50
     
    MsgBox "Volume of space: " & FormatNumber(fCalculateVolume(lngLength, lngWidth, lngHeight), 0) & _
                " Cubic Feet"
    End Function
    
    
    Private Function fCalculateVolume(lngLen, lngWdth, lngHgt) As Long
      fCalculateVolume = (lngLen * lngWdth * lngHgt)
    End Function
    
    
    Private Sub Jumper()
    
    
    Dim current As Date
    Dim owner As String
     
    current = Now()
    owner = CurrentUser()
    
    
    Call Starter(current, owner)
    End Sub
    
    
    Public Sub Starter(first As Date, second As String)
      MsgBox "Date passed to Starter: " & first & vbCrLf & _
                  "Current User Name passed to Starter(): " & second
    End Sub

  2. #2
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659
    Private Sub Command25_Click()
    Module1.fIntroFunction
    End Sub

    This doesnt pass any information
    Modify your function like

    Public Function fIntroFunction(lngLength As Long, lngWidth As Long, lngHeight As Long)

    'lngLength = 150
    'lngWidth = 75
    'lngHeight = 50

    MsgBox "Volume of space: " & FormatNumber(fCalculateVolume(lngLength, lngWidth, lngHeight), 0) & _
    " Cubic Feet"
    End Function

    And now you can Call Module1.fIntroFunction(150,75,50)

  3. #3
    EonsTimE is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Posts
    12
    Hi Perceptus,
    Thanks for that, unfortunately, it is the Sub routine [Module1.Starter] that throws the error.
    Function [Module1.fIntroFunction] gets fired by the button without arguments passed for now and as you've noticed, the arguments for both Sub and Function are built it.
    I'm trying to figure out why [Module1.Starter] doesn't get fired by button:

    Code:
    Private Sub Command26_Click()
    Module1.Starter 
    End Sub
    

  4. #4
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659
    To call starter you must pass it the arguments. It requires First and Date.

    Try

    Module1.Starter date,"testt"

  5. #5
    EonsTimE is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Posts
    12
    Ahh, so you pass arguments to a Sub without (). That's a new one for me. Thanks for that!
    I've also noticed I've by error switched calls Public/Private for my Subs.
    Meanwhile I've learned how to make arguments Optional, yaay.

    Working example code looks like this:

    Buttons code:

    Code:
    Private Sub Command25_Click()
    Module1.fIntroFunction
    End Sub
    
    
    Private Sub Command26_Click()
    Module1.Jumper
    End Sub
    Module1 code:

    Code:
    Option Compare Database
    
    
    Public Function fIntroFunction()
    Dim lngLength As Long
    Dim lngWidth As Long
    Dim lngHeight As Long
     
    lngLength = 150
    lngWidth = 75
    lngHeight = 50
     
    MsgBox "Volume of space: " & FormatNumber(fCalculateVolume(lngLength, lngWidth, lngHeight), 0) & _
                " Cubic Feet"
    End Function
    
    
    Private Function fCalculateVolume(lngLen, lngWdth, lngHgt) As Long
      fCalculateVolume = (lngLen * lngWdth * lngHgt)
    End Function
    
    
    Public Sub Jumper()
    
    
    Dim current As Date
    Dim owner As String
     
    current = Now()
    owner = CurrentUser()
    
    
    Call Starter(current, owner)
    End Sub
    
    
    Private Sub Starter(Optional ByVal first As Date, Optional ByVal second As String)
      MsgBox "Date passed to Starter: " & first & vbCrLf & _
                  "Current Employee Name passed to Starter(): " & second
    End Sub
    Note I'm not passing arguments from buttons in this code. I call a public function/sub that has arguments and passes them to a private function/sub for action. All for the sake of practice \o.o/

    Thank you for your help, Perceptus.

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

Similar Threads

  1. VBA passing Array arguments to procedure
    By George in forum Access
    Replies: 2
    Last Post: 05-13-2015, 10:03 AM
  2. VBA ByVal vs ByRef Passing Arguments
    By ylatodd in forum Programming
    Replies: 5
    Last Post: 10-22-2013, 02:49 PM
  3. Replies: 11
    Last Post: 05-17-2013, 06:10 AM
  4. Replies: 0
    Last Post: 04-02-2012, 11:30 AM
  5. Replies: 4
    Last Post: 06-13-2011, 12:14 PM

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