Results 1 to 5 of 5
  1. #1
    MatthewGrace is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    159

    Passing a control as a parameter to a code module.

    I've created my first standalone module with code such as:

    Public Sub Dimensions(ctrl As Control)


    // code
    end sub

    I try to invoke it in a Form with a textbox on it as follows:
    Private Sub txtDim1_Exit(Cancel As Integer)
    Dimensions (txtDim1)
    End Sub

    For some reason the line with "Dimensions (txtDim1)" trips run time error 424: 'Object Required'. I'm having trouble comprehending the message because I am passing an object (txtDim1 is a textbox after all). I'm probably missing something obvious.

    Thank you to anyone who helps me find it.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,816
    You aren't passing a control object, you are passing the value that is in the control.

    Why would you pass control object and not value in the control?

    Want to post entire procedure for analysis?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    MatthewGrace is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    159
    Sure. The purpose is this: I have an input mask to force entries into a textbox to come out with inches and fractions like so: "24 1/8". Trouble is, the fraction portion is completely optional, in real life you can have a dimension that is just "24" with no fraction... .inputmask doesn't like that. Inputmask does support optional characters, however, the / used to separate the numerator and the denominator remains. It looks gawky so I made some code that senses whether the user has entered a whole integer value and has no need for a fraction. If so, .inputmask = "" to eliminate the / character.

    'When the user tabs out of the textbox, send it to the Public Sub Dimensions(ctrl as Control) for refinement.
    Private Sub txtDim1_Exit(Cancel As Integer)
    Dimensions (txtDim1) //This line throws run time error 424: Object Required.
    End Sub

    Public Sub Dimensions (ctrl as Control)

    '25" is too small, force them to enter something larger.
    If Left(ctrl.Value, 2) < 25 Then
    MsgBox "Number too small."
    Cancel = True
    Exit Sub
    End If

    'If the len() returns 4, they can only have entered an integer value...
    ' so, get rid of the / from inputmask.

    If Len(ctrl.Value) = 4 Then
    ctrl.InputMask = ""
    ctrl.Value = Left(ctrl.Value, 2) 'Rewrite, omitting all but the 2-digit integer

    'If the len() is greater than 4, they must be trying to enter a fraction
    ' so, ensure its a multiple of 1/8"

    ElseIf Len(ctrl.Value) > 4 Then
    If Right(ctrl.Value, 3) = "1/8" Then Exit Sub
    If Right(ctrl.Value, 3) = "1/4" Then Exit Sub
    If Right(ctrl.Value, 3) = "3/8" Then Exit Sub
    If Right(ctrl.Value, 3) = "1/2" Then Exit Sub
    If Right(ctrl.Value, 3) = "5/8" Then Exit Sub
    If Right(ctrl.Value, 3) = "3/4" Then Exit Sub
    If Right(ctrl.Value, 3) = "7/8" Then Exit Sub

    'They should only get this far if they entered something invalid.
    MsgBox "You must enter a valid fraction in 1/8 inch increments", vbOKOnly, "Invalid Fraction"

    Cancel = True
    Exit Sub

    End If

    End Sub

    Bear in mind, all this code works just fine if I place it not in a separate module, but appropriately named under the txtDim1_Exit event. The reason I'm placing it in a separate code module is for reuse with a great deal of textboxes that need the same functionality through my application. Lastly, in hindsight I realize the Cancel = True statements will not work in the separate module... if I could even get that far.

    Thanks for helping.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,816
    If you pass the control then probably have to also pass the source form name. Can get really complicated if you want to use with subforms. Pass the value and consider a Function instead of Sub.
    Code:
    Public Function Dimensions(strInput As Variant) As Variant
        If IsNull(strInput) Then
            'do nothing, user cleared the field and Null is acceptable
            Dimensions = Null
        ElseIf Val(strInput) < 25 Then
            '25" is too small, force them to enter something larger.
            MsgBox "Number too small."
            Dimensions = Null
        ElseIf Len(strInput) = 4 Then
            'If the len() returns 4, they can only have entered an integer value...
            ' so, get rid of the / from inputmask.
            Dimensions = Val(strInput)
        ElseIf Len(strInput) > 4 Then
            'If the len() is greater than 4, they must be trying to enter a fraction
            ' so, ensure it is a multiple of 1/8"
            Select Case Right(strInput, 3)
                Case "1/8", "1/4", "3/8", "1/2", "5/8", "3/4", "7/8"
                    Dimensions = strInput
                Case Else
                    'They should only get this far if they entered something invalid.
                    MsgBox "You must enter a valid fraction in 1/8 inch increments", vbOKOnly, "Invalid Fraction"
                    Dimensions = Null
            End Select
        End If
    End Function
    Then call the function.

    Private Sub txtDim1_AfterUpdate()
    Me.txtDim1 = Dimensions(Me.txtDim1)
    End Sub


    This value is not used in calcs? This was an issue I faced in my development efforts. I put my foot down and declared "no fractions, only decimals".
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    MatthewGrace is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    159
    Thanks for responding, in addition to solving the main problem, I also picked up on several other coding subtleties.
    Will mark as solved.

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

Similar Threads

  1. Query - Not passing a module on time
    By Moss in forum Modules
    Replies: 1
    Last Post: 06-05-2013, 12:43 AM
  2. Parameter Passing
    By Juan4412 in forum Queries
    Replies: 1
    Last Post: 11-21-2011, 10:23 AM
  3. Passing List of Parameter
    By vignes10 in forum Access
    Replies: 3
    Last Post: 09-15-2011, 07:35 AM
  4. Replies: 7
    Last Post: 12-29-2010, 04:07 PM
  5. Macro passing a parameter
    By SlowPoke in forum Access
    Replies: 1
    Last Post: 09-26-2010, 09:57 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