I have a text file with x number of lines. When opening the file, I have an extra blank line which I presume is a carriage return,
How do I
1. ensure I don't have a blank line when I write to the file or
2. get rid of the extra line afterwards
Here's the code I've used for the data write: - - Private Sub Command0_Click()
-
-
Dim rst As DAO.Recordset
-
-
Set rst = CurrentDb.OpenRecordset("Table2", dbOpenDynaset)
-
-
DoCmd.TransferText acExportDelim, "", "Table1", "D:\Table1.txt", False
-
DoCmd.TransferText acExportDelim, "", "Table2", "D:\Table2.txt", False
-
-
Open "D:\Table1.txt" For Append As #1
-
Open "D:\Table2.txt" For Input As #2
-
-
Do While Not rst.EOF
-
Print #1, rst!FirstName
-
rst.MoveNext
-
Loop
-
-
Close #1
-
Close #2
-
-
Kill ("D:\Table2.txt")
-
-
End Sub
6 3762
I have a text file with x number of lines. When opening the file, I have an extra blank line which I presume is a carriage return,
How do I
1. ensure I don't have a blank line when I write to the file or
2. get rid of the extra line afterwards
Here's the code I've used for the data write: - - Private Sub Command0_Click()
-
-
Dim rst As DAO.Recordset
-
-
Set rst = CurrentDb.OpenRecordset("Table2", dbOpenDynaset)
-
-
DoCmd.TransferText acExportDelim, "", "Table1", "D:\Table1.txt", False
-
DoCmd.TransferText acExportDelim, "", "Table2", "D:\Table2.txt", False
-
-
Open "D:\Table1.txt" For Append As #1
-
Open "D:\Table2.txt" For Input As #2
-
-
Do While Not rst.EOF
-
Print #1, rst!FirstName
-
rst.MoveNext
-
Loop
-
-
Close #1
-
Close #2
-
-
Kill ("D:\Table2.txt")
-
-
End Sub
Typically a line containing a Carriage Return only will have a 0 length. Test for this condition before you write to the file. Modify the code within your Loop as follows, and let me know how you make out: - Do While Not rst.EOF
-
If Len(rst!FirstName) > 0 Then
-
Print #1, rst!FirstName
-
rst.MoveNext
-
End If
Loop
Tried that, but made no difference. I don't think the Len function includes the vbCrLf in it's length evaluation.
Tried that, but made no difference. I don't think the Len function includes the vbCrLf in it's length evaluation.
Try this and let me know again: - Do While Not rst.EOF
-
If Instr(rst![FirstName], vbCrLf) = 0 Then 'If Carriage Return not in Line
-
Print #1, rst!FirstName
-
End If
-
rst.MoveNext
-
Loop
I will try that tomorrow. Just a point here, in that there will be a carriage return for each record. It's just the LAST carriage return that I'm trying to get rid of. I'll try your suggestion and see how it goes.
It didn't work, unfortunately.
It didn't work, unfortunately.
Give this a try. The Debug.Print statement will generate a Run Time Error when it comes to the Line with the last Carriage Return. It will go to the Handler which will close the File and the Recordset. The previous lines would have already been written to the Recordset. Let me know how you make out. - Dim strLine As String, MyDB As DAO.Database, MyRS As DAO.Recordset
-
-
On Error GoTo Err_Click
-
Set MyDB = CurrentDb()
-
Set MyRS = MyDB.OpenRecordset("tblName", dbOpenDynaset)
-
-
-
Open "C:\Dell\Test.txt" For Input As #1
-
-
Do While Not EOF(1)
-
Line Input #1, strLine
-
Debug.Print Left$(strLine, Len(strLine) - 1)
-
MyRS.AddNew
-
MyRS![TheName] = strLine
-
MyRS.Update
-
Loop
-
-
Close #1
-
MyRS.Close
-
-
Err_Click_Exit:
-
Exit Sub
-
-
Err_Click:
-
Close #1
-
MyRS.Close
-
Resume Err_Click_Exit:
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Canes_Rock |
last post by:
The information posted at:
...
|
by: Josh |
last post by:
Hi,
I'm using System.Data.DataSet.ReadXml to convert some xml from a webservice
to a DataSet. The xml looks like:
<?xml version="1.0"...
|
by: John Dalberg |
last post by:
Hi
What's the regex to remove the carriage return/line field from a string?
These can occur multiple times in the string as in xxx\r\n\r\n.
--
John Dalberg
|
by: Andyza |
last post by:
I'm using FileSystemObject to open and write to a tab delimited text
file.
First, I connect to a database and select some data. Then I create the
text file and insert each record in the text...
|
by: Smythe32 |
last post by:
Hi All,
I am trying to remove carriage returns from a text field via some code.
Here is an example of what I am up against.
The field streetname contains the little square box that I believe...
|
by: shajias |
last post by:
Hi,
I am having a xml code like this
<name>
I am having a carriage return here.
Second line with carriage return.
This is the last line.
</name>
|
by: polohero |
last post by:
I am using an FtpClient.cs that someone else created and when I'm uploading (it uses a Socket) a file a carriage return is getting deleted when the file appears on the ftp location. Normally this...
|
by: LOCAFO |
last post by:
I am using vb.net and have a long string of text. When I use vbnewline or vbCrLf, for a carraige retun, I get a black square box in the SQL database table. I need the carriage return but I don't...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |