How to build a vba function to perform an operation X num of times

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andre cheese
    New Member
    • Nov 2010
    • 4

    #1

    How to build a vba function to perform an operation X num of times

    I'm building a mthly task schedule (table-"mth_tasks" ) which is populated from table-"master". "Master" is a list of all possible tasks that could be scheduled in a month. "Input" table lists only those specific tasks for the specific mth and the number of times (iterations) a task may be performed during that mth. For ex, "Input" has task A6 "create extract", which must be performed 10 times. I want the vba function to append task A6 from the master list to the mthly schedule 10 times.

    I don't know vba and cannot determine how to do this in a macro without creating a vba loop statement. I don't know how to structure the function statement to perform the operation "x" times based on the value of the "iterations " field in the "Input" table.
    Thank you very much for any help you can give me.
  • gnawoncents
    New Member
    • May 2010
    • 214

    #2
    Let me see if I understand.

    You want to take this:

    TABLE
    -----
    master

    RECORD
    ------
    Name: A6 / Iterations: 10
    Name: A7 / Iterations: 5
    Name: A8 / Iterations: 2
    Name: A10 / Iterations: 12
    Name: A15 / Iterations: 1


    When you select A6 (combo box?) from a form, you want A6 to show up like so:

    TABLE
    -----
    Input

    RECORDS
    ------
    Name: A6
    Name: A6
    Name: A6
    Name: A6
    Name: A6
    Name: A6
    Name: A6
    Name: A6
    Name: A6
    Name: A6

    Is that what you're trying to accomplish? If so, what fields are in the two tables that must be transferred? If not, please explain further. Thanks.

    Comment

    • andre cheese
      New Member
      • Nov 2010
      • 4

      #3
      Yes, actually, "Input" was my form/combo box (the user would select and update the # of times that a task would need to be performed). I was just going to have a simple form that the user could modify the # of iterations desired. But yes, you are on the right path.

      All fields from the the Master table, including the primary field "Process" which identifies certain groups of tasks. For ex. A6 represents 8 different records/tasks and A7, say 9 different records. The Input table, as you have identified above, will also have the "Process" field and a field named Business Day.

      Based on your example, in theory, Insert [Input] into [Mthly schedule] where [Input].[Process] = [Master].[Process]. The key being, if the combo box says iterations =10, then the collection of records, identified by "Process" in the Input table will be appended from Master, 10 times into [Mthly schedule]. I hope that helps. And thank you.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32669

        #4
        In simple terms, your procedure would look something like the following :

        Code:
        Public Sub Iterate(strValue As String, intTimes As Integer)
            Dim intX As Integer
        
            For intX = 1 To intTimes
                'Code to append the value once here
            Next intX
        End Sub
        Does that answer your question (It does of course, but are you happy with it)?

        Comment

        • andre cheese
          New Member
          • Nov 2010
          • 4

          #5
          NeoPa,
          Thank you. This is a subroutine? So I'll have to noodle on how to make it actually work in the db. In other words, build a process/function to incorporate this within a macro or form. I'm going to try to make a small model db and see if I can make it work to be happy.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32669

            #6
            The procedure is certainly a Subroutine procedure yes.

            I guess you're saying you want to know how to go about adding the data too. I expect we can help there too, but we prefer to keep threads to a single topic. If you'd like to create a new thread (question) about how to append data then please go ahead. You can even post a link to it in here if you'd like those of us already involved here to be aware of it. I suggest you include some more detailed information as to the structure of the data you're working with though. We can provide more general answers that don't require knowledge of what you're working with, but experience tells me that you're much less likely to understand the suggestion if it's not tailored to your needs. This is experience of posters generally of course, rather than any knowledge I have of you personally, in case that wasn't clear.

            Comment

            • andre cheese
              New Member
              • Nov 2010
              • 4

              #7
              NeoPa,
              Yes, since I'm a novice here, you are probably correct, it might make more sense to me if I can relate it to what I'm doing. I will mockup a small db to hopefully provide more insight into my problem.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32669

                #8
                Feel free to post a link in here to your new (related) thread when you have one.

                Comment

                Working...