0

I need to enter values in text box and when ever i press enter i have to save the text box values into an array.How can i achieve this functionality?

1
  • Is this a multi-line edit box, and you want to add one value per line? Commented Jul 23, 2012 at 14:33

4 Answers 4

1

I would use a separate counter variable to redefine the size of the array, like this:

Option Explicit

Dim myArr() As String  '~~~ dynamic array
Dim lngCnt As Long     '~~~ a counter variable that keep track of the index

' inital stage..
Private Sub Form_Load()
    lngCnt = 0
End Sub

' on KeyPress
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then   '~~~ if Enter Key is pressed..
        ReDim Preserve myArr(lngCnt)    '~~~ reclare the array with the new size, while preserving any elements it may contain
        myArr(lngCnt) = Text1.Text      '~~~ store the line
        Text1.Text = ""     '~~~ empty the textbox, so that you could type the next line

        lngCnt = lngCnt + 1 '~~~ increment the counter, which we would use as size during the next keypress
    End If
End Sub

' to display the elements
Private Sub Command1_Click()
    Dim i As Long

    For i = LBound(myArr) To UBound(myArr)  '~~~ loop through the elements(from Lowerbound to Upperbound)..
        Debug.Print myArr(i)                '~~~ ..and display the item.
    Next
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

ThanK You...Akhilesh B Chandran
1

If you just want a new entry to be added each time they press the button, then use something like:

Redim Preserve YourArray(LBound(YourArray) To UBound(YourArray) + 1)
YourArray(UBound(YourArray)) = TextBox.Text

Note that this can get very slow and inefficient when the array contains large numbers of items as it's reallocating memory each time. A better method would involve expanding the size of the array in chunks, while keeping track of the last valid entry.

Comments

0

Just give same name to all the text Boxes

1 Comment

I need to use only one text box
0

The short answer is to use split to break the string up (you will need to tell the user what char to use to split on).

Long answer don't change the user interface to a repeater and have them use an input per value start here http://blogs.microsoft.co.il/blogs/basil/archive/2008/08/20/javascript-repeater-control-datarepeater-using-jquery-presenter-1-0-8-uicontrols-library.aspx

Else you will be debugging for ever.

1 Comment

This question has been tagged VB6 - your answer seems to be for ASP.NET.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.