Does anyone know how to write to a text file using VB only?
I need to query an access database. Then gather the queried information and then populate that information into the text file.
Can someone help a new comer to VB coding.
Thanks
Rey
Printable View
Does anyone know how to write to a text file using VB only?
I need to query an access database. Then gather the queried information and then populate that information into the text file.
Can someone help a new comer to VB coding.
Thanks
Rey
Like this:
Open (C:\MyFile.txt) for output as #1'doesnt have to exist
For intCount = 1 to intNumberofRecords
Write #1, strData1(intCount),strData2(intCount)'as many different fields as there are
Next intCount
Close #1
If that isn't what you meant let me know
Code:Dim myDB as Database
Dim myRS as Recordset
Dim FileNum as Integer
FileNum = FreeFile
Set myDB = DBEngine.Workspaces(0).OpenDatabase("DB.mdb")
Set myRS = myDB.OpenRecordset("DaRecordset")
Open "DB.txt" for Output as #FileNum
Do Until myRS.EOF
Print #FileNum, myRS!Feild1; "," ; myRS!Field2 ;vbCrLf 'and so on(End with vbCrLf or vbNewLine)
myRs.MoveNext
Loop
Close FileNum
Set myRS= Nothing
Set myDB = Nothing
I am confused by the #1? What does that stand for? Also, where would I place my query into the database.Quote:
Originally posted by wengang
Like this:
Open (C:\MyFile.txt) for output as #1'doesnt have to exist
For intCount = 1 to intNumberofRecords
Write #1, strData1(intCount),strData2(intCount)'as many different fields as there are
Next intCount
Close #1
If that isn't what you meant let me know
Example. I need to find all the last names in the Peoples database.
Select lastname from Peoples
Is my query going to be placed within the For Next?
Also, do I have to place all the information returned from the query into some placeholder, a recordset or where does the information reside until I want to place into the text file?
Let me know if this confused you.
Thanks
Rey
In my code:
put your SQL statement in openRecordset:
the 1 is a filenumber, a refrance for what file to open. Use the FreeFile function to get a freeone. 1 is almost always free, unless multiple files are open at same time. Just to be sure, use FreeFile.Code:set myRS = myDB.OpenRecordset("SELECT lastname FROM Peoples")
SQL will return a "Recordset" based on your Query into the myRS object. Use the methods in my code to loop through.
Hope this helps
Okay. I guess you better go with GWDash's code, since you are working with a database. For the general purpose of writing to a text file in VB, use the code I gave you.
The #1 (where the # is optional these days) represents that it is the first file you have open. Whenever you use an OPEN statement, VB holds that file open until it receives a CLOSE statement. Therefore if you wanted to open several files, you would use #1,#2,#3.... to reference them. So when you say Write#1, that tells VB to write to the file that you designated as #1.
Example
Open "C:\text1.txt" for output as #1
Open "C:\text2.txt" for output as #2
Write #1, "This is the first text file."
Write #2,"This is the first text file."
Close #1
Close #2
Then you should have 2 new files on your c: with the corresponding texts.
Sorry for the confusion.