Re: Arrays and TextBoxes

On Jun 12, 12:20 pm, MattW <winber...@xxxxxxxxx> wrote:
Hi all.  I've got 2 textboxes in an HTA, and I'm trying to pull the
values of the textboxes into an array.  Here's what I've got code

                <TITLE>Users Group Migration HTA</TITLE>
                        ICON="http://YourURL/your icon.ico"
        <SCRIPT Language="VBScript">
        Sub Window_onLoad
window.resizeTo 640, 480
End Sub

' Cancel button
Sub ExitProgram
End Sub

Dim arrUsers(1)
arrUsers(0) = User1.value
arrUsers(1) = User2.value
Function myBox(x)
    myBox = arrusers(i)
    End Function

'Let's use our function!
Sub btnComp_Click
Dim result
For i = 0 To 1
If i = 0 Then
result = myBox(i)
MsgBox (result)
ElseIf i = 1 Then
MsgBox "This is what comes next."
End If
End Sub
        <body STYLE="font:10pt arial; color:white;
        <p align=left><b>Group Migration Tool</b></font></p>
 <col align="left"></col>
 <col width="160"></col>
 <col align="left"></col>
 <th><b>New User ID.</b></th>
 <th><b>Template User ID.</b></th>
  <td><input type="text" name="User1" size=20><p align=left></td>
 <td><input type="text" name="User2" size=20><p align=left></td>
        <td><input type="button" value="Compare" name="btnComp"
onClick="btnComp_Click" STyle='background-color: ButtonFace;'
         <td><input id=runbutton type="button" value="Cancel"
onClick="ExitProgram" STyle='background-color: ButtonFace;'
<Span ID = "UserGroup1"</Span>
<Span ID = "UserGroup2"</Span>

When I try to run it, I immediately get an Object Required error on
User1 in line 34.  I don't understand why.  I've been googling, and
I've seen solutions that involve assigning variables to each textbox,
but I had thought that defining them in the array would take care of
that.  Can someone show me where I'm going wrong?  Thanks!

The problem lies in the fact that the array assignments are occurring
before the page has had time to load. This is because your code
places the assignments outside of the initialization routine. Code in
this 'global' space is executed as the page loads, which means it is
executed before the USERx elements are instantiated. This also has
the major problem of making them statically defined, so that the
contents of the array cannot change in response to the user's input.

This can be fixed in one of two ways:

1. Move the assignment statements into the Mybox routine (but leave
the DIM statement outside to create the needed global variable). That
is ...

Function myBox(x)
arrUsers(0) = User1.value
arrUsers(1) = User2.value
myBox = arrUsers(x).value
End Function

or, a better way

2. Change the two textboxes to have the exact same name, say
arrUsers, and access them directly as an array of textboxes, as in ...

<td><input type="text" name="arrUsers" size=20><p align=left></td>
<td><input type="text" name="arrUsers" size=20><p align=left></td>


Function myBox(x)
myBox = arrUsers(x).value
End Function

Then you don't need the array or its value assignments. In fact the
need for the function goes away as well, as the arrUsers element array
can be directly accessed where needed.

Tom Lavedas

Relevant Pages

  • Re: Arrays and TextBoxes
    ... values of the textboxes into an array. ... Dim arrUsers ...
  • Re: "Sorting" assignment
    ... If the array is already sorted, this means that you end up ... less time than a bubble sort, ...     int intLeft, ... I don't declare anything inside of a loop is why. ...
  • Re: #defining an array within another in C language
    ... you do not know the number of elements of the array. ...     int var; ... C99 flexible array members or the plain old "struct hack". ...
  • Re: Generic programming in C
    ... struct tag$ArrayInterface; ...     unsigned int Flags; ... specific type as the contents of the array. ...
  • Re: Allocattable and namelist object
    ...   NAMELIST/TOTO/TAB ... array can be in a namelist object. ... The shape of an allocatable array can ... A deferred-shape array is an allocatable array or an ...