Results 1 to 3 of 3
  1. #1
    karmacable is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2011
    Posts
    24

    More efficient way of repeating code

    Hi Guys,



    Generally new to VBA and had a long piece of code I simply wanted copied to a bunch of different 'AfterUpdate' properties for combo boxes (have hundreds of these combo boxes in my form).

    I'm having to repeat the same long code to each one, and if I update the code then I have to manually go through to every 'AfterUpdate' and add in that piece. I'm hoping to have it one place as some kind of routine, and simply refer to that routine with a single word/line in each property.

    The code is fairly simple, I'm just setting up variables and assigning them the value from a lookup column. Here's the code for reference;
    -------------------------------------------
    Code:
     Dim PurchaseCamARig As Currency
        Dim PurchaseCamACam As Currency
        Dim PurchaseCamALens1 As Currency
        Dim PurchaseCamALens2 As Currency
        Dim PurchaseCamAMedia As Currency
        Dim CostCamARig As Currency
        Dim CostCamACam As Currency
        Dim CostCamALens1 As Currency
        Dim CostCamALens2 As Currency
        Dim CostCamAMedia As Currency
        
        PurchaseCamARig = Me.cmb1stCamARig.Column(2)
        PurchaseCamACam = Me.cmb1stCamACam.Column(2)
        PurchaseCamALens1 = Me.cmb1stCamALens1.Column(2)
        PurchaseCamALens2 = Me.cmb1stCamALens2.Column(2)
        PurchaseCamAMedia = Me.cmb1stCamAMedia.Column(2)
        CostCamARig = Me.cmb1stCamARig.Column(3)
        CostCamACam = Me.cmb1stCamACam.Column(3)
        CostCamALens1 = Me.cmb1stCamALens1.Column(3) * Me.txt1stCamALens1Q
        CostCamALens2 = Me.cmb1stCamALens2.Column(3) * Me.txt1stCamALens2Q
        CostCamAMedia = Me.cmb1stCamAMedia.Column(3)
        
        Me.txt1stCamAPurchaseSubTotal = PurchaseCamARig + PurchaseCamACam + PurchaseCamALens1 + PurchaseCamALens2 + PurchaseCamAMedia
        Me.txt1stCamACostSubtotal = CostCamARig + CostCamACam + CostCamALens1 + CostCamALens2 + CostCamAMedia
    ---------------------------------------------------

    Thanks for any help!

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Put the code in a Sub procedure that can be called by any combobox. Are all these comboboxes on the same form? Put that code in Sub behind the form. Go to the VBA editor and in the form code module, simply start typing (I named it Calcs, name it what you want):

    Private Sub Calcs()

    'code here

    End Sub

    Then in the event for the combobox in place of all the lines of code, call the sub: Calcs
    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
    karmacable is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2011
    Posts
    24
    Awesome, that was exactly what I needed! Thanks....

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

Similar Threads

  1. Replies: 7
    Last Post: 08-03-2011, 08:00 AM
  2. Efficient Process?
    By compooper in forum Database Design
    Replies: 1
    Last Post: 06-14-2011, 03:01 PM
  3. Repeating info on form
    By bjr001 in forum Forms
    Replies: 1
    Last Post: 11-16-2010, 01:58 PM
  4. Repeating Records
    By windwardmi in forum Reports
    Replies: 13
    Last Post: 05-22-2010, 02:54 PM
  5. Are text primary keys less efficient than autonumbers?
    By bar tomas in forum Database Design
    Replies: 4
    Last Post: 05-11-2009, 09:37 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