Results 1 to 12 of 12
  1. #1
    Seamus59 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    May 2013
    Posts
    34

    Field names in an array


    I have field names in an array fname

    I have the following code in a subroutine

    [Ranking].Value = assessment() ' where '[Ranking]' is a field in the curent recordset

    .....my function assessment attempts to read the value from the field whose name is an element in the array
    Function assessment()
    For I = 0 To UBound(fname) - 1
    assessment = assessment + Me.Recordset(fname(I))
    Next
    End Function

    Me.Recordset(fname(I)) keeps returning null

    any ideas or pointers to where this may have already been answered?

  2. #2
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Not sure what you're trying to achieve.

    Is that function intended to be recursive? If so, you need to have a default value returned (for instance 0) after the loop.

    If not, then when it exits the loop and reaches the end of the function, it will return Null.

    Then, the recursive call will set the value of the prior call to (assessment + Me.Recordset(fname(I))) which is (Null + Me.Recordset(fname(I)) which is Null.

  3. #3
    Seamus59 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    May 2013
    Posts
    34
    I know it's a little confusing, but it's not recursive... I'll give you a little more info.
    I am simply trying to retrieve the value in the current record of the field described by fname(i)

    fname(0)="DGA", so to return the value of the field DGA in the current record which I manipulate then store in a field called Ranking. Just picking up fom one field and storing into another, but the name of the first field is in an array.

    If this helps, I am trying to use this in a form, when someone changes one field, it changes another as well.

    Thanks

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    This code is behind a form? The fields are included in the form's RecordSource?

    Try:

    assessment = assessment + Me!(fname(I))

    Why do you need to save calculated value? And this looks like an aggregate calc which is usually a particularly bad idea to save.
    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.

  5. #5
    Seamus59 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    May 2013
    Posts
    34
    Der, sorry, I'm not too bright...looking at the wrong end...of course it was returning a null, because that's what was in the field! I didn't check that all had been initialized.

    Thanks again!

  6. #6
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    It still won't work because of the uncontrolled recursion. Try something more like this (I assumed Currency type for the function.) -
    Code:
    Function assessment() As Currency
       Dim I As Integer
       Dim tempAssess As Currency
    
       tempAssess = 0
       For I = 0 To UBound(fname) - 1
          tempAssess = tempAssess + Me!(fname(I))
       Next I 
    
       assessment = TempAssess
    
    End Function

  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,930
    Dal, I tested function like OP's original. It works. Don't see need for additional variable.

    However, might want to handle the null because math with null returns null.

    assessment = assessment + Nz(Me!(fname(I)),0)
    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
    Seamus59 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    May 2013
    Posts
    34
    That makes sense, thanks, I'll try that and let you know.

  9. #9
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    If it works, then I've been suffering a delusion about the way function calls work. I had understood that assigning a value to the name of the function immediately returns that value to the calling line, ending any processing in the function itself.

    What, precisely, does this line do when I = 0?
    Code:
    assessment = assessment + Me.Recordset(fname(I))
    Does it ever get to I = 1?

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I think function result is not passed back to calling source until End Function.

    Array element index starts with 0 by default. So fname(0) refers to the first element of the array and grabs its value.

    The For Next increments the I variable. The upper limit is established by the UBound(fname) - 1 calc. UBound returns the number of elements and must subtract 1 because element index begins with 0 so 4 elements would index 0 through 3.

    However, Seamus, I just noticed that your function does not declare or populate an array. Where does that occur?
    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.

  11. #11
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Wow. If that's true, then I've written a lot of functions over the years that mostly worked by serendipity.

    I didn't know you needed an "Exit Function" immediately afterwards to avoid continuing logic- I assumed that was inherent in assigning the return value to the function name. ("Return this", to me, meant "return this NOW".)

    I'm sure that most of my code worked fine anyway, but it's disconcerting to think how many chances I had to code a grave error. Lucky I never worked in aerospace.

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    I didn't mention Exit Function, I said End Function. However, Exit Function can be used to force exiting a loop if some condition is met
    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.

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

Similar Threads

  1. Referencing Field Names
    By andy-29 in forum Access
    Replies: 6
    Last Post: 11-20-2012, 03:27 PM
  2. How do I make a field represent other field names?
    By Alpana in forum Import/Export Data
    Replies: 6
    Last Post: 01-15-2012, 08:41 PM
  3. TransferSpreadsheet - Use first row as field names
    By jgelpi16 in forum Programming
    Replies: 1
    Last Post: 02-23-2011, 11:41 AM
  4. using like with field names
    By TheShabz in forum Queries
    Replies: 4
    Last Post: 10-07-2010, 05:11 PM
  5. Quick way to stuff field names into text field
    By kfinpgh in forum Programming
    Replies: 0
    Last Post: 01-04-2007, 01:13 PM

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