I am attempting to import a Comma Separated Value (CSV) file into an MS
Access 2007 mdb table. The table consists of text and numeric (defined as
Double) fields.
Unfortunately, negative amounts are NOT enclosed by double quotes in the CSV
file, instead the are enclosed with parenthesis signs.
Example:
"10,485.16","24",(42),"16.35",(835.33)
Currently, when I import the CSV file, the negative amounts are NOT imported.
I need assistance with how to import these negative amounts.
Thanks in advance.
|
|
0
|
|
|
|
Reply
|
Utf
|
1/5/2010 7:20:01 PM |
|
"ND Pard" <NDPard@discussions.microsoft.com> wrote in message
news:1EB07877-22EC-4366-A2CF-4D58FA29403F@microsoft.com...
>I am attempting to import a Comma Separated Value (CSV) file into an MS
> Access 2007 mdb table. The table consists of text and numeric (defined as
> Double) fields.
>
> Unfortunately, negative amounts are NOT enclosed by double quotes in the
> CSV
> file, instead the are enclosed with parenthesis signs.
>
> Example:
>
> "10,485.16","24",(42),"16.35",(835.33)
>
> Currently, when I import the CSV file, the negative amounts are NOT
> imported.
>
> I need assistance with how to import these negative amounts.
>
> Thanks in advance.
Here's one way:
First load the entire file into a string
Then replace all instances of left paranthesis with minus sign
Then replace all instances of right paranthesis with zero-length string
Save the string as a new file (or overwrite the original) and import that.
Dim f As Integer, buf As String
Open "c:\temp\myFileName.csv" For Binary Access Read As f
buf = Space$(LOF(f))
Get #f, , buf
Close f
buf = Replace(buf, "(", "-")
buf = Replace(buf, ")", "")
Open "c:\temp\myFileName1.csv" For Binary Access Write As f
Put #f, , buf
Close f
|
|
0
|
|
|
|
Reply
|
Stuart
|
1/5/2010 8:11:40 PM
|
|
Thanks Stuart. Unfortunately I was unable to run your subprocedure in
Access. I am not sure why not; however, your solution allowed me to write
the following procedure using the File System Object (or FSO) that did work.
Sub Replace_Negative_TextStream()
'This subprocedure reads the selected CVS file one line at a time.
'It then replaces any ,( with ,"- in the line read
'and replaces any ), with ", in the line read.
'Thus, a negative number that looked like: ,(42.84), will not appear as
,"-42.84",
'Finally, it writes the revised line to the text file:
BUS-BillHistory_NegRevised.csv
'set a reference to: Microsoft Scripting Runtime via Tools | References
Dim fso As FileSystemObject
Dim tsIN As TextStream
Dim tsOUT As TextStream
Dim strLineRead As String
Dim strFileToRead As String
Dim FLD As Object
Const ForReading = 1, ForWriting = 2, ForAppending = 8
strFileToRead = "C:\temp\myFileName1.csv"
Set fso = New FileSystemObject
Set FLD = fso.GetFolder("C:\Temp\")
'Debug.Print FLD.Type
'Re-write the file to a new CVS text file
Set tsOUT = FLD.CreateTextFile("myFileName1_NegRevised.csv", True)
'If the above value is True if the file can be overwritten;
'False if it can't be overwritten.
'If omitted, existing files are not overwritten.
Set tsIN = fso.OpenTextFile(strFileToRead, ForReading)
While Not tsIN.AtEndOfStream
strLineRead = tsIN.ReadLine
strLineRead = Replace(strLineRead, ",(", ",""-")
strLineRead = Replace(strLineRead, "),", """,")
With tsOUT
.Write strLineRead
.Write Chr(13)
End With
Wend
tsOUT.Close
Set tsIN = Nothing
Set tsOUT = Nothing
Set FLD = Nothing
Set fso = Nothing
End Sub
|
|
0
|
|
|
|
Reply
|
Utf
|
1/6/2010 4:12:01 PM
|
|
"ND Pard" <NDPard@discussions.microsoft.com> wrote in message
news:E5C4638C-9900-494C-8AE1-822F72D07A93@microsoft.com...
> Thanks Stuart. Unfortunately I was unable to run your subprocedure in
> Access. I am not sure why not; however, your solution allowed me to write
> the following procedure using the File System Object (or FSO) that did
> work.
>
> Sub Replace_Negative_TextStream()
> 'This subprocedure reads the selected CVS file one line at a time.
> 'It then replaces any ,( with ,"- in the line read
> 'and replaces any ), with ", in the line read.
> 'Thus, a negative number that looked like: ,(42.84), will not appear as
> ,"-42.84",
> 'Finally, it writes the revised line to the text file:
> BUS-BillHistory_NegRevised.csv
>
> 'set a reference to: Microsoft Scripting Runtime via Tools | References
> Dim fso As FileSystemObject
> Dim tsIN As TextStream
> Dim tsOUT As TextStream
> Dim strLineRead As String
> Dim strFileToRead As String
> Dim FLD As Object
> Const ForReading = 1, ForWriting = 2, ForAppending = 8
>
> strFileToRead = "C:\temp\myFileName1.csv"
>
> Set fso = New FileSystemObject
>
> Set FLD = fso.GetFolder("C:\Temp\")
> 'Debug.Print FLD.Type
>
> 'Re-write the file to a new CVS text file
> Set tsOUT = FLD.CreateTextFile("myFileName1_NegRevised.csv", True)
> 'If the above value is True if the file can be overwritten;
> 'False if it can't be overwritten.
> 'If omitted, existing files are not overwritten.
>
> Set tsIN = fso.OpenTextFile(strFileToRead, ForReading)
>
> While Not tsIN.AtEndOfStream
> strLineRead = tsIN.ReadLine
> strLineRead = Replace(strLineRead, ",(", ",""-")
> strLineRead = Replace(strLineRead, "),", """,")
>
> With tsOUT
> .Write strLineRead
> .Write Chr(13)
> End With
> Wend
>
> tsOUT.Close
>
> Set tsIN = Nothing
> Set tsOUT = Nothing
> Set FLD = Nothing
> Set fso = Nothing
>
> End Sub
Glad you got it working, but I'm curious as to why my code 'didn't work'.
Did it compile? Did you get an error? If so, on which line?
That's if you remember what happened before writing your version :-)
|
|
0
|
|
|
|
Reply
|
Stuart
|
1/6/2010 4:32:47 PM
|
|
|
3 Replies
594 Views
(page loaded in 0.07 seconds)
Similiar Articles: Help, how do I import a CSV file when Negatives are in Parenthesis ...I am attempting to import a Comma Separated Value (CSV) file into an MS Access 2007 mdb table. The table consists of text and numeric (defined as ... Import Multiple CSV Files Into Access 2007 - microsoft.public ...Help, how do I import a CSV file when Negatives are in Parenthesis ... Import Multiple CSV Files Into Access 2007 - microsoft.public ... Help, how do I import a CSV file ... want parentheses, not negative numbers - microsoft.public.mac ...Help, how do I import a CSV file when Negatives are in Parenthesis ... want parentheses, not negative numbers - microsoft.public.mac ... Help, how do I import a CSV file ... How can I read CSV file using VBA? - microsoft.public.access ...Help, how do I import a CSV file when Negatives are in Parenthesis ... How can I read CSV file using VBA? - microsoft.public.access ... Help, how do I import a CSV file ... Double parentheses when scripting defaults - microsoft.public ...Help, how do I import a CSV file when Negatives are in Parenthesis ..... consists of text and numeric (defined as Double ... csv 'set a reference to: Microsoft Scripting ... Text import - date format - microsoft.public.access.forms ...Can someone help; pipe delimited text import not working ... Help, how do I import a CSV file when Negatives are in Parenthesis ... Text import - date format - microsoft ... Update query to replace everything after comma - microsoft.public ...Help, how do I import a CSV file when Negatives are in Parenthesis ... Help, how do I import a CSV file when Negatives are in Parenthesis ... Update query to replace ... Write a line in a text file with quotes in it - microsoft.public ...Write a line in a text file with quotes in it - microsoft.public ... Help, how do I import a CSV file when Negatives are in Parenthesis ..... are NOT enclosed by double ... Trailing "-" for Neg Number - microsoft.public.access ...Help, how do I import a CSV file when Negatives are in Parenthesis ... Trailing "-" for Neg Number - microsoft.public.access ... Help, how do I import a CSV file when ... Can I import existing styles into a document? - microsoft.public ...How to import a file including header into a new document in Word ... Can I import ... Styles from Template(.dotx) to Word document(.docx) Could any one help me how to import ... Help, how do I import a CSV file when Negatives are in Parenthesis ...DataBase - Help, how do I import a CSV file when Negatives are in Parenthesis Help, how do I import a CSV file when Negatives are in ParenthesisI am attempting to import a Comma Separated Value (CSV) file into an MS Access 2007 mdb table. The table consists of text and numeric (defined as Help, how do I import a CSV file when Negatives are in Parenthesis ...I am attempting to import a Comma Separated Value (CSV) file into an MS Access 2007 mdb table. The table consists of text and numeric (defined as ... Import Multiple CSV Files Into Access 2007 - microsoft.public ...Help, how do I import a CSV file when Negatives are in Parenthesis ... Import Multiple CSV Files Into Access 2007 - microsoft.public ... Help, how do I import a CSV file ... Excel - Excel 2010 Keeps Changing Format From Number To Currency ...- Free Excel Help ... to text prior to import. I would like to find a way of opening the CSV file ... desktop where all negative numbers are in parenthesis. The files ... 7/12/2012 7:55:33 AM
|