|
-
May 26th, 2005, 02:20 PM
#1
Thread Starter
Addicted Member
Reading form text file (RESOLVED)
Hey all,
I am having trouble trying to read/write to/from a text file to/from multiple
text boxes.
What I would like to do is write each texbox to a seperate line in a text file
like this...
Text1 - line #1
Text2 - line #2
Text3 - line #3
and so on...
and then I would like to read each line into a different textbox, like this...
line #1 - Text1
line #2 - Text2
line #3 - Text3
Any help would be appreciated,
Ron
Last edited by rdcody; May 27th, 2005 at 04:58 PM.
-
May 26th, 2005, 02:27 PM
#2
Re: Reading form text file
To read try something like this:
VB Code:
Dim intFree As Integer
Dim strReadText As String
Dim strTextLines() As String
intFree = FreeFile
Open "C:\SomeFile.txt" For Binary As #intFree
strReadText = Space$(LOF(intFree))
Get #intFree, , strReadText
Close #intFree
' Create array of text lines from the file...
strTextLines = Split(strReadText, vbNewLine)
Text1.Text = strTextLines(0) ' First line...
Text2.Text = strTextLines(1) ' Second line...
And to write try the follwoing...
VB Code:
Dim strMyText As String
strMyText = Text1.text & vbNewLine & Text2.Text & vbNewLine & Text3.Text ' Etc.
Dim intFree As Integer
intFree = FreeFile
Open fileName For Binary Access Write As #intFree
Put #intFree, , strMyText
Close #intFree
Cheers,
RyanJ
-
May 26th, 2005, 03:02 PM
#3
Thread Starter
Addicted Member
Re: Reading form text file
Hey sciguyryan,
Thanks for the quick reply.
On this line...
VB Code:
strTextLines = Split(strReadText, vbNewLine)
I get an "Can't assign to array" error. Any Ideas? By the way I'm using VB5.
Thanks,
Ron
-
May 26th, 2005, 03:25 PM
#4
Re: Reading form text file
 Originally Posted by rdcody
Hey sciguyryan,
Thanks for the quick reply.
On this line...
VB Code:
strTextLines = Split(strReadText, vbNewLine)
I get an "Can't assign to array" error. Any Ideas? By the way I'm using VB5.
Thanks,
Ron
Hmm, I am afraid I do not use VB5 and so, am unable to test that....
I'll see if I can check if it can be done that way in VB5 
Cheers,
RyanJ
-
May 26th, 2005, 03:39 PM
#5
Re: Reading form text file
check the codebank, posted by manavo like 5 days ago
-
May 26th, 2005, 03:42 PM
#6
Re: Reading form text file
 Originally Posted by |2eM!x
check the codebank, posted by manavo like 5 days ago
ARGH! Damn my memory... Its not my fault - I blame the exams 
Thanks for that - I wound have been here for hours looking otherwise 
Cheers,
RyanJ
-
May 26th, 2005, 04:02 PM
#7
Thread Starter
Addicted Member
Re: Reading form text file
Well...I'll be damned if I can find it...
-
May 26th, 2005, 04:11 PM
#8
Thread Starter
Addicted Member
Re: Reading form text file
Are you guys talking about the split function by manavo11? I have that,
but I still get the same error. Any ideas?
-
May 26th, 2005, 07:07 PM
#9
Member
Re: Reading form text file
Hi RD Cody! I had that kind of program as well.. why don't you give this a try.. These codes saves the text being inputted by the user in the textbox in a file (which the file extension can btw be specified by you) and the user can retrieve the file and show the same text and order back to the form where your textboxes are... Quite a long explanation eyh.. You can tweek it to suit your needs if you want.
I made a class, which I named namelist.
This class has properties namely:
name
symbol
quantity
My example.plp output would look like this when opened in a text editor(like notepad):
[Name] x32
[Quantity] x75
[Symbol] q21
(globalvar.bas)
VB Code:
Option Explicit
Public pl1 As New namelist
(frm_field) 'This is the form where the textboxes are 
VB Code:
Option Explicit
Private Sub fieldupload()
With pl1
.symbol = txt_sym.Text
.name = txt_name.Text
.quantity = txt_qty.Text
End With
End Sub
Private Sub cmd_apply_Click()
If Len(txt_sym.Text) = 0 Or _
Len(txt_name.Text) = 0 Or _
Len(txt_qty.Text) = 0 Or _
MsgBox "please enter the necessary values."
txt_sym.SetFocus
Else
fieldupload
MsgBox ("Creating profile... Please choose what directory to save your profile.")
frm_dir.Caption = "Save Profile"
frm_dir.Show vbModal
End If
End Sub
Function GetString(tmpStr As String, tmpDiv As String, Mode As String) As String
Dim tmpRetStr As String
Dim tmpLen As Integer
Dim tmpErr As Integer
' *****************************************************
' tmpString = The whole string
' tmpDiv = The Divider chr
' if mode = "F" then get the string in front of the div
'
' Return values:
' Errors
' Err1 = wrong mode
' Err2 = No Div, did not found a divider
' Err5 = No String
' Err6 = No C, did not find any string in the middle
' Normal
' String
' ******************************************************
' Example: GetString("Red=Green","=","F") will retrun "Red"
' *** for string
tmpErr = 0
If Len(tmpStr) = 0 Then
tmpErr = 5
GoTo GetStringErr
End If
' *** test for chr in tmpDiv
If Len(tmpDiv) = 0 Then
tmpErr = 2
GoTo GetStringErr
End If
' *** test for div in string
If InStr(1, tmpStr, tmpDiv) = 0 Then
tmpErr = 2
GoTo GetStringErr
End If
If Mode = "C" Then
tmpRetStr = Mid(tmpStr, 9)
If Len(tmpRetStr) > 0 Then
GetString = tmpRetStr
Exit Function
Else
tmpErr = 6
'GoTo GetStringErr
MsgBox ("Error: Non-standard format in saving profile name. No hyphen '-' found.")
frm_dir.txt_plprofile.Text = "*.plp"
End If
MsgBox ("Error: Non-standard format in saving profile name. No hyphen '-' found.")
frm_dir.txt_plprofile.Text = "*.plp"
End If
tmpErr = 1
frm_dir.txt_plprofile.Text = "*.plp"
'GoTo GetStringErr
Exit Function
GetStringErr:
GetString = "Err" & tmpErr
End Function
Private Sub loadfield()
txt_sym.Text = pl1.symbol
txt_name.Text = pl1.name
txt_qty.Text = pl1.quantity
End Sub
Private Sub cmd_back_Click()
Form_Load
Me.Hide
frm_loadfile.Show vbModal
End Sub
Private Sub cmd_next_Click()
Me.Hide
frm_summary.Show vbModal ' another form which basically are just labels that shows a summmay of the information
End Sub
Private Sub cmd_retrieve_Click()
frm_dir.file1.Enabled = True
loadfield
frm_dir.Caption = "Load Profile"
frm_dir.Show vbModal
End Sub
Private Sub Form_Load()
On Error GoTo Form_Load_Error
'This code sets up the Text Boxes
cmd_next.Enabled = False
Me.txt_name.Text = ""
Me.txt_qty.Text = ""
Me.txt_sym.Text = ""
On Error GoTo 0
Exit Sub
Form_Load_Error:
MsgBox "Error " & Err.Number & " (" & Err.description & ") in procedure Form_Load of Form " & Me.Name
End Sub
Private Sub Validate_Entry(ByRef txtBox As TextBox)
If Left$(txtBox.Text, 1) <> vbNullString Then
If UCase(txtBox.Text) Like "[A-Z]" & String(Len(txtBox.Text) - 1, "#") And Len(txtBox.Text) < 6 Then
txtBox.Text = UCase(txtBox.Text)
txtBox.SelStart = Len(txtBox.Text)
Else
MsgBox "Please try again.", vbOKOnly + vbInformation, "Invalid Entry"
txtBox.Text = ""
End If
End If
End Sub
Private Sub txt_qty_Change()
Call Validate_Entry(txt_qty)
End Sub
Private Sub txt_sym_Change()
Call Validate_Entry(txt_sym)
End Sub
Private Sub txt_name_Change()
Call Validate_Entry(txt_name)
End Sub
now to save the text inputted in the textbox in a file....use this
(frm_dir)
VB Code:
Option Explicit
Public profilepath As String
Public finalprofile As String
Private Sub Form_Load()
Dim prof As String
txt_profilepath.Enabled = False
file1.Enabled = False
txt_plprofile.Text = frm_loadfile.var1
prof = frm_field.GetString(txt_plprofile.Text, "-", "C")
txt_plprofile.Text = prof + ".plp"
'txt_plprofile.Text = prof + ".plp"
End Sub
Private Sub cmd_cancel_Click()
MsgBox "Cancelled"
Unload Me
End Sub
Private Sub cmd_ok_Click()
Dim prof As String
Select Case Me.Caption
Case "Save Profile"
txt_profilepath.Text = file1.Path & "\" & file1.FileName & txt_plprofile.Text
If isFile(txt_profilepath.Text) Then
txt_plprofile.Text = frm_loadfile.var1
prof = frm_field.GetString(txt_plprofile.Text, "-", "C")
If MsgBox("File " & txt_profilepath & " exists. Do you want to overwrite the current profile?{Y/N]", vbQuestion + vbYesNo, App.ProductName + " Prompt") = vbYes Then
SaveProfile txt_profilepath.Text, pl1
finalprofile = txt_plprofile.Text
Else
txt_profilepath.Text = file1.Path & file1.FileName & "\" & prof & Format(Now, "mmddyyyyhhmmss") & ".plp"
SaveProfile txt_profilepath.Text, pl1
finalprofile = txt_plprofile.Text
End If
Else
MsgBox "Saving " & txt_profilepath & " as new profile."
SaveProfile txt_profilepath.Text, pl1
finalprofile = txt_plprofile.Text
End If
profilepath = txt_profilepath.Text
frm_field.cmd_next.Enabled = True
Case "Load Profile"
txt_profilepath.Text = file1.Path & "\" & file1.FileName
MsgBox "Loading " & txt_profilepath & " profile."
LoadProfile txt_profilepath.Text, pl1
profilepath = txt_profilepath.Text
frm_field.cmd_next.Enabled = True
End Select
Me.Hide
End Sub
Private Sub LoadProfile(WhatFile As String, TheProfile As partslist)
Dim FreeFileHandle As Integer, a$, B() As String
FreeFileHandle = FreeFile
Open WhatFile For Input As FreeFileHandle
With TheProfile
Line Input #FreeFileHandle, a ' "[Symbol] " & .symbol
B = Split(a, " ")
.symbol = B(1)
frm_field.txt_sym.Text = .symbolic
Line Input #FreeFileHandle, a ' "[Name] " & .Name
B = Split(a, " ")
.name = B(1)
frm_field.txt_name.Text = .name
Line Input #FreeFileHandle, a ' "[Quantity] " & .quantity
B = Split(a, " ")
.quantity = B(1)
frm_field.txt_qty.Text = .quantity
End With
Close FreeFileHandle
End Sub
Private Sub SaveProfile(WhatFile As String, TheProfile As namelist)
Dim FreeFileHandle As Integer
FreeFileHandle = FreeFile
Open WhatFile For Output As FreeFileHandle
With TheProfile
Print #FreeFileHandle, "[Name] " & .name
Print #FreeFileHandle, "[Quantity] " & .quantity
Print #FreeFileHandle, "[Symbolic] " & .symbol
End With
Close FreeFileHandle
End Sub
Private Sub dir1_Change()
file1.Path = dir1.Path
End Sub
Private Sub drive1_Change()
dir1.Path = drive1.Drive
End Sub
Private Sub file1_PathChange()
Me.Caption = "Dialog Box - [" & file1.Path & "]"
End Sub
Private Sub file1_Click()
txt_plprofile.Text = file1.FileName 'file1 is a file list box
End Sub
Function isFile(ByVal sFileName As String) As Integer
On Error Resume Next
Dim lFileLength As Long
Const ATTR_NORMAL = 0
isFile = False
If Dir$(sFileName, ATTR_NORMAL) <> "" Then
lFileLength = FileLen(sFileName)
If (lFileLength > 0) Then isFile = True
End If
End Function
I hope this helps..
Yoroshiku,
 seraphicmortal
______________________________________________________
Thirst for knowledge can never be quenched...Ask for more!!.
Oh! Please..Show your appreciation by clicking  if you deem this post helpful.
Rate this Post! 
-
May 26th, 2005, 08:19 PM
#10
Member
Re: Reading form text file
The codes above btw came not only from me but with the help of our buds from vbforum.. (Baja and Joacim) 
You can go here to view my thread...
http://www.vbforums.com/showthread.php?t=339887
Yoroshiku,
 seraphicmortal
______________________________________________________
Thirst for knowledge can never be quenched...Ask for more!!.
Oh! Please..Show your appreciation by clicking  if you deem this post helpful.
Rate this Post! 
-
May 26th, 2005, 11:25 PM
#11
Thread Starter
Addicted Member
Re: Reading form text file
Thanks for the reply, but I'm still having trouble with the Split function. I'm
going to have to find a different way of doing this.
-
May 26th, 2005, 11:38 PM
#12
Member
Re: Reading form text file
mind expounding why you're having trouble using Split function?
Yoroshiku,
 seraphicmortal
______________________________________________________
Thirst for knowledge can never be quenched...Ask for more!!.
Oh! Please..Show your appreciation by clicking  if you deem this post helpful.
Rate this Post! 
-
May 27th, 2005, 08:07 AM
#13
Thread Starter
Addicted Member
Re: Reading form text file
I use VB5, and the Split function that manavo11 supplied just doesn't seem
to work.
-
May 27th, 2005, 12:49 PM
#14
Re: Reading form text file
Try:
VB Code:
Private Function SplitEx(ByVal sString As String, Optional ByVal sDelim As String = " ") As Variant
Dim sItem As String
Dim sList() As String
Dim lChar As Long
Dim lCount As Long
Do
' Reset Split for non-Quoted Values
lChar = InStr(sString, sDelim)
If lChar > 0 Then
' Extract the Item from the String
sItem = Left(sString, lChar - 1)
' Remove Item from the Remaining String
sString = Mid(sString, lChar + Len(sDelim))
Else
' Remaining String, becomes final Item
sItem = sString
sString = ""
End If
' If something was extracted, add it to the Array
If Len(sItem) > 0 Then
ReDim Preserve sList(lCount)
sList(lCount) = sItem
lCount = lCount + 1
End If
' Continue until no more delimiters are found.
Loop While lChar > 0
' If there were items, return the array
If lCount > 0 Then
SplitEx = sList
Else
' Otherwise return an empty array
SplitEx = Array()
End If
End Function
Private Sub Command1_Click()
Dim intFree As Integer
Dim strReadText As String
Dim strTextLines() As String
intFree = FreeFile
Open "C:\SomeFile.txt" For Binary As #intFree
strReadText = Space$(LOF(intFree))
Get #intFree, , strReadText
Close #intFree
' Create array of text lines from the file...
strTextLines = SplitEx(strReadText, vbNewLine)
Text1.Text = strTextLines(0) ' First line...
Text2.Text = strTextLines(1) ' Second line...
End Sub
Regards,
- Aaron.
-
May 27th, 2005, 01:12 PM
#15
Thread Starter
Addicted Member
Re: Reading form text file
Hey Aaron,
I'm still getting the same error...
VB Code:
[B]strTextLines =[/B] SplitEx(strReadText, vbNewLine)
Compile error: "Can't assign to array"
I don't know...I'm about to give up.
-
May 27th, 2005, 02:45 PM
#16
Hyperactive Member
Re: Reading form text file
I run VB6 at work and had that same problem the other day. The reason you get that array error when using the split function is because split turns the variable into an array using a delimiter to make distinctions between elements. An example is:
dim fso as FileSystemObject
dim myTextStream as TextStream
dim myReadText() as String
set fso = New FileSystemObject
set myTextStream = fso.OpenTextFile("C:\File.txt")
myReadText = Split(myTextStream.ReadLine, "|") 'The "pipe" symbol is the divider of the text
MsgBox (myReadText(0))
MsgBox (myReadText(1))
MsgBox (myReadText(2))
This is just an example. The split function turns the variable into an array. So make sure you dim that variable accordingly.
I hope this helps.
-
May 27th, 2005, 03:03 PM
#17
Thread Starter
Addicted Member
Re: Reading form text file
Well...I don't know...this is above my head. Isn't everything declared properly?
-
May 27th, 2005, 03:19 PM
#18
Hyperactive Member
Re: Reading form text file
Try defining a delimiter like I did, instead of vbNewLine. It may be that VB 5 doesn't have that command, I think that is a .NET upgrade.
-
May 27th, 2005, 03:32 PM
#19
Re: Reading form text file
Try changing "strTextLines" to a Variant data type instead of a String array, i.e.
VB Code:
Private Sub Command1_Click()
Dim intFree As Integer
Dim strReadText As String
'Dim strTextLines() As String
Dim strTextLines As Variant
intFree = FreeFile
Open "C:\SomeFile.txt" For Binary As #intFree
strReadText = Space$(LOF(intFree))
Get #intFree, , strReadText
Close #intFree
' Create array of text lines from the file...
strTextLines = SplitEx(strReadText, vbNewLine)
Text1.Text = strTextLines(0) ' First line...
Text2.Text = strTextLines(1) ' Second line...
End Sub
Regards,
- Aaron.
-
May 27th, 2005, 03:40 PM
#20
Fanatic Member
Re: Reading form text file
I'm kinda new and don't really know too much but If you want to avoid the split, you can simply open the file a different way.
VB Code:
dim strArray(0 to NUMOFTXTBOXES -1) as string
dim i as long
dim FileHandle%
dim strFileName as string
FileHandle% = FreeFile
i = 0
Open strFileName For Input As #FileHandle%
Do While Not EOF(FileHandle%)
Line Input #FileHandle%, strArray(i)
i = i+1
Loop
textbox0 = strArray(0)
textbox1 = strArray(1)
...
textboxN = strArray(N)
Close FileHandle%
HTH
Last edited by space_monkey; May 27th, 2005 at 03:47 PM.
Reason: Cause I'm a little slow late in the afternoon especially on a friday
Using VB6 or VB.net 2008 with .net 3.5
"Life... death... either way I'll be confined to a small cubicle!" - Hermes Conrad
-
May 27th, 2005, 04:56 PM
#21
Thread Starter
Addicted Member
Re: Reading form text file
OK...here are the results...
To tacoman667...I tried that, and it didn't make any difference.
To Aaron...That worked...except if you enter a blank line in the file, then I
get a "subscript out of range" error. In other words, there are 6 lines of
text (2 blank), and when it puts them into the 6 textboxes, it uses the first
four, and then I get the error.
To space_monkey...it worked perfectly!
I want to thank all you guys for helping to figure this out,
Ron
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|