+ Reply to Thread
Results 1 to 4 of 4

Getting Data From User And Storing Into Array

  1. #1
    Registered User
    Join Date
    07-05-2007
    Posts
    44

    Getting Data From User And Storing Into Array

    Hi - this might seem like a really dumb question, but I'm having a lot of trouble with a userform I'm creating.

    I would like to get a list of strings from the user and store them into an array.

    First, I would like to have the user define the size of the list. Next, an array will be created based on that size, and the user will be able to enter strings into a userform until the array is filled.

    The way I'm doing it now is this: I call a first userform to get the size of the list from the user. Then, for however large that list is, I will call a second userform that many times and each time the user will enter a string and it is stored in an array.

    My problem is: when the user enters the size of the list, an array of that size is created in a standard VBA module. In that same module, I call the second userform that many times and the user enters the string. However, to access this string I have to get it from the "OK_Button" event of this userform. I would like to store it in an array right there and then, but it cannot access the array, which is in the standard module. Niether can I declare the array in any event of this userform, because each time I call the userform from the standard module, the array is wiped out and redefined.

    I did come upon a pseudo-solution by sending the string each time to a different procedure in the standard VBA module, and storing it into the array from there. However, to do this I had to make the array and the counter that keeps track of the array index both module-level variables so they could be accessed from this procedure. I really don't enjoy doing that because I'd rather have everything encapsulated, and I know that there has to be an easier way to do this.

    Here is a description of what the code runs:

    From my first userform that gets size of list:

    Please Login or Register  to view this content.
    From standard VBA module, a procedure that redims array size (the array is declared at the top of this module) and calls the second userform that many times:

    Please Login or Register  to view this content.
    From the second userform that gets the string from the user and calls a procedure in the standard module which stores it:
    Please Login or Register  to view this content.
    From the standard VBA module, the procedure that gets this string and stores it into the array. Notice that "ListArray" and "i" were defined at the top of this module, so I am able to access them:
    Please Login or Register  to view this content.



    Does anyone know if there is a way to do this using better encapsulation or a different method???

    Thanks so much!

    Dan

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Dan

    See if the attached gives you some ideas. I've used a couple of global variables, and reused the same textbox in the one form rather than trying to use multiple forms.

    Open the form, enter 2, then press the button.
    Enter some text, then press the button.
    Again enter some text then press the button.

    At this stage the label will adivse you can enter no more text.

    If you press the button again (either with, or without entering more text), you will get a warning message and a comma separated list of the entries.

    The last bit was only to show you that the data was collected in the array, and available for use.

    HTH

    rylo
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    07-05-2007
    Posts
    44
    Wow - that is perfect, exactly what I was looking for - thanks a bunch!

    Dan

  4. #4
    Forum Contributor Tarball's Avatar
    Join Date
    04-21-2007
    Location
    Atlanta, GA
    Posts
    166
    Be careful with the .FIND method. In your code you are actually looking for a any part matching the text (i.e. not an exact match).

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1