Results 1 to 5 of 5
  1. #1
    gangel is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    194

    Using a variable to replace DAO table reference for loop

    Hi ppl,



    SO i have this code:

    Code:
    Set IRS = CurrentDb.OpenRecordset("Select * FROM ITEMS where [deactivate] = false and [ManualInvoicing] = false", dbOpenDynaset)
    Set Tmp = CurrentDb.OpenRecordset("TmpPriceList", dbOpenDynaset)
    
    Do Until IRS.EOF
    ItName = IRS(1)
    StdQty = IRS(2)
    Cost = IRS(3)
    Sched = IRS(5)
    
    
    Tmp.AddNew
    Tmp!ItemName = ItName
    Tmp!CostPrice = Cost
    Tmp!StdQty = StdQty
    Col = 1
    
    
    Do Until Col = 12
    Percent = 0.1 * Col
    TMP REFERENCE: THIS IS WHERE I NEED TO DO Tmp!"Col" & Col <<<<<This is where i create it
    Qty = StdQty * Percent
    
    
    If Qty < 1 Then
    Else
    Call Priceit(ItName, StdQty, Cost, Sched, Qty, Col)
    TMPREFERENCE = invamt <<<<<<<<<<This is where i use it
    End If
    Tmp.Update
    Col = 1 + Col
    
    
    Loop
    
    
    
    
    IRS.MoveNext
    Loop
    So the idea is i have 10 columns literally named Col1 Col2 and so forth.

    As the Variable COL goes up during the loop then the figure is "Invamt" is saved into the column on Tmp according to the variable.

    Thus When Col = 1
    Tmp!Col1

    when it loops and Col = 2
    The the Tmp! becomes Tmp!Col2

    Im sure i should be using TableDef but ive never used it and it confused me quite a bit

    Thanks for any help u can give

    GAngel

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    You have 12 fields with similar names: Col1, Col2, Col3, etc.? This sounds like non-normalized data structure.

    Consider:

    For x = 1 to 12
    Percent = 0.1 * x
    Qty = StdQty * Percent
    If Qty >= 1 Then
    Call Priceit(ItName, StdQty, Cost, Sched, Qty, Col)
    Tmp.Fields("Col" & x) = invamt
    End If
    Tmp.Update
    Next

    Where is invamt value set?
    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
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    First, "deactivate" & "Percent" are reserved words in Access and shouldn't be used for object names.

    It is difficult to suggest good code without the full function/sub, but consider this: (untested)
    Code:
        Dim knt As Integer   'counter
        '.
        '.
        '.
        Set IRS = CurrentDb.OpenRecordset("Select * FROM ITEMS where [deactivate] = false and [ManualInvoicing] = false", dbOpenDynaset)
        Set tmp = CurrentDb.OpenRecordset("TmpPriceList", dbOpenDynaset)
    
        Do Until IRS.EOF
            ItName = IRS(1)
            StdQty = IRS(2)
            Cost = IRS(3)
            Sched = IRS(5)
    
            'add new record
            tmp.AddNew
            tmp!ItemName = ItName
            tmp!CostPrice = Cost
            tmp!StdQty = StdQty
            tmp.Update
    
            '        Col = 1  
            knt = 1
    
            Do Until knt = 12
                Percent = 0.1 * knt
                '  TMP REFERENCE: THIS IS WHERE I NEED TO DO Tmp!"Col" & Col <<<<<This is where i create it
                Qty = StdQty * Percent
    
    
                If Qty >= 1 Then
                    Call Priceit(ItName, StdQty, Cost, Sched, Qty, knt)
    
                    'edit record
                    tmp.Edit
                    tmp.Fields("Col" & knt) = invamt      'TMPREFERENCE = invamt <<<<<<<<<<This is where i use it
                    tmp.Update
                End If
                knt = knt + 1
            Loop
    
            IRS.MoveNext
        Loop

  4. #4
    gangel is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    194
    amazing, thanks june.

    Invamt is set in PRICEIT

  5. #5
    gangel is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2014
    Posts
    194
    thanks steve, i will rename those parts rightaway!

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

Similar Threads

  1. Replies: 6
    Last Post: 04-07-2015, 03:41 PM
  2. Variable to reference a spreadsheet from Access
    By lawdy in forum Programming
    Replies: 9
    Last Post: 03-03-2015, 11:31 AM
  3. Replies: 3
    Last Post: 03-10-2013, 07:04 AM
  4. Variable within form control reference
    By Tyork in forum Programming
    Replies: 2
    Last Post: 10-13-2010, 09:55 AM
  5. Replies: 2
    Last Post: 05-09-2010, 04:10 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