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

    passing a control name as as variable

    I use the following code and it works but I am trying to convert the repeating code into a function
    The function in a module


    Code:
    Sub ModkMilk(FormName)
    
    Dim ctl As Control
    
    If Forms(FormName).milk = -1 Then
        For Each ctl In Forms(FormName).Controls
            If ctl.Tag = "milk" Then
                ' do stuff
    End Sub
    
    Sub ModCheese(FormName)
    
    Dim ctl As Control
    
    If Forms(FormName).cheese = -1 Then
        For Each ctl In Forms(FormName).Controls
            If ctl.Tag = "cheese" Then
                ' do same stuff
    Code on form:
    Code:
    Private Sub Form_Current()
    
        Dim FormName As String
        FormName = Me.Name
        
        ModMilk (FormName)
        ModCheese(FormName)
        ' etc
    End Sub
    I have this code repeated for each product checked. The milk variable can be swapped out for cheese, carrots, onions, green_beans, etc.

    I want to make it a function that passes both the FormName and the ProductName (which is referenced twice as tag matches product control name).

    Tried below but it bombs:

    Code:
    Sub ModProduct(FormName), (ProductName)
    
    Dim ctl As Control
    
    If Forms(FormName).ProductName = -1 Then       'bombs here
        For Each ctl In Forms(FormName).Controls
            If ctl.Tag = "ProductName" Then
                ' do stuff
    
    Private Sub Form_Current()
    
        Dim FormName As String
        Dim ProductName As String
        FormName = Me.Name
        
        ModProduct(FormName), ("milk")
        'ModProduct(FormName), ("cheese")
        'ModProduct(FormName), ("carrots")
        'ModProduct(FormName), ("onions")
        ' etc
    End Sub
    If I change function code to below it works, so obviously I am having a hard time passing the variable to a control name.:

    Code:
    If Forms(FormName).milk = -1 Then
        For Each ctl In Forms(FormName).Controls
            If ctl.Tag = "ProductName" Then
                ' do stuff
    Also, is there a way to pass all the variables as a list in a single command, such as

    Code:
    MyList = ("milk", "cheese", "carrots", "onions")
    ModProduct (FormName), (MyList)
    As that would be even more awesome.

    Thanks.

  2. #2
    drexasaurus's Avatar
    drexasaurus is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    Santa Ana, CA
    Posts
    60
    Have you tried:
    Forms(FormName).Controls(ControlName)
    ControlName being the variable in the above example. You might want to reference the actual attribute you want like Controls(ControlName).value.

    As far as passing multiple values as one, look up vba and arrays, or paramarrays on google (paramarrays being vba's version of a variable number of parameters, handled as an array in the sub/function). You could also split a string with commas, but I generally advise against that if possible.

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,624
    A procedure can have multiple arguments, separate them with comma. If data type not explicitly declared, they will be Variant.

    Sub ModProduct(FormName, ProductName)

    Then pass values to the arguments when procedure is called:

    Static literal text input:
    Call ModProduct("form name", "product name")

    Variable input:
    Call ModProduct(Me.Name, Me.textboxname)

    Why would you need multiple products passed in one call? Code could build and pass a comma separated string and then parse the items into an array then loop through array and do something with each element.
    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.

  4. #4
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111
    Quote Originally Posted by drexasaurus View Post
    Have you tried:
    Forms(FormName).Controls(ControlName)
    ControlName being the variable in the above example. You might want to reference the actual attribute you want like Controls(ControlName).value.
    Adding the keyword "Controls" did the trick. Many thanks.

    Quote Originally Posted by drexasaurus View Post
    As far as passing multiple values as one, look up vba and arrays, or paramarrays on google (paramarrays being vba's version of a variable number of parameters, handled as an array in the sub/function). You could also split a string with commas, but I generally advise against that if possible.
    I imagine I will have to loop through it also so that each time ProductName changes, it runs through the code again. I would rather call the function once per form than 10 times per form.

    My brain hurts and my mind is mush. I will shelve learning about arrays for next week. I'll call each product separately for now.

    Again, thank you.

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

Similar Threads

  1. Passing variable in DLookup (VBA)
    By rustynails in forum Programming
    Replies: 2
    Last Post: 08-22-2013, 10:43 AM
  2. Passing a variable back from a sub
    By RonL in forum Programming
    Replies: 7
    Last Post: 01-25-2013, 12:10 AM
  3. Variable Not passing to query
    By chris.williams in forum Queries
    Replies: 2
    Last Post: 10-14-2011, 08:30 AM
  4. Passing SQL result into variable
    By jonny in forum Access
    Replies: 3
    Last Post: 10-18-2009, 07:46 AM
  5. Passing a variable to a form
    By cjamps in forum Forms
    Replies: 0
    Last Post: 03-02-2009, 05:32 AM

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