PDA

View Full Version : read textfile



av8tordude
08-12-2011, 04:31 PM
I've added a textbox (FName) on Userform1 and Userform2 for the user's name. When the user initializes the program via Userform1, Data1; vbTab; Data2; vbTab; Data3; vbTab; FName are entered into the textfile. When Userform2 is displayed, the code populates the labels and textbox from the information found in the textfile. I'm able to populate FName with my name found in the textfile, but names varies in lenth. How can I adjust this code to account for varying lengths in names?


flName = Split(f, vbTab)(3)
FName = Left$(flName, 15)

Rob342
08-13-2011, 01:40 AM
Try


FName = Left$(flName, Len(flName))


Rob

av8tordude
08-13-2011, 07:06 AM
I tried the code and it inputs a "Enter" symbol behind my name. The symbol looks like a shaded backwards P.

GTO
08-13-2011, 07:36 AM
If you wrote


Data1; vbTab; Data2; vbTab; Data3; vbTab; FName

to the textfile, then flName should return okay on its own.

av8tordude
08-13-2011, 08:05 AM
GTO, you are right. I discovered that when writing the info to the text file, the code creates a enter input in the text file (The cursor was not at the end of the line of text; like hitting the enter button to move to the next line). So my question now, when writing the info to the textfile, how do I prevent the code from creating an Enter input?

av8tordude
08-13-2011, 08:20 AM
I put a semi-colon at the end of the code. I'm not sure that the correct way of doing it, but it seems to work


Data1; vbTab; Data2; vbTab; Data3; vbTab; FName;

GTO
08-13-2011, 08:48 AM
GTO, you are right. I discovered that when writing the info to the text file, the code creates a enter input in the text file (The cursor was not at the end of the line of text; like hitting the enter button to move to the next line). So my question now, when writing the info to the textfile, how do I prevent the code from creating an Enter input?

I think you would need to show an example file so that we could see how it is actually constructing/readin the textfile. .xls Format if possible, as I do not have access to .xlsm until Monday.

Here was my admittedly (and early-bound) simplistic testing:

Userform1:


Option Explicit

Private Sub CommandButton1_Click()
Dim sData1 As Variant, sData2 As Variant, sData3 As Variant, sFName As String
Dim FSO As FileSystemObject
Dim fsoTStream As TextStream
Dim strLine As String
sData1 = txtData_1.Value
sData2 = txtData_2.Value
sData3 = txtData_3.Value
sFName = txtFName.Value
Set FSO = New FileSystemObject
Set fsoTStream = FSO.CreateTextFile(ThisWorkbook.Path & "\Test.txt", True)
fsoTStream.WriteLine sData1 & vbTab & sData2 & vbTab & sData3 & vbTab & sFName
fsoTStream.Close
Unload Me
UserForm2.Show
End Sub

UserForm2:


Private Sub UserForm_Initialize()
Dim FSO As FileSystemObject
Dim fsoTStream As TextStream
Set FSO = New FileSystemObject
Set fsoTStream = FSO.OpenTextFile(ThisWorkbook.Path & "\Test.txt", ForReading, False, TristateUseDefault)
TextBox1.Value = Split(fsoTStream.ReadLine, vbTab)(3)
End Sub