Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add text from txtbox into array

Status
Not open for further replies.

neoice

Programmer
Feb 15, 2003
110
0
0
GB
Hello,

I have written a simple spelling program for the special needs department at school. It displays a word on the screen for say 5 seconds then it disappears. They then have to type in the correct spelling. The teacher says the kids love it and asked if there is a way in which she could type in some more words.

I have stored the words in an array called "words" and then randomized the list:-

words(1)="despite"
words(2)="suggest"
words(50)="except"
etc..

I would be grateful if somebody could give me some ideas on how to take a word from a text box and append it to my current list. I imagine that I would probably have to store the words in a txt file rather than in the VB program and load the list into memory.

cheers,

neil
 
if you are using an array to store the words look into using

redim preserve yourarray()

for reading/saving words to a text file use the open statement

open "c:\yourwords" for input as #freefile

to read in the words, and

open "c:\yourwords" for append as #freefile

if you have any difficulties just post again and myself or another tektipper will provide code, or a link to code

info can be found in MSDN

good luck If somethings hard to do, its not worth doing - Homer Simpson
 
Thanks for the reply. It all seems a bit hazey. I am still very much a novice and would appreciate it if you could supply some code for me to study and digest.

cheers,
neil
 
Hello,

From what I gather you are going to initially load the information from a text file into memory then do your appendages to the array through typing the words into a text box then you want to rewrite them into the text file:

Lets start with the loading into memory...

'This code is in the form load event @@ also note that the
'file that has the information on it is called myFile.txt
'and is located on the local C drive
'also the the array is defined in the general declaration
'with a private statement and a size of 1 ...
'an example
' Private word(1) as String

'First we open the file for input
'input is needed to read from a file its contents
'OPEN 'LOCATION 'STATE 'INDEX
Dim strInput As String

Open C:/myFile.txt For Input As #1

'Now we will loop through the file and write to array

While NOT EOF(1) 'Where 1 is the INDEX
Input #1, strInput 'This reads the next
'Next value and stores
'it as strInput
words(UBound(words)) = strInput 'Fills higest array
ReDim Preserve words(UBound(words)+1)
Wend

'********************************************************
'Now we must make new appendages to the array

'Perhaps under some command button click event
'Resize the array
ReDim Preserve words(UBound(words)+1)

'and now append from a textbox named TextBox1
words(UBound(words)) = TextBox1.Text

'********************************************************
'Now we must Write to myFile.txt as Output because we dont
'to get redundant context

'This would be in perhaps another command_Button click event
Dim lB as Integer
Dim uB as Integer
Dim iCounter As Integer
Open C:/myFile.txt For Output As #1
lB = LBound(words)
uB = UBound(words)
For iCounter = lB To uB Step 1
Write #1, words(iCounter)
Next

'******************************************************

If you have anymore questions just repost.

Mike
 
hello Mike,

Thanks for taking the time to assist me, however I'm still struggling. When I "run" the code I get the following error message:-

"Array already dimensioned", this is the first time I have ever come across the command "redim" so I am lost, for want of a better word.

This is what I have written:-

'general declarations
Private words(1) As String

Private Sub Form_Load()

Dim strInput As String
Open "c:\myfile.txt" For Input As #1

While Not EOF(1)
Input #1, strInput

words(UBound(words)) = strInput

ReDim Preserve words(UBound(words) + 1)

Wend

End Sub


Private Sub cmdSave_Click()
Dim lB As Integer
Dim uB As Integer
Dim iCounter As Integer
Open "C:\myFile.txt" For Output As #1
lB = LBound(words)
uB = UBound(words)
For iCounter = lB To uB Step 1
Write #1, words(iCounter)
Next
End Sub

Private Sub cmdSubmit_Click()
ReDim Preserve words(UBound(words) + 1)
words(UBound(words)) = Text1.Text
End Sub

Again thank you for your help.

Neil


 
If you want to ReDim an array you have to declare with empty parenthese in the first place.

Instead of
Private words(1) As String

Try
Private words() As String
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thanks Johnwm,

That solved that issue but now I get a run time error 9, subscript out of range.

Private Sub cmdSubmit_Click()
ReDim Preserve words(UBound(words) + 1)
words(UBound(words)) = Text1.Text
End Sub

I guess I'll do some research on the ReDim command

Cheers,

Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top