Reputation: 23
I have a VB6 Program that saves the text in a Text Box to a File, and When you open it again, The same text will be there, But whenever I re-open it the textbox text now has quotes around it, how might I remove the quotes? code is:
Private Sub Form_Load()
On Error GoTo NoFile
Randomize
Dim sFile As String
Dim Blank As String
Dim c1Path As String
Dim iFileNum As Integer
sFile = "C:\JPLData"
iFileNum = FreeFile
Open sFile For Input As iFileNum
Line Input #iFileNum, c1Path
Close #iFileNum
Text1.Text = c1Path
NoFile:
If Err.Number = 5 Then
sFile = "C:\JPLData"
c1Path = "No Custom Defined."
iFileNum = FreeFile
Open sFile For Output As iFileNum
Write #iFileNum, Text1.Text
Close #iFileNum
End If
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim sFile As String
Dim cName As String
Dim iFileNum As Integer
sFile = "C:\JPLData"
cName = vbClrf & Text1.Text & vbClrf
iFileNum = FreeFile
Open sFile For Output As iFileNum
Write #iFileNum, cName
Close #iFileNum
End Sub
EDIT: I've answered my own problem, I spelled vbCrLf wrong, and the I foorgot to add in the BLsnk variable to handle the Quotes :P
Upvotes: 1
Views: 264
Reputation: 1072
To remove string quotes when writting to a file use Print instead of Write.
Dim ff As Integer, MyString As String
ff = FreeFile
TheString= "test"
Open "myfile.txt" For Output As #ff
Print #ff, TheString
Close #ff
Upvotes: 0
Reputation: 1183
LINE INPUT
reads an entire line of text from your file including commas and quotation marks. Using LINE INPUT
you can read something like this CSV data sample into one String variable:
12345,"John Doe","Fake Street",123,"Test Town",900343
The entire line will become one String and you will have to split it on your own. The "opposite" statement for LINE INPUT
is PRINT
. PRINT
outputs as String as is into a file. That String may contain commas, quotation marks etc.
INPUT
is used to read separate fields (column values = cells!) from a single line. It expects the format WRITE
produces. So INPUT
and WRITE
go together. Using INPUT
you can read several variables from a single comma separated line of your file without having to split the columns on your own. You don't have to use split
or any regular expressions stuff. Moreover, you don't have to do any type casting. WRITE
and INPUT
just work together hand in hand.
For example, one could read a comma separated data row like this:
Dim Id As Integer, FullName As String, Street As String, Income As Double
...
' Write a few separated fields in one operation:
Write #fx, Id, FullName, Street, Income
...
' Read all the fields with different types using just one line of code:
Input #fy, Id, FullName, Street, Income ' Read the cell values straight into your variables
If you mix the two pairs LINE INPUT
/PRINT
and INPUT
/WRITE
you'll not get the intended results in most cases.
If you only want to save/restore a single String (whatever commas, ... it might contain) and don't have several fields (especially with different data types (Integer, String, ...)), go for LINE INPUT
and PRINT
. Those do not include any field separation processing. The just work with entire lines separated by vbCrLf
. If you use PRINT
, you won't need to write any additional vbCrLf
characters as PRINT
will include one line break at the end automatically. (If you don't want that line break at the end, put a ;
at the end of your statement line.)
' Save a single String
Dim ff As Integer, MyString As String
ff = FreeFile
MyString = "Hello World!"
Open "myfile.txt" For Output As #ff
Print #ff, MyString 'Just write the String without any additional characters.
Close #ff
' ... Later restore the entire line into one String:
Dim RestoredString As String
ff = FreeFile
Open "myfile.txt" For Input As #ff
Line Input #ff, RestoredString
Close #ff
Upvotes: 0
Reputation: 124766
The documentation says that data written using the Write #
statement is usually read using the Input #
statement.
Also: data writen using the Print #
statement us usually read using the Line Input #
statement.
You're mixing Write #
with Line Input #
, hence the inconsistency.
Upvotes: 2
Reputation: 56755
The [Line] Input
and Write
commands are quite old statements that still retain a lot of legacy behavior. It is likely that one of these is adding the quotes.
This simplest fix is probaly just to remove the quotes when you display the text box. So change this line:
Text1.Text = c1Path
to this:
Text1.Text = Replace(c1Path, Chr(34), "")
The Chr(34)
is the quotation mark character, the Replace
function just searchs the c1Path
string for all instances of Chr(34)
(quotes) and replaces them with nothing (""
, which is the empty string), effectively deleting them.
Upvotes: 0