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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating, Reading, and Writing to a Text File in VB 1

Status
Not open for further replies.

dataman86

Technical User
Oct 11, 2008
104
US
I am a technical user not a programmer, but would like some help in creating, reading, and writing to a text file. does anyone have any sample coding out there using VB to do this. I am learning and know a little, but not very much.

Thanks

DataMan86
 

Option explicit
Type TmpItem
Tno As Long
Lno As Long
Pno As Long
Tdate As Integer
Ref As Long
Detail As String * 15
Pfrom As Integer
Pto As Integer
Amount As Integer
End Type
Global THI As TmpItem




DIM THIMax As Long
DIM THICh As Integer
DIM THIFile As String
Dim THILen As Long



Public Sub OpenTmpFile()
THILen = Len(THI)
THICh = FreeFile
Open THIFile For Random As THICh Len = THILen
THIMax = LOF(THICh)
THIMax = THIMax \ THILen
End Sub


SUB DOIT
dim RecNo as Integer
THIFile = App.path & "ABC.DAT"
OpenTmpFile
Recno = 1
thi.lno = 1
thi.pno = 2
thi.tno = 3
.
.
.
Put #THICh, RecNo , Thi
Recno = 2
thi.lno = 4
thi.pno = 5
thi.tno = 6
.
.
.
Put #THICh, RecNo , Thi
Close #ThiCh

OpenTmpFile
RecNo = 2
Get #THICh, RecNo , Thi
dbug.Print Thi.PNO

Close #ThiCh
 
And that's not exactly reading or writing from a text file ...

DataMan6, as a technical user, are you sure you want to work with VB6? Or are you actually working with VBA (i.e. the Visual Basic that appears in the Office Applications, and which is a slightly different animal)?

 
Peter Wallace and Strongm, I appreciate your help greatly

I am trying to learn Visual Studio 2008 and use the VB coding and not C# Coding. I would like to learn C#, but will just try to learn VB at the moment. VB6 and VB5 as I understand it, is the old VB languages before Visual Studio came out and was used mostly with Visual Basic.Net in 2003 and back. Visual Basic Access, I am trying to learn too so I can use with Microsoft Access. However, I want to be able to create a text file, read it and write it out using the Stream Reader and Stream Writer or Text Reader and TExt Writer. Actually I would like to read and write with both StreamReader/TextReader/and StreamWriter/TextWriter.

I would like sample coding representing StreamREader/Writer and TextReader/Writer classes as I udnerstand it.

I hope I have explained myself well. I am learning and I have picked up some of the jargon but I am about 15%. So I got about 85% to learn.
 
Visual Basic 5 was the language and tool that came out with Visual Studio 97. VB6 is matched to Visual Studio 6.0 (98).

Microsoft abandoned Visual Basic after that, and produced a new and incompatible language often labeled Visual Basic but quite different in most regards. Versions of this were included in Visual Studio .Net (2002) and later editions.


The products labeled VB.Net or now just VB are covered in another forum.
 
Also...

VBA (Visual Basic for Applications) is similar to VB6 but is always hosted in another product (Excel, Access, 3rd party applications). There is another forum for VBA as well.

While I'm at it I'll mention VBScript, which is yet another entirely different thing and covered in its own forum.
 
DataMan,

Here is the sample code for creating,reading and writing to text file in vb6.

Regards,
Beautieee
 
So sorry, code attached bellow :-

' ******************************
' ******************************
' ** MASTERING VB6 **
' ** by Evangelos Petroutos **
' ** SYBEX, 1998 **
' ******************************
' ******************************
Dim FSys As New FileSystemObject

Private Sub bttnCreateFile_Click()
Dim OutStream As TextStream

TestFile = App.Path & "\textfile.txt"
Set OutStream = FSys.CreateTextFile(TestFile, True, False)
OutStream.WriteLine Text1.Text
Set OutStream = Nothing
End Sub

Private Sub bttnReadFile_Click()
Dim InStream As TextStream

TestFile = App.Path & "\textfile.txt"
Set InStream = FSys.OpenTextFile(TestFile, 1, False, False)
While InStream.AtEndOfStream = False
TLine = InStream.ReadLine
txt = txt & TLine & vbCrLf
Wend
Text1.Text = "The following text was read from the file" & vbCrLf
Text1.Text = Text1.Text & vbCrLf & String(50, "*")
Text1.Text = Text1.Text & vbCrLf & txt
Text1.Text = Text1.Text & vbCrLf & String(50, "*")
Set InStream = Nothing
End Sub

 
Not to be hypercritical, but that's about the worst example of VB6 code I've seen in a while.

No Option Explicit and mostly missing data declarations, long-deprecated While...Wend, redundant use of intermediate variables, about twice as much String concatentation as required, use of the Variant form of the String() function (use String$() instead), use of the FSO instead of native VB I/O, odd atypical Hungarian prefixes, line-by-line file I/O when you're bringing the whole file into memory anyway...

Glad I don't have that book in my library.


You could do far worse than simply reading the VB6 documentation.
 
Not to be hypercritical, but that's about the worst example of VB6 code I've seen in a while.

No Option Explicit and mostly missing data declarations, deprecated While...Wend, redundant use of intermediate variables, about twice as much String concatentation as required, use of the Variant form of the String() function (use String$() instead), use of the FSO instead of native VB I/O, odd atypical Hungarian prefixes, line-by-line file I/O when you're bringing the whole file into memory anyway...

Glad I don't have that book in my library.


You could do far worse than simply reading the VB6 documentation.
 
*Sigh*

Double-post due to a Tek-Tips web site error. Gee, as if I didn't feel bad enough already about that post!
 
Not to mention that DIMming an object as New is bad practice ...

>use of the FSO instead of native VB I/O

This is about the only point I'd vaguely disagree with you on, as I don't really see any problem in using FSO for reading and writing simple text files.




 
You could also use the Open Statement to open a text file for input/output.

Open pathname For mode [Access access] [lock] As [#]filenumber [Len=reclength]

 
That's what dilettante was getting at when mentioning the native VB I/O
 
So, given the level of expertise of the OP, I wouldn't have expected them to know that, and now they do and won't waste time investigating what they might have thought was an alternative solution.
 
How did u judge thats the worst example?? As long as it leads to desire result better than nuts.

I came across these code when searching for others and wish to share as REFERENCE.

Dilettante, feel free to add those code if you really need them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top