MS Access (VS Help)

Discussion in 'OT Technology' started by CyberBullets, Aug 11, 2005.

  1. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    Hey Guys,

    The Sys Admin is having some problems with some Access/VB code. It wasnt our choice of language/database, but we have to fix up the fuckup from the previous people.

    What we are trying to do is loop through all the labels, and getting the sum of them. However, val() is treating Temp as a string, instead of an object. We have tried type casting, to no avail, and I dont use VB/Access to know what is going, since I'm the PHP/Java dev, and only used VB/Access back in 1st year university for 1 RAD course.

    Anyways, here is the code (variable names have been changed for reasons):
    Code:
    Function ValLoop(LineStart As Integer, LineEnd As Integer, LabelStart As String, LabelEnd As String) As Double
        Dim Total As Double
        Dim Temp As String
        
        Total = 0
        
        Do While (LineStart <= LineEnd)
            Temp = LabelStart & LineStart & LabelEnd & ".Caption"
            Total = Total + Val(Temp)
            LineStart = LineStart + 1
        Loop
        
        ValLoop = Total
    End Function
    
    An examle of the Temp would be: "LBL2E4.Caption"
    It should run Val(LBL2E4.Caption) but its running Val("LBL2E4.Caption")

    Any suggestions? TIA!
     
  2. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    so, do you have a number of items on a form or something? i don't think there is a way to do what you are trying to do without looping through the form's object (control) collection
     
  3. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    Yeah Sled is right - you need to use the collections object. Val is treating temp as string because that's exactly what you dimmed it at
     
  4. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    And the sum of labels? WTF? Labels are just text - why would you sum text?
     
  5. EvilSS

    EvilSS New Member

    Joined:
    Jun 11, 2003
    Messages:
    5,104
    Likes Received:
    0
    Location:
    STL
    Yea, as far as I'm aware you can't "build" an object name out of strings. You need to pass the function an object, not a set of strings. If for some reason you don't know the control names, you are going to have to figure out a way to find the ones you want by their properties.
     
  6. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    the labels contain numeric values. its a makeshift spreadsheet :ugh2:

    my suggestion was to sum the information as it was pulled out of the database, i think that is what he is going to do instead.
     
  7. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    I guess without seeing it I can't say more - I'm thinking there's a better solution out there to what you're doing tho
     

Share This Page