Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Question about CSV file [modified]

Question about CSV file [modified]

Scheduled Pinned Locked Moved Visual Basic
questionhelptutorial
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TeiUKei
    wrote on last edited by
    #1

    I gt a line of data which i intend to display in CSV file as below, "A12435625" "b" "b2" "c44566" "A12435625" "b" "b2" "c44566" however with my coding, it only displayed as "A12435625" "b" "b2" "c44566" "A12435625" "b" "b2" "c44566" What is my mistake and how to display my desire answer? Below is my coding, pls kindly help! FileOpen(2, sFilename, OpenMode.Output) For b = 0 To CreatePP.Counter - 1 For a = 0 To 4 c = CStr(CreatePP.Data(a)) If c.Length <= 1 Then c = c.PadRight(2, CChar(" ")) ElseIf c.Length <= 2 Then c = c.PadRight(3, CChar(" ")) ElseIf c.Length > 2 And c.Length <= 4 Then c = c.PadRight(5, CChar(" ")) ElseIf c.Length > 4 And c.Length <= 6 Then c = c.PadRight(7, CChar(" ")) ElseIf c.Length > 6 And c.Length <= 11 Then c = c.PadRight(12, CChar(" ")) End If Write(2, c) Next a Next b FileClose(2) -- modified at 0:27 Thursday 12th October, 2006

    D 1 Reply Last reply
    0
    • T TeiUKei

      I gt a line of data which i intend to display in CSV file as below, "A12435625" "b" "b2" "c44566" "A12435625" "b" "b2" "c44566" however with my coding, it only displayed as "A12435625" "b" "b2" "c44566" "A12435625" "b" "b2" "c44566" What is my mistake and how to display my desire answer? Below is my coding, pls kindly help! FileOpen(2, sFilename, OpenMode.Output) For b = 0 To CreatePP.Counter - 1 For a = 0 To 4 c = CStr(CreatePP.Data(a)) If c.Length <= 1 Then c = c.PadRight(2, CChar(" ")) ElseIf c.Length <= 2 Then c = c.PadRight(3, CChar(" ")) ElseIf c.Length > 2 And c.Length <= 4 Then c = c.PadRight(5, CChar(" ")) ElseIf c.Length > 4 And c.Length <= 6 Then c = c.PadRight(7, CChar(" ")) ElseIf c.Length > 6 And c.Length <= 11 Then c = c.PadRight(12, CChar(" ")) End If Write(2, c) Next a Next b FileClose(2) -- modified at 0:27 Thursday 12th October, 2006

      D Offline
      D Offline
      Dave Sexton
      wrote on last edited by
      #2

      I think if you add either vbCrLf or ControlChars.NewLine to where you want the string to be written on a new line that may work.

      T 1 Reply Last reply
      0
      • D Dave Sexton

        I think if you add either vbCrLf or ControlChars.NewLine to where you want the string to be written on a new line that may work.

        T Offline
        T Offline
        TeiUKei
        wrote on last edited by
        #3

        Thanx for help although it didn't work. As a result,it ony return "aaaaaaaa",blank line,"b",blank line,"b2". Do you have another tricks or idea?

        D 1 Reply Last reply
        0
        • T TeiUKei

          Thanx for help although it didn't work. As a result,it ony return "aaaaaaaa",blank line,"b",blank line,"b2". Do you have another tricks or idea?

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          We need to see your code so we can figure out what you did wrong.

          Dave Kreskowiak Microsoft MVP - Visual Basic

          T 1 Reply Last reply
          0
          • D Dave Kreskowiak

            We need to see your code so we can figure out what you did wrong.

            Dave Kreskowiak Microsoft MVP - Visual Basic

            T Offline
            T Offline
            TeiUKei
            wrote on last edited by
            #5

            It is almost the same,i highlighted the change the below. I gt a line of data which i intend to display in CSV file as below, "A12435625" "b" "b2" "c44566" "A12435625" "b" "b2" "c44566" however with my coding, it only displayed as "A12435625" "b" "b2" "c44566" "A12435625" "b" "b2" "c44566" What is my mistake and how to display my desire answer? Below is my coding, pls kindly help! FileOpen(2, sFilename, OpenMode.Output) For b = 0 To CreatePP.Counter - 1 For a = 0 To 4 c = CStr(CreatePP.Data(a)) If c.Length <= 1 Then c = c.PadRight(2, CChar(" ")) ElseIf c.Length <= 2 Then c = c.PadRight(3, CChar(" ")) ElseIf c.Length > 2 And c.Length <= 4 Then c = c.PadRight(5, CChar(" ")) ElseIf c.Length > 4 And c.Length <= 6 Then c = c.PadRight(7, CChar(" ")) ElseIf c.Length > 6 And c.Length <= 11 Then c = c.PadRight(12, CChar(" ")) End If Write(2, c) Write(2,c,ControlChars.Newline) Next a Next b FileClose(2) I appreciate so much for help!Thanx!

            D 1 Reply Last reply
            0
            • T TeiUKei

              It is almost the same,i highlighted the change the below. I gt a line of data which i intend to display in CSV file as below, "A12435625" "b" "b2" "c44566" "A12435625" "b" "b2" "c44566" however with my coding, it only displayed as "A12435625" "b" "b2" "c44566" "A12435625" "b" "b2" "c44566" What is my mistake and how to display my desire answer? Below is my coding, pls kindly help! FileOpen(2, sFilename, OpenMode.Output) For b = 0 To CreatePP.Counter - 1 For a = 0 To 4 c = CStr(CreatePP.Data(a)) If c.Length <= 1 Then c = c.PadRight(2, CChar(" ")) ElseIf c.Length <= 2 Then c = c.PadRight(3, CChar(" ")) ElseIf c.Length > 2 And c.Length <= 4 Then c = c.PadRight(5, CChar(" ")) ElseIf c.Length > 4 And c.Length <= 6 Then c = c.PadRight(7, CChar(" ")) ElseIf c.Length > 6 And c.Length <= 11 Then c = c.PadRight(12, CChar(" ")) End If Write(2, c) Write(2,c,ControlChars.Newline) Next a Next b FileClose(2) I appreciate so much for help!Thanx!

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              First, copy and paste your ACTUAL code, not something that's "alomst the same". I very small difference in your code can make all the difference in your output. Next, don't use ControlChars.NewLine. If you want a complete new line, which is TWO characters in Windows (CR and LF), use Environment.NewLine instead.

              TeiUKei wrote:

              If c.Length <= 1 Then c = c.PadRight(2, CChar(" ")) ElseIf c.Length <= 2 Then c = c.PadRight(3, CChar(" ")) ElseIf c.Length > 2 And c.Length <= 4 Then c = c.PadRight(5, CChar(" ")) ElseIf c.Length > 4 And c.Length <= 6 Then c = c.PadRight(7, CChar(" ")) ElseIf c.Length > 6 And c.Length <= 11 Then c = c.PadRight(12, CChar(" ")) End If

              What on earth is this doing?? You realize that c is being evaluated for length in every one of those if statements, including after each new change you make by padding it with spaces?

              Dave Kreskowiak Microsoft MVP - Visual Basic

              T 1 Reply Last reply
              0
              • D Dave Kreskowiak

                First, copy and paste your ACTUAL code, not something that's "alomst the same". I very small difference in your code can make all the difference in your output. Next, don't use ControlChars.NewLine. If you want a complete new line, which is TWO characters in Windows (CR and LF), use Environment.NewLine instead.

                TeiUKei wrote:

                If c.Length <= 1 Then c = c.PadRight(2, CChar(" ")) ElseIf c.Length <= 2 Then c = c.PadRight(3, CChar(" ")) ElseIf c.Length > 2 And c.Length <= 4 Then c = c.PadRight(5, CChar(" ")) ElseIf c.Length > 4 And c.Length <= 6 Then c = c.PadRight(7, CChar(" ")) ElseIf c.Length > 6 And c.Length <= 11 Then c = c.PadRight(12, CChar(" ")) End If

                What on earth is this doing?? You realize that c is being evaluated for length in every one of those if statements, including after each new change you make by padding it with spaces?

                Dave Kreskowiak Microsoft MVP - Visual Basic

                T Offline
                T Offline
                TeiUKei
                wrote on last edited by
                #7

                Well, this is the actual code i am doing. I still can't find where the problem is although i use Environment.Newline, sorry for the troublesome! This is the data i try to displayed Dim Data1 As New PropertyCreateFile Dim aa As New ArrayList aa.Add("A4585525581") aa.Add("b") aa.Add("b2") aa.Add("c234") aa.Add("M45686") Data1.Data = aa Data1.Counter = 3 'aaa.CreateTextFile(Data1) aaa.CreateCSVFile(Data1) Public Function CreateCSVFile(ByVal CreatePP As PropertyCreateFile) As String Dim sFilename As String = (CurDir & "/CSVFile.csv") Dim b As Integer Dim a As Integer Dim c As String Dim Data(4) As String FileOpen(20, sFilename, OpenMode.Output) For b = 0 To CreatePP.Counter - 1 For a = 0 To 4 c = CStr(CreatePP.Data(a)) 'For these if statement, i intend to get and check the 'length of data pass in by user, so the data can displayed 'systematically If c.Length <= 1 Then c = c.PadRight(2, CChar(" ")) ElseIf c.Length <= 2 Then c = c.PadRight(3, CChar(" ")) ElseIf c.Length > 2 And c.Length <= 4 Then c = c.PadRight(5, CChar(" ")) ElseIf c.Length > 4 And c.Length <= 6 Then c = c.PadRight(7, CChar(" ")) ElseIf c.Length > 6 And c.Length <= 11 Then c = c.PadRight(12, CChar(" ")) End If Data(a) = c 'Is it right if i use Environment.NewLine here? Write(20, Data(a), Environment.NewLine) Next a Next b FileClose(20)

                D 1 Reply Last reply
                0
                • T TeiUKei

                  Well, this is the actual code i am doing. I still can't find where the problem is although i use Environment.Newline, sorry for the troublesome! This is the data i try to displayed Dim Data1 As New PropertyCreateFile Dim aa As New ArrayList aa.Add("A4585525581") aa.Add("b") aa.Add("b2") aa.Add("c234") aa.Add("M45686") Data1.Data = aa Data1.Counter = 3 'aaa.CreateTextFile(Data1) aaa.CreateCSVFile(Data1) Public Function CreateCSVFile(ByVal CreatePP As PropertyCreateFile) As String Dim sFilename As String = (CurDir & "/CSVFile.csv") Dim b As Integer Dim a As Integer Dim c As String Dim Data(4) As String FileOpen(20, sFilename, OpenMode.Output) For b = 0 To CreatePP.Counter - 1 For a = 0 To 4 c = CStr(CreatePP.Data(a)) 'For these if statement, i intend to get and check the 'length of data pass in by user, so the data can displayed 'systematically If c.Length <= 1 Then c = c.PadRight(2, CChar(" ")) ElseIf c.Length <= 2 Then c = c.PadRight(3, CChar(" ")) ElseIf c.Length > 2 And c.Length <= 4 Then c = c.PadRight(5, CChar(" ")) ElseIf c.Length > 4 And c.Length <= 6 Then c = c.PadRight(7, CChar(" ")) ElseIf c.Length > 6 And c.Length <= 11 Then c = c.PadRight(12, CChar(" ")) End If Data(a) = c 'Is it right if i use Environment.NewLine here? Write(20, Data(a), Environment.NewLine) Next a Next b FileClose(20)

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  This is not all the code that's involved. What's the code for PropertyCreateFile and what is it supposed to do? Where is aaa defined ("aaa.CreateCSVFile(Data1)"), and as what?

                  If c.Length <= 1 Then
                  c = c.PadRight(2, CChar(" "))
                  ElseIf c.Length <= 2 Then
                  c = c.PadRight(3, CChar(" "))
                  ElseIf c.Length > 2 And c.Length <= 4 Then
                  c = c.PadRight(5, CChar(" "))
                  ElseIf c.Length > 4 And c.Length <= 6 Then
                  c = c.PadRight(7, CChar(" "))
                  ElseIf c.Length > 6 And c.Length <= 11 Then
                  c = c.PadRight(12, CChar(" "))

                  A CSV file doesn't care about padding, so what's this for? Is this for displaying the information in a TextBox or something, while you write the data to a CSV file?

                  Dave Kreskowiak Microsoft MVP - Visual Basic

                  T 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    This is not all the code that's involved. What's the code for PropertyCreateFile and what is it supposed to do? Where is aaa defined ("aaa.CreateCSVFile(Data1)"), and as what?

                    If c.Length <= 1 Then
                    c = c.PadRight(2, CChar(" "))
                    ElseIf c.Length <= 2 Then
                    c = c.PadRight(3, CChar(" "))
                    ElseIf c.Length > 2 And c.Length <= 4 Then
                    c = c.PadRight(5, CChar(" "))
                    ElseIf c.Length > 4 And c.Length <= 6 Then
                    c = c.PadRight(7, CChar(" "))
                    ElseIf c.Length > 6 And c.Length <= 11 Then
                    c = c.PadRight(12, CChar(" "))

                    A CSV file doesn't care about padding, so what's this for? Is this for displaying the information in a TextBox or something, while you write the data to a CSV file?

                    Dave Kreskowiak Microsoft MVP - Visual Basic

                    T Offline
                    T Offline
                    TeiUKei
                    wrote on last edited by
                    #9

                    Really? So, how about when it convert to text file? Anyway,this is where aaa defined which you ask about. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim Data1 As New PropertyCreateFile Dim aa As New ArrayList aa.Add("A45855") aa.Add("b") aa.Add("120306") aa.Add("c234") aa.Add("M45686") Data1.Data = aa Data1.Counter = 3 aaa.CreateCSVFile(Data1) End Sub 'This is the code for PropertyCreateFile. It will take in the data detail and 'counter on how many times it will loop. Public Class PropertyCreateFile Dim Str As ArrayList Dim Ctr As Integer Dim UBSBadgeNo As Integer Public Property Data() As ArrayList Get Return Str End Get Set(ByVal Value As ArrayList) Str = Value End Set End Property Public Property Counter() As Integer Get Return Ctr End Get Set(ByVal Value As Integer) Ctr = Value End Set End Property End Class Public Function CreateCSVFile(ByVal CreatePP As PropertyCreateFile) As String Dim sFilename As String = (CurDir & "/CSVFile.csv") Dim b As Integer Dim a As Integer Dim c As String Dim x As String Dim Data(4) As String FileOpen(20, sFilename, OpenMode.Output) For b = 0 To CreatePP.Counter - 1 For a = 0 To 4 c = CStr(CreatePP.Data(a)) If c.Length <= 1 Then c = c.PadRight(2, CChar(" ")) ElseIf c.Length <= 2 Then c = c.PadRight(3, CChar(" ")) ElseIf c.Length > 2 And c.Length <= 4 Then c = c.PadRight(5, CChar(" ")) ElseIf c.Length > 4 And c.Length <= 6 Then c = c.PadRight(7, CChar(" ")) ElseIf c.Length > 6 And c.Length <= 11 Then c = c.PadRight(12, CChar(" ")) End If Data(a) = c Next a x = Data(0) + Data(1) + Data(2) + Data(3) + Data(4) WriteLine(20, x, Environment.NewLine) Next b FileClose(20) Return Nothing End Function Thanks for help, Dave!!

                    D 1 Reply Last reply
                    0
                    • T TeiUKei

                      Really? So, how about when it convert to text file? Anyway,this is where aaa defined which you ask about. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim Data1 As New PropertyCreateFile Dim aa As New ArrayList aa.Add("A45855") aa.Add("b") aa.Add("120306") aa.Add("c234") aa.Add("M45686") Data1.Data = aa Data1.Counter = 3 aaa.CreateCSVFile(Data1) End Sub 'This is the code for PropertyCreateFile. It will take in the data detail and 'counter on how many times it will loop. Public Class PropertyCreateFile Dim Str As ArrayList Dim Ctr As Integer Dim UBSBadgeNo As Integer Public Property Data() As ArrayList Get Return Str End Get Set(ByVal Value As ArrayList) Str = Value End Set End Property Public Property Counter() As Integer Get Return Ctr End Get Set(ByVal Value As Integer) Ctr = Value End Set End Property End Class Public Function CreateCSVFile(ByVal CreatePP As PropertyCreateFile) As String Dim sFilename As String = (CurDir & "/CSVFile.csv") Dim b As Integer Dim a As Integer Dim c As String Dim x As String Dim Data(4) As String FileOpen(20, sFilename, OpenMode.Output) For b = 0 To CreatePP.Counter - 1 For a = 0 To 4 c = CStr(CreatePP.Data(a)) If c.Length <= 1 Then c = c.PadRight(2, CChar(" ")) ElseIf c.Length <= 2 Then c = c.PadRight(3, CChar(" ")) ElseIf c.Length > 2 And c.Length <= 4 Then c = c.PadRight(5, CChar(" ")) ElseIf c.Length > 4 And c.Length <= 6 Then c = c.PadRight(7, CChar(" ")) ElseIf c.Length > 6 And c.Length <= 11 Then c = c.PadRight(12, CChar(" ")) End If Data(a) = c Next a x = Data(0) + Data(1) + Data(2) + Data(3) + Data(4) WriteLine(20, x, Environment.NewLine) Next b FileClose(20) Return Nothing End Function Thanks for help, Dave!!

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      TeiUKei wrote:

                      Really? So, how about when it convert to text file?

                      A CSV file (Comma Seperated Values) IS a text file!

                      TeiUKei wrote:

                      WriteLine(20, x, Environment.NewLine)

                      Change it to this:

                      WriteLine(20, x)
                      WriteLine(20, Environment.NewLine)

                      Dave Kreskowiak Microsoft MVP - Visual Basic

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups