Writing or Saving String to Text File

Cassie
02-06-2003, 03:23 AM
Hello! I'm having problem writing or saving string to a text file particularly into notepad. Here's my code:


DbaseString - variable for my string

Dim f, a
Dim inFileNumber As Integer
Dim strLine As String
Dim lngFileLength As Integer

CommonDialog1.CancelError = True
On Error GoTo ErrHandler
CommonDialog1.DialogTitle = "Select a file name"
CommonDialog1.Flags = cdlOFNHideReadOnly
CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files" & _
"(*.txt)|*.txt|Batch Files (*.bat)|*.bat"
CommonDialog1.FilterIndex = 2
CommonDialog1.ShowSave
MsgBox "Save to Path: " + CommonDialog1.FileName

intFileNumber = FreeFile
lngFileLength = FileLen(CommonDialog1.FileName)
Open CommonDialog1.FileName For Input As intFileNumber _
Len = lngFileLength
txtDocument.Text = Input$(lngFileLength, intFileNumber)
Close intFileNumber

Set f = fs.OpenTextFile(CommonDialog1.FileName, ForWriting, TristateFalse)
f.Write (DbaseString)
f.Close

Exit Sub

ErrHandler:
Exit Sub

---------

I'm not sure with the other codes. I just tried them and they work fine. I think what's lack in there is the saving or writing string into text file. Is this code,

f.Write (DbaseString)

suppose to write string to text file?

This codes is about locating the text file through commondialog box and then I want to save my string to that text file. Just overwrite any characters save in there if there is.

I will appreciate any of your help. Thank you in advance. :)

gallicus
02-06-2003, 05:22 AM
to write to a text file you must open it for output or append

Open "Text.txt" For Output As #1

then use

print #1, "string"

to write to the file. Not really sure what you are meant to be doing with

Set f = fs.OpenTextFile(CommonDialog1.FileName, ForWriting, TristateFalse)
f.Write (DbaseString)
f.Close

NateBrei
02-06-2003, 07:09 AM
A couple of things I see:
1) The last section of code looks like it is using FileSystemObjects (set reference to it from Tools, References, Microsoft Scripting Runtime -- scrrun.dll) to write to your textfile. Make sure you have your reference set and that you have 'fs' declared as a FileSystemObject and 'f' declared as a File object.

Dim fs As FileSystemObject
Dim f As File
'******
Set fs = New FileSystemObject
'******
2) In this section, you use DbaseString as the argument to the Write method, but in your code sample you never set the value of DbaseString. Therefore, it would/could be blank which would explain why it seems that nothing is being written to the text file.

Set f = fs.OpenTextFile(CommonDialog1.FileName, ForWriting, TristateFalse)
f.Write (DbaseString)
f.Close

3) At the top of your module, you should add the following statement before anything else in the module. It will require you to declare all variables before you can use them (otherwise you will get an undeclared variable error when you try to run). This will help you figure out what is happening with your code. I highly encourage this...

Option Explicit
Hope these suggestions help.

Nate

Cassie
02-06-2003, 08:23 PM
Originally posted by gallicus
to write to a text file you must open it for output or append
Open "Text.txt" For Output As #1

then use

print #1, "string"

to write to the file. Not really sure what you are meant to be doing with

Set f = fs.OpenTextFile(CommonDialog1.FileName, ForWriting, TristateFalse)
f.Write (DbaseString)
f.Close

Thanks gallicus! The open and print solved the problem. :)

Cassie
02-06-2003, 08:27 PM
Originally posted by NateBrei
A couple of things I see:
1) The last section of code looks like it is using FileSystemObjects (set reference to it from Tools, References, Microsoft Scripting Runtime -- scrrun.dll) to write to your textfile. Make sure you have your reference set and that you have 'fs' declared as a FileSystemObject and 'f' declared as a File object.

Dim fs As FileSystemObject
Dim f As File
'******
Set fs = New FileSystemObject
'******
2) In this section, you use DbaseString as the argument to the Write method, but in your code sample you never set the value of DbaseString. Therefore, it would/could be blank which would explain why it seems that nothing is being written to the text file.

Set f = fs.OpenTextFile(CommonDialog1.FileName, ForWriting, TristateFalse)
f.Write (DbaseString)
f.Close

3) At the top of your module, you should add the following statement before anything else in the module. It will require you to declare all variables before you can use them (otherwise you will get an undeclared variable error when you try to run). This will help you figure out what is happening with your code. I highly encourage this...

Option Explicit
Hope these suggestions help.

Nate

Thanks Nate! I forgot to add the "option explicit" statement and declare my variable. :)

As for those FileSystemObject, thank you also for was I able to understand its function.

---

This problem is solved.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum