Help populatin an Dynamic array with a TextBox

A

aymarti

Hello All,

I'm a very beginner programmer and I am having problem with my form. I
'm trying to set up a form where the user decides how many elements will be
in the dynamic array using an input box during the form load event. (i.e, How
many name will you like to add? user enter 2 the form redims the array to two
elements) a form then opens and allows the user to enter names into a text
box.
These text boxes will populate the array and if the user enters more than the
number of names stated int the Input box, there is a msgbox stating the array
is full! Also if the user wants to see the name they have entered into the
array they can click a button on the form which will provide a msgbox with
the names they have entered.

Here is the code that I used for the Load Form
Private Sub Form_Load()

Dim dblNumberofNames As Double

Do While dblNumberofNames = 0

dblNumberofNames = Val(InputBox("How many names?", "Name Count"))

If dblNumberofNames = 0 Then

MsgBox "Please enter a number greater than 0.", vbCritical, "Correct Value
Required"

Else
ReDim sNameArrays1(dblNumberofNames)
End If
Loop
End Sub

This is the code for the Add Name event
Private Sub cmdAddName_Click()

Dim intCount As Integer
Dim intI As Integer

If IsNull(txtFname.Value) Or IsNull(txtLname.Value) Then

MsgBox "There Must be values for both First and Last names", vbCritical,
"Data Entry Error"
Exit Sub
If (UBound(sNameArray1) + 1) > dblNumberofNames Then
MsgBox "Sorry, the array is full!"
End If

Else
For intI = 1 To UBound(sNameArray1)
intCount = intCount + 1
Next
ReDim Preserve sNameArray1(intI)

txtFname.Value = " "
txtLname.Value = " "
End If
End Sub
Any suggestion why this doesn't work????
Thanks
 
G

Guest

Hi aymarti... there does not seem to be a point where you actually add the
name to the array. You have a loop that does nothing
For intI = 1 To UBound(sNameArray1)
intCount = intCount + 1
Next
ReDim Preserve sNameArray1(intI)

As you've already redifined your array you don't need to do this line again.
Instead of the loop you need to assign the name to the array. You'll need to
have a counter that knows the next position to insert into the array.

You could possibly do this as follows...

'Declare before first procedure in general declarations
Private iArrayIndex As Integer

Private Sub Form_Load()
'Clear array
ReDim sNameArrays1(0)
'Reset array position index
iArrayIndex = 0
End Sub

'This is the code for the Add Name event
Private Sub cmdAddName_Click()

Do
'Redefine for new entry
ReDim Preserve sNameArray1(iArrayIndex)
'Store new entry
sNameArray1(iArrayIndex) = "new value"
'Increment index
iArrayIndex = iArrayIndex + 1
'Ask user whether to continue
Loop While MsgBox("Add another entry", vbQuestion + vbYesNo, "Add Name")
= vbYes
End Sub


Luck Jonathan
 
A

aymarti

Thanks so much for responding!
And for pointing out my do nothing loop, I will try your suggestions and let
you know what happens.

Thanks again
Hi aymarti... there does not seem to be a point where you actually add the
name to the array. You have a loop that does nothing
For intI = 1 To UBound(sNameArray1)
intCount = intCount + 1
Next
ReDim Preserve sNameArray1(intI)

As you've already redifined your array you don't need to do this line again.
Instead of the loop you need to assign the name to the array. You'll need to
have a counter that knows the next position to insert into the array.

You could possibly do this as follows...

'Declare before first procedure in general declarations
Private iArrayIndex As Integer

Private Sub Form_Load()
'Clear array
ReDim sNameArrays1(0)
'Reset array position index
iArrayIndex = 0
End Sub

'This is the code for the Add Name event
Private Sub cmdAddName_Click()

Do
'Redefine for new entry
ReDim Preserve sNameArray1(iArrayIndex)
'Store new entry
sNameArray1(iArrayIndex) = "new value"
'Increment index
iArrayIndex = iArrayIndex + 1
'Ask user whether to continue
Loop While MsgBox("Add another entry", vbQuestion + vbYesNo, "Add Name")
= vbYes
End Sub

Luck Jonathan
Hello All,
[quoted text clipped - 57 lines]
Any suggestion why this doesn't work????
Thanks
 

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