I have been wrestling with this problem with past 4 hours and to be honest really exhausted... I am trying to write a CSV file through vbscript with multi lines in few of the fields... thought it would be easy peasy ... not so ...My original code is
summary = "This is my brief summary"
description = "Hello" & vbCrLf & "Today, I am going to describe my issue."
reference_id = "0000102203"
Set fso = CreateObject("Scripting.FileSystemObject")
Set csv = fso.CreateTextFile(download_file_name,True)
csv.WriteLine summary & "," description & "," & "Consulting" & "," & reference_id
csv.close
set csv = Nothing
All I am trying to do is create a csv, and when I use excel to open the file would show up as
This is my brief summary| Hello |Consulting| 0000102203
| Today, I am going to describe my issue. |
And what I am getting with numerous tries of replacing vbCrLf with vbCr,vbLf, chr(10), chr(13), "\n", "\n", "\r","\r" and heaps more
This is my brief summary| Hello
Today, I am going to describe my issue. |Consulting| 0000102203
Is there anyway I can actually solve this problem?
Thanks
There is no actual standard. This RFC came well after CSVs were invented. So you can do what you want, but you'll also need code to read your own standard as other tools won't. Implement CR as a special character (code 222 has historical meaning for me - SurveyCraft) and replace with CR after importing.
From https://www.rfc-editor.org/rfc/rfc4180 (also see https://en.wikipedia.org/wiki/Comma-separated_values)
Definition of the CSV Format
While there are various specifications and implementations for the
CSV format (for ex. [4], [5], [6] and [7]), there is no formal
specification in existence, which allows for a wide variety of
interpretations of CSV files. This section documents the format that
seems to be followed by most implementations:
Each record is located on a separate line, delimited by a line
break (CRLF). For example:
aaa,bbb,ccc CRLF
zzz,yyy,xxx CRLF
The last record in the file may or may not have an ending line
break. For example:
aaa,bbb,ccc CRLF
zzz,yyy,xxx
OKay, after 4 hours of literally banging my head on the keyboard, I finally figure out how to solve the problem. So if anyone is interested ... Just changed this part of the code
description = "Hello" & vbCrLf & "Today, I am going to describe my issue."
csv.WriteLine summary & "," description & "," & "Consulting" & "," & reference_id
to
desc1 = "Hello"
desc2 = "Today, I am going to describe my issue."
csv.WriteLine summary & ",""" desc1 & vbCrLf & desc2 & """," & "Consulting" & "," & reference_id
Ta Da ... worked like a charm ...
Related
I am using below code for reporting purpose, everything was fine but all of sudden this code started giving error saying Singular expression refer to nonexisting object
I also break the whole code bit by bit & able to trace the problem but confused what to use in there & why its giving that error statement.
if I remove following part I am able to get the results & relevance works perfectly fine but that will break the formatting which required to csv output.
(html it) of concatenations of
<?relevance
(html it) of concatenations of ( "%22" &
name of computer of it & "%22|%22" &
name of issuer of action of it & "%22|%22" &
name of action of it & "%22|%22" &
status of it as string & "%22|%22" &
state of action of it & "%22%0d%0a"
)
of results of bes actions whose (name of it starts with "Config_item")
?>
I was able to solve it, actually the output which this code is generating against each from was not available for some property which cause the problem.
after I made the below changes into it with else condition it solve the problem.
(html it) of concatenations of ( "%22" &
(name of computer of it | "None") & "%22|%22" &
name of issuer of action of it & "%22|%22" &
name of action of it & "%22|%22" &
status of it as string & "%22|%22" &
state of action of it & "%22%0d%0a"
)
This line actually helped in solving this - (name of computer of it | "None")
I have multiple combo & text boxes to search for different values in my main table on my front end. The code is as follows, just replicated for different types etc. This all works fine.
If Not IsNull(Me.strSearch) Then
strWhere = strWhere & "([tbl_Main.Description] Like ""*" & Me.strSearch & "*"") AND "
End If
My Problem is, I'm trying to create a text box which searches 2 columns simultaneously in my tbl_Main (tbl_Main.LessonsLearnt & tbl_Main.RecommendedAction) but can't figure out how to modify my current code to add another column in, searchable from the same textbox.
Disclaimer: I'm very much a beginner in MS Access - so please keep the explanation as simple as possible :D
If you need any other info - just let me know!
Thanks in advance
strWhere = strWhere & "(tbl_Main.Description Like '*" & Me.strSearch & "*' OR tbl_Main.OtherField Like '*" & Me.strSearch & "*') AND "
This will search for the strSearch being in either Desscription or OtherField. I also replaced your double double quotes with single quotes for better code readability and cross compatibility with other DBMS and removed the brackets that are only needed if you have spaces in your table/field names (something you really should never do anyway).
I'm trying to create a plant database using MS-Access 2010. I'm trying to open a form by clicking on a button. From what I've researched online, I realize the problem lies with the single quotes, but I don't understand coding enough to figure our what I'm supposed to do to bypass the issue.
Here is the code I'm using.
="[Latin Name]=" & "'" & [Latin Name] & "'"
The last part of the Latin name ("cultivar" says a dictionary) usually is enclosed in single quotes itself. So you cannot simply concatenate the name into a query expression where it is separated by single quotes.
If you have ever heard of SQL injection, that's what is happening here.
You need to escape the single quotes with the Replace() function:
="[Latin Name]=" & "'" & Replace([Latin Name], "'", "''") & "'"
I found a strange issue, I'm wondering if anyone knows either why this happens or if it is a known bug (yes I Googled).
I have a database where I have to import CSV files using Access. I'm making some changes to the project to allow UNICODE characters to be into the database. There is some preprocessing involved in VBA by reading and writing using ADODB.Stream.
This ADODB.Stream is the only method of processing UNICODE files that I found to be of a useful level. However, I'm writing UTF-8 files with it and obviously, as it should, it writes a BOM (Byte Order Mark) at the start of the file: .
So far so good Microsoft, way to support standards! Now when this file is saved I want to import it using an ImportExportSpecification. I specifically specify Microsoft's Codepage 65001 which is what the rest of the world calls utf-8. Then try to import and no surprise it works.. Until your file starts with a double quote, then Access complains that the record 2 (2??) could not be parsed.
The unfortunate thing is that I cannot control the source of these files and I'm not involved in the actual imports, it's automated.. So now I either have to remove the BOM, which I don't really want to do.. because I need to read and write the entire file from to disk or in memory and these files are insanely huge.. Or remove the double quote, which is actually necessary for the headers of a file (that I can't control either).
TL;DR: It appears that if your files starts with " you break Access's ImportExport module. Is it a bug, a "feature" or why the .. does it happen?
Ok, so I will remove the BOM, so Christmas doesn't have to start without me. But it's not a solution, it's a band aid. I would very much like to know how to fix this, it's itching me.. :)
Merry Christmas!
It appears that if your files starts with " you break Access's ImportExport module.
No, that is not true, at least for reasonably current versions of Access. Given the CSV file "bomTest.csv":
and the Import Specification
the following statement imports the file correctly in Access 2010
DoCmd.TransferText _
TransferType:=acImportDelim, _
SpecificationName:="BomTest Import Specification", _
TableName:="bomTest", _
FileName:="C:\__tmp\bomTest.csv", _
HasFieldNames:=True
Edit re: comment
Having a BOM in your UTF-8 CSV file when using the ImportExportSpecification method is not necessarily a show-stopper, either. Using the exact same sample CSV file as above, the following code imports it without incident:
Public Sub bomImportTest()
Dim ies As ImportExportSpecification, ies_XML As String
On Error Resume Next
Set ies = CurrentProject.ImportExportSpecifications("zzzTemp")
If Err.Number = 0 Then
ies.Delete
End If
Set ies = Nothing
On Error GoTo 0
ies_XML = ""
ies_XML = ies_XML & "<?xml version=""1.0"" encoding=""utf-8"" ?>" & vbCrLf
ies_XML = ies_XML & "<ImportExportSpecification Path=""C:\Users\Gord\Desktop\bomTest.csv"" xmlns=""urn:www.microsoft.com/office/access/imexspec"">" & vbCrLf
ies_XML = ies_XML & " <ImportText TextFormat=""Delimited"" FirstRowHasNames=""true"" FieldDelimiter="","" TextDelimiter=""{DoubleQuote}"" CodePage=""65001"" Destination=""BomTest"" >" & vbCrLf
ies_XML = ies_XML & " <NumberFormat DecimalSymbol=""."" />" & vbCrLf
ies_XML = ies_XML & " <Columns PrimaryKey="""">" & vbCrLf
ies_XML = ies_XML & " <Column Name=""Col1"" FieldName=""column 1"" Indexed=""NO"" SkipColumn=""false"" DataType=""Text"" Width=""14"" />" & vbCrLf
ies_XML = ies_XML & " <Column Name=""Col2"" FieldName=""column 2"" Indexed=""NO"" SkipColumn=""false"" DataType=""Text"" Width=""14"" />" & vbCrLf
ies_XML = ies_XML & " </Columns>" & vbCrLf
ies_XML = ies_XML & " </ImportText>" & vbCrLf
ies_XML = ies_XML & "</ImportExportSpecification>" & vbCrLf
Set ies = CurrentProject.ImportExportSpecifications.Add("zzzTemp", ies_XML)
ies.Execute
Debug.Print "Import complete."
End Sub
As Guntram Blohm pointed out:
Maybe the access import module reads the BOM, decides on UTF-16, and has a fallback to UTF-8 if the next 2 bytes are not a valid codepoint.
This seems to be the case, it may be a bug, it may be that UTF-8 with BOM was not implemented completely, I don't know.. If you do feel free to drop a comment, I'm still curious.
Anyway, there are 3 ways you can "solve" the issue:
Remove the BOM (rewriting the entire file, could be done in memory rather than reading & writing from disk) see here: http://axlr8r.blogspot.nl/2011/05/how-to-export-data-into-utf-8-without.html
Remove the double quote or place an extra character after the BOM if you can.
Save the file as UTF-16 which Access seems to have better support for. Compared to an ANSI file a UTF-16 file will be twice the size. Whereas UTF-8 would only add extra bytes per character when special characters are met. In my case the file is in an intermediary state, I'm not keeping it, after importing I delete the file. So I chose to save the file as UTF-16 and import it with Codepage 1200 (which is UTF-16 for the rest of the world).
I have written some HTML with notepad, when i open it with IE it works fine and loads the google map on the page.
I have then converted that text into vb.net stream writer. when it creates the html document it wont load the google map. but, if i copy the html that the application creates and paste it into a html document i make with notepad it works fine.
Whats going on?
I use
Using writer As StreamWriter = New StreamWriter("C:\CarPosition" & Loc & ".htm", True)
to write the bulk of the HTML
and i have a loop in the application to write an array, i use the following line to do that:
My.Computer.FileSystem.WriteAllText("C:\CarPosition" & Loc & ".htm", vbNewLine & "['Car:" & CarNumber & "'," & " -" & LatsDecimal & "," & " " & LongsDecimal & ", " & Count & "],", True)
I want to know if vb.net writes some hidden characters in text documents or notepad does? because if i analyse the two html documents they are identical, one loads correctly in IE and one doesnt.
thanks,
Mike
Tips:
Do a binary diff between the two files to find the difference. You can do it online
Check BinaryReader for reading the file in the first place https://stackoverflow.com/a/10353961/390330