Results 1 to 9 of 9
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    Possible to use this in VBA

    Is it possible to use a statement like this in VBA coding?



    Code:
    'Declaring Name here
    Dim Name(10) As String
    
    'Code using to call Name and it is throwing a debug error on the Name(i) = Name(i+1) 
    j = i
        i = 0
        If j > 10 Then j = 10
    While i <= j
    If i < 19 Then
    FName(i) = FName(i + 1)
     End If
            End If
            i = i + 1
    End
    Debug error says...line saying subscript is out of range? Which is what makes me question is it possible to do this in VBA, I know you can in C# and other languages, thought i'd give it a whirl in VBA.

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    NAME is an Access Reserved Word. Use sName or something else.

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Sorry, It's actually FName. It was a typo on my part.

  4. #4
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Next part -

    j = i
    i = 0

    makes no sense the way it is written. j can't be set to i since i has no value yet.

    also

    FName(i) = FName(i+1)
    makes no sense either. There is no value in FName(i + 1) yet so how can it be used to set the value of the previous item?

    shouldn't one of them be j instead?

    Also, what is it you are really wanting to do here? C# would have a problem with this logic as well.

  5. #5
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Also,

    i & j have not been declared (as in Dim i As Integer, ...)

    You have FName(10), but then you have

    Code:
          If i < 19 Then
             FName(i) = FName(i + 1)
          End If
    IF the array is limited to 10, why do you count up to 19 in the loop?
    I think this is the "subscript is out of range" error.

  6. #6
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Quote Originally Posted by ssanfu View Post
    IF the array is limited to 10, why do you count up to 19 in the loop?
    I think this is the "subscript is out of range" error.
    Actually, I tried running the code EXACTLY as written and I don't think that is the problem with the 'subscript is out of range" error. I think it has to do with the fact that, the way the code is currently written, it just runs until the variant for i gets too large. There is really no end to it - it is an endless loop because of the way it is written.

  7. #7
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 7 Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    It's a database that was used and then the programmer gave up one it, and I am trying to for lack of a better term "revive" it becauase now the same thing is taking place. I just don't understand his coding at all. I removed the j = 1 and it took care of the 1st subscript error mentioned, but now it is throwing the same error on this line:

    Code:
    DisplayText = SqlValues & "," & FName(i) & ",'" & LName(i) & "','" & Phone(i)

  8. #8
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Jo, the problem is here my friend:

    Quote Originally Posted by jo15765 View Post
    FName(i) = FName(i + 1)
    When i gets to 9, you're trying to put the 11th element of the array into the 10th element. And of course, there are only 10 elements available because the array is fixed at (10). And it's base 0 as well. Even if your module is Base 1, it will still throw an error on line this line, but the problem will be the right side of the equality not the left side.

  9. #9
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Any chance of you posting a copy of the MDB, sans data? Do a compact and repair, then zip it to make it smaller.
    It will be much easier for us to see what the code is trying to do and to see the problem......

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

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