CSV creation

M

musosdev

Hi

I've written some code to generate a CSV file from a database table.
However, some colums are getting broken up into 2 fields, or truncated - I
think cos they have comma in, and of course commas are used as field
seperators.

How can I ensure that my fields stay in tact?!

Thanks
 
F

Fred

Dans : musosdev écrivait :
Hi
Hello,

I've written some code to generate a CSV file from a database table.
However, some colums are getting broken up into 2 fields, or
truncated - I think cos they have comma in, and of course commas are
used as field seperators.

How can I ensure that my fields stay in tact?!


According to the specifications I found (seems there is not a real
standard (?)) I use this little program in vb to escape the values.
separator is the chosen separator for the CSV file : a comma most of the
time
a value should be enclosed with quotes when :
- containing the separator
- containing line breaks
- containing quotes
- starting or ending with space

If strvalue.Contains(separator) OrElse _
strvalue.Contains(vbCrLf) OrElse _
strvalue.Contains(vbCr) OrElse _
strvalue.Contains(vbLf) OrElse _
strvalue.Contains("""") OrElse _
strvalue.StartsWith(" ") OrElse _
strvalue.EndsWith(" ") Then
strvalue = strvalue.Replace("""", """""")
strvalue = """" & strvalue & """"
End If
 
C

Cowboy \(Gregory A. Beamer\)

The norm is to surround text fields with quotes, so they do not break on
commas inside the field.
 
M

musosdev

Thank you both - problem solved!

Cowboy (Gregory A. Beamer) said:
The norm is to surround text fields with quotes, so they do not break on
commas inside the field.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top