Results 1 to 4 of 4

Thread: [RESOLVED] Variable Length Array?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Location
    Chicago
    Posts
    136

    Resolved [RESOLVED] Variable Length Array?

    I want to use an array, but I don't know what the size will be until I read the number of fields from a DB. All of the fields have the same name with a number appended to them (lr_transaction1, lr_transaction2...) but I'd like to maintain the flexibility to add new fields without having to code and recompile. Any suggestions?



    Code:
    Dim txtTrans() As String
    Dim z As Integer
    Dim strField As String
    
    z = 1
    
    For z = 1 To gsNumTrans
        strField = "lr_transaction" & z
        If IsNull(rsScript.Fields(strField)) Then
            txtTrans(z) = ""
        Else
            txtTrans(z) = rsScript.Fields(strField)
        End If
    Next

    The gsNumTrans variable holds the number of "lr_transaction" fields and is read from an INI file.

    And yes I know I'm using antique DAO technology, but it works and isn't worth the hassle to convert it at this point.
    Last edited by bat711; Dec 20th, 2007 at 03:48 PM.
    CodeBank: Launch IE

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Variable Length Array?

    two words... ReDim and Preserve...

    Code:
    Dim myArray() as Long
    
    ReDim Preserve myArray(100)
    The preserve will preserve any existing data in the array as you resize it. If you DON'T use it, it will ERASE ALL VALUES in the array.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Location
    Chicago
    Posts
    136

    Re: Variable Length Array?

    Ahh...thanks! I didn't think there was any way to declare an array using a variable for the number of elements, but the below worked...


    Code:
    ReDim Preserve txtTrans(gsNumTrans)
    CodeBank: Launch IE

  4. #4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width