TextBox - > Array Help

A

Al & Cay Grant

Hello All,

I have several textboxes on a form. A user clicks a button and the
information from the texboxes is stored as an array.

The textbox1 which has a name is then inserted into a listbox.

The textboxes are then cleared for another name to be entered. When this
second name is entered the array needs to have a second row with the second
person added.

The seconds persons name also needs to be added to the listbox.

How do I do this please?

Additem does not work with arrays, and list, just adds the current line?

So far I have the following, but as I addrows the lsitbox only displays the
most recent addition and blanks the previous rows.

Private Sub CommandButtonAddOffender_Click()
Dim OffenderArray As Variant
ListBox2.ColumnCount = 10
ListBox2.ColumnWidths = "100;0;0;0;0;0;0;0;0;0"
i = ListBox2.ListCount
ReDim OffenderArray(i, 10)
OffenderArray(i, 0) = TextBox1.Value & " " & TextBox2.Value
OffenderArray(i, 1) = TextBox1.Value
OffenderArray(i, 2) = TextBox2.Value
OffenderArray(i, 3) = TextBox3.Value
OffenderArray(i, 4) = TextBox4.Value
OffenderArray(i, 5) = TextBox5.Value
OffenderArray(i, 6) = TextBox6.Value
OffenderArray(i, 7) = TextBox7.Value
OffenderArray(i, 8) = TextBox8.Value
OffenderArray(i, 9) = TextBox9.Value

ListBox2.List() = OffenderArray
Call ClearBoxes

End Sub


Help!

Thanks

-Al
 
A

Al & Cay Grant

Even with preserve added it still dont work :-(

Public Sub CommandButtonAddOffender_Click()
Dim OffenderArray() As String
Dim OffenderArray1() As String

With ListBox2
.ColumnCount = 10
.ColumnWidths = "100;20;20;20;0;0;0;0;0;0"
.BoundColumn = 2
.TextColumn = 2
End With
a = ListBox2.ListCount
ReDim Preserve OffenderArray(a, 9)

OffenderArray(a, 0) = TextBox1.Value
OffenderArray(a, 1) = TextBox2.Value
OffenderArray(a, 2) = TextBox3.Value
OffenderArray(a, 3) = TextBox4.Value
OffenderArray(a, 4) = TextBox5.Value
OffenderArray(a, 5) = TextBox6.Value
OffenderArray(a, 6) = TextBox7.Value
OffenderArray(a, 7) = TextBox8.Value
OffenderArray(a, 8) = TextBox9.Value

ListBox2.List() = OffenderArray
Call ClearBoxes

End Sub


Heeeeelp

-AL
 
J

Jonathan West

Hi al,

ReDim Preserve only preserves the contents of the array if you are only
changing the size of the *last* dimension of the array.

So you need to transpose the rows & columns, and you need to assign the
array to the Column property of the ListBox instead of the List property, so
that the array is transposed back again for display in the list.

Something like this.

Public Sub CommandButtonAddOffender_Click()
Dim OffenderArray() As String
Dim OffenderArray1() As String

With ListBox2
.ColumnCount = 10
.ColumnWidths = "100;20;20;20;0;0;0;0;0;0"
.BoundColumn = 2
.TextColumn = 2
End With
a = ListBox2.ListCount
ReDim Preserve OffenderArray(9, a)

OffenderArray(0, a) = TextBox1.Value
OffenderArray(1, a) = TextBox2.Value
OffenderArray(2, a) = TextBox3.Value
OffenderArray(3, a) = TextBox4.Value
OffenderArray(4, a) = TextBox5.Value
OffenderArray(5, a) = TextBox6.Value
OffenderArray(6, a) = TextBox7.Value
OffenderArray(7, a) = TextBox8.Value
OffenderArray(8, a) = TextBox9.Value

ListBox2.Column() = OffenderArray
Call ClearBoxes

End Sub
 
J

Jonathan West

By the way, in addition, you will also need to move the Dim statement out of
the routine into the Declaratiobns section of the form, so that it reads as
follows

Private OffenderArray() As String


--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top