Results 1 to 8 of 8
  1. #1
    MegA248 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2013
    Posts
    3

    Loop Assistance


    In Access 2007, we have a form (“Run CASE IBNR”) that we manually enter in the year in a text box in descending order (2013, 2012, etc.) for fifty years. After each year is entered, a macro (“Run Macro”) is tied to a button and used to kickoff multiple queries. The queries and macro are written and working properly but obviously, manually entering in the year 50 times is a huge pain. I’ve done some research and I believe this can be solved using a loop in Visual Basic. Any ideas on how to do that?

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Google "vba access for next loop syntax".

    This really doesn't make sense (to me).... but you asked..

    Here is the basic (no pun intended) code:
    Code:
       Dim k As Integer
       Dim StartYr As Integer
       Dim sSQL As String
       Dim TheYear As Integer
    
       'enter the year
       StartYr = 2013
    
       For k = 0 To 49
          TheYear = StartYr - k
          
          sSQL = "INSERT INTO YourTableName ( YourFieldName) VALUES ( " & TheYear & ");"
    
          CurrentDb.Execute sSQL, dbFailOnError
    
       Next
    
       MsgBox "Done"
    Change the table name and field name (in BLUE) to your table and field names.
    You could have a control (textbox) on a form to enter the start year and a button to run this code.

    BTW, it really doesn't matter the order in which the years are entered. Records in a table do not have an "order"; a table is just a "bit bucket".

  3. #3
    MegA248 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2013
    Posts
    3
    Hi Steve,

    Thanks so much for your response!! I agree with you that this doesn't make a lot of sense...I didn't build it. I just have to use it, frequently

    I know these are rookie questions but I am completely new to this so any help is greatly appreciated - in the code above, you reference a table and field name. I tried entering in the form for the table and the macro for the field and I get an error (run-time error '3134'). How can I incorporate the current macro in place into the code without getting an error? The macro takes the year entered and runs it through 13 queries. Also, the amount calculated for each year is not displayed in any table (and I don't want it to) and each year uses the amount from the previous year to provide the correct calculation.

    Help!

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I don't know/understand what your process is.....

    You say
    we manually enter in the year in a text box in descending order (2013, 2012, etc.) for fifty years
    So the form “Run CASE IBNR” (shouldn't use spaces - I know, you didn't design this ) is a bound form? You manually enter 50 years then click a button that executes 13 queries?

    Would you post your code?

    See attached mdb for looping example

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Maybe try setting the DefaultValue property of the textbox. In the textbox BeforeUpdate event:

    Me.textbox.DefaultValue = Me.textbox - 1

    Now when you initiate the next record in the same session, the box will populate with the incremented value.

    You can even have the textbox DefaultValue have a starting value of 2013 or calculation such as Year(Date()). The programmatic change will not be saved when form closes so it starts all over again in a new session.
    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.

  6. #6
    MegA248 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2013
    Posts
    3
    The form "RUN CASE IBNR" has a textbox to enter the carryforward year. After you enter each year starting with the current and working backwards fifty years, you click the "Run Macro" button that runs 13 queries in the background (the macro kicking off the queries and the queries themselves are working properly). Here is my tedious process - I enter the year 2013, click the run macro button, 13 queries are run. I enter 2012, click the run macro, button 13 queries are run, etc for fifty years. The amount we are calculating within the queries rely on the amount produced from the previous year, however each amount is not presented on the form. Also, we are only concerned with the total amount calculated going back fifty years, not the amount for each year specifically.

    To make matters more difficult, this database is #2 of #5 of this process and all of them are linked together. Also, the only code I have is what you provided above, I told you I am a beginner Can I provide screenshots of specific areas that would help make this more clear?

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    So this is an unbound form and unbound textbox, the year value is not feeding into a record? You are using macro not VBA? With VBA could easily enter just one year value and have code loop 50 times, reverse incrementing the year and running the 13 queries in each loop. What are these 13 queries doing? Building loop structure in a macro is not as easy but is doable. https://www.accessforums.net/program...ype-34102.html
    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.

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Like June says, VBA could make this less mind numbing..

    I don't use macros, but there is a VBA command that will execute your macro string. Fairly easy to add the code. Can you post the dB?
    Make a copy, delete the data in the copy, compact and repair, then zip it and attach it. (See the bottom of June's post)

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

Similar Threads

  1. Replies: 3
    Last Post: 03-10-2013, 07:04 AM
  2. Assistance with SQL syntax.
    By gm_lowery in forum Access
    Replies: 6
    Last Post: 06-27-2012, 12:07 PM
  3. dlookup assistance
    By underscore in forum Access
    Replies: 4
    Last Post: 12-28-2011, 06:32 AM
  4. Assistance on DB Design required - please
    By Optimus_1 in forum Database Design
    Replies: 13
    Last Post: 07-05-2010, 04:38 PM
  5. Need Assistance with Forms
    By JDA2005 in forum Forms
    Replies: 5
    Last Post: 06-30-2009, 03:37 PM

Tags for this Thread

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