export to CSV.

Good Afternoon,

i'm relativity new to this but i'm getting close to the result that i'm after.. however when i run the section of script below it exports the data to a csv in a list 

e.g

BC(1),

BC(2),

BC(3)

i'm after it in a row, is it possible to make it export the data in a row.. e.g. BC(1),BC(2),BC(3)

---

Dim BC(1 To 3) As String

BC(1) = TextBox1.Value
BC(2) = " "
BC(3) = TextBox3_Title.Value
For i = 1 To 3

mytxt = BC(i)

Open "C:\test.csv" For Append As #1

Print #1, mytxt
Close #1

Next i

Parents
  • Hi,

    for future posts and questions, I recommend to read and follow MicroStation Programming forum best practices, which will help you to provide a standard set of information, that are missing now, like product do you use (is it MicroStation, PowerDraft or something else?), it's version or build number (V8.0 can behave differently than CONNECT Edition) and also to format the subject properly (e.g. "[VBA V8 SS3] Export CSV").

    I also recommend to use Syntaxhighlighter tool (yellow pencil icon in editor) with properly selected language (VB in your case) every time you post any code snippet. It will display the code properly formated and colorized, because "code as text" is ugly, hard to read and annoying.

    L said:
    is it possible to make it export the data in a row

    Yes, it is. This question is not specific to MicroStation and I am not sure if you tried to search internet for similar topic, because there are plenty of discussions and code snippets available on StackOverflow and other famous programming forums.

    L said:
    i'm getting close to the result that i'm after

    With all respect, the code is not good. Not only because it does not do what you want, but it contains some "code smells" like hard coded array boundaries or opening text file in a cycle for every item to be added.

    L said:
    export the data in a row

    It's default behaviour of Print method that a new line is added. Fortunately Google provide enough correct answers, like this one, so your code should be:

    Print #1, mytxt;

    But it's still bad code, because the file does not contain commas and as I wrote earlier, it's very bad intention to open the file for every write. In my opinion it's better to prepare a whole CSV record (line with all fields) and to write it into a file at once:

    Option Explicit
    
    Public Sub CreateCsvExample()
        Dim BC(1 To 3) As String
        
        BC(1) = "Value 1"
        BC(2) = "Value 2"
        BC(3) = "Value 3"
        
        Dim recordIndex As Integer
        Dim csvRecord As String
        
        For recordIndex = LBound(BC) To UBound(BC)
            AddFieldToRecord csvRecord, BC(recordIndex)
        Next recordIndex
    
        Open "D:\\temp\\test.csv" For Append As #1
        Print #1, csvRecord
        Close #1
    End Sub
    
    Private Sub AddFieldToRecord(record As String, field As String)
        If record <> vbNullString Then
            record = record & ","
        End If
        
        record = record & field
    End Sub

    It can be enhanced further, Print method is primitive and I guess it's better to use FSO (FileSystemObject from Scripting) as demonstrated e.g. in this discussion.

    With regards,

      Jan

Reply
  • Hi,

    for future posts and questions, I recommend to read and follow MicroStation Programming forum best practices, which will help you to provide a standard set of information, that are missing now, like product do you use (is it MicroStation, PowerDraft or something else?), it's version or build number (V8.0 can behave differently than CONNECT Edition) and also to format the subject properly (e.g. "[VBA V8 SS3] Export CSV").

    I also recommend to use Syntaxhighlighter tool (yellow pencil icon in editor) with properly selected language (VB in your case) every time you post any code snippet. It will display the code properly formated and colorized, because "code as text" is ugly, hard to read and annoying.

    L said:
    is it possible to make it export the data in a row

    Yes, it is. This question is not specific to MicroStation and I am not sure if you tried to search internet for similar topic, because there are plenty of discussions and code snippets available on StackOverflow and other famous programming forums.

    L said:
    i'm getting close to the result that i'm after

    With all respect, the code is not good. Not only because it does not do what you want, but it contains some "code smells" like hard coded array boundaries or opening text file in a cycle for every item to be added.

    L said:
    export the data in a row

    It's default behaviour of Print method that a new line is added. Fortunately Google provide enough correct answers, like this one, so your code should be:

    Print #1, mytxt;

    But it's still bad code, because the file does not contain commas and as I wrote earlier, it's very bad intention to open the file for every write. In my opinion it's better to prepare a whole CSV record (line with all fields) and to write it into a file at once:

    Option Explicit
    
    Public Sub CreateCsvExample()
        Dim BC(1 To 3) As String
        
        BC(1) = "Value 1"
        BC(2) = "Value 2"
        BC(3) = "Value 3"
        
        Dim recordIndex As Integer
        Dim csvRecord As String
        
        For recordIndex = LBound(BC) To UBound(BC)
            AddFieldToRecord csvRecord, BC(recordIndex)
        Next recordIndex
    
        Open "D:\\temp\\test.csv" For Append As #1
        Print #1, csvRecord
        Close #1
    End Sub
    
    Private Sub AddFieldToRecord(record As String, field As String)
        If record <> vbNullString Then
            record = record & ","
        End If
        
        record = record & field
    End Sub

    It can be enhanced further, Print method is primitive and I guess it's better to use FSO (FileSystemObject from Scripting) as demonstrated e.g. in this discussion.

    With regards,

      Jan

Children
No Data