Results 1 to 10 of 10

Thread: Export VB6 MsFlexgrid to NotePad

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    28

    Export VB6 MsFlexgrid to NotePad

    Hello,

    Before, i had an excel spreadsheet that could export data to notepad and transferted data in a text file using VBA.

    Now that i use VB6 and a MsFlexgrid to do the job, i want to transfert the vba Script to my VB6 project. That his to transfer the data from the MsFlexgrid to a text file using the same parameters.
    HTML Code:
     vFieldArray = Array(1, 1, 12, 6, 6, 12, 12, 11, 3, 12, 12, 12, 12, 12, 1, 20, 12, 1, 1, 3, 1, 12, 20, 11, 20, 7, 20, 11, 1, 12, 12, 12, 12, 1, 20, 12, 12, 12, 1, 50, 12, 75, 1, 1, 12, 6, 6, 9, 6, 6, 12, 5, 35, 1, 50)


    But i need help on that.

    Could you help me please?

    My VBA Script was:
    HTML Code:
    Private Sub CommandButton2_Click()
     Const DELIMITER As String = "" 'Normally none
            Const PAD As String = " "   'or other character
            Dim vFieldArray As Variant
            Dim myRecord As Range
            Dim nFileNum As Long
            Dim i As Long
            Dim sOut As String
            
            'vFieldArray contains field lengths, in characters, from field 1 to N
            vFieldArray = Array(1, 1, 12, 6, 6, 12, 12, 11, 3, 12, 12, 12, 12, 12, 1, 20, 12, 1, 1, 3, 1, 12, 20, 11, 20, 7, 20, 11, 1, 12, 12, 12, 12, 1, 20, 12, 12, 12, 1, 50, 12, 75, 1, 1, 12, 6, 6, 9, 6, 6, 12, 5, 35, 1, 50)
            nFileNum = FreeFile
            Open "C:\Documents and Settings\All Users\Desktop\ordtemp." For Output As #nFileNum
            For Each myRecord In Range("A8:A" & _
                    Range("A" & Rows.Count).End(xlUp).Row)
                With myRecord
                    For i = 0 To 41
                        sOut = sOut & DELIMITER & Left(.Offset(0, i).Text & _
                                String(vFieldArray(i), PAD), vFieldArray(i))
                    Next i
                    Print #nFileNum, Mid(sOut, Len(DELIMITER) + 1)
                    sOut = Empty
                    For i = 42 To UBound(vFieldArray)
                        sOut = sOut & DELIMITER & Left(.Offset(0, i).Text & _
                                String(vFieldArray(i), PAD), vFieldArray(i))
                    Next i
                    Print #nFileNum, Mid(sOut, Len(DELIMITER) + 1)
                    sOut = Empty
                End With
            Next myRecord
            Close #nFileNum
            
           
            
            
     ' Open our source file
      f1 = FreeFile
      Open "C:\Documents and Settings\All Users\Desktop\ordtemp." For Input As #f1
    
      ' Open our target file
      f2 = FreeFile
      Open "C:\Documents and Settings\All Users\Desktop\ord." For Output As #f2
    
      ' Start processing till we reach the end
      Do While Not EOF(f1)
        ' Read a line from the source file
        Line Input #f1, x
    
        ' And if the line isn't empty, write it to the target file
        If x <> "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            " Then
          If x <> "                                                                                                                                                      " Then
          Print #f2, x
        End If
        End If
      Loop
    
      ' Close the two opened files
      Close #f2
      Close #f1
            
            'Delete the unecessary temp file
            strFile = "C:\Documents and Settings\All Users\Desktop\ordtemp."
    
            Set fs = CreateObject("Scripting.FileSystemObject")
    
            fs.deletefile strFile
    
    
            
             'This formats name of the text file to be ord.YYYYMMDDHHMI
            OldFile1 = "C:\Documents and Settings\All Users\Desktop\ord."
            newfile1 = Format(Now, "yyyymmddhhmmss")
            newfile1 = OldFile1 & newfile1
            Name OldFile1 As newfile1
            
    
            MsgBox "File Creation Complete"
    End Sub

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Export VB6 MsFlexgrid to NotePad

    I will take the same example that I gave you in the previous thread

    Hope this helps...

    vb Code:
    1. Private Sub Form_Load()
    2.    '~~> Declare variables.
    3.    Dim Entry As String, i As Long
    4.    
    5.    MSFlexGrid1.FixedCols = 0
    6.    MSFlexGrid1.FixedRows = 0
    7.    
    8.    MSFlexGrid1.Cols = 1
    9.    '~~> To add 10 items
    10.    For i = 1 To 10
    11.       Entry = "This is item number : " & i
    12.       MSFlexGrid1.AddItem Entry
    13.    Next i
    14. End Sub
    15.  
    16. Private Sub Command1_Click()
    17.     Dim intRow As Integer, intCol As Integer
    18.    
    19.     '~~> Change to target File
    20.     FileName = "C:\Test.Txt"
    21.    
    22.     iFreeFile = FreeFile
    23.    
    24.     '~~> Open File for output
    25.     Open FileName For Output As iFreeFile
    26.    
    27.     '~~> Get the rows and Columns of Flex
    28.     intCol = MSFlexGrid1.Cols
    29.     intRow = MSFlexGrid1.Rows
    30.  
    31.     For i = 1 To intRow
    32.         For j = 1 To intCol
    33.             With MSFlexGrid1
    34.                 '~~> Write to text file
    35.                 Print #iFreeFile, .TextMatrix(i - 1, j - 1) & " "
    36.             End With
    37.         Next
    38.     Next
    39.  
    40.     MsgBox "File Exported Successfully"
    41.     Close #iFreeFile
    42. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    28

    Re: Export VB6 MsFlexgrid to NotePad

    I have an error on "FileName":
    HTML Code:
    #
    '~~> Change to target File
    #
       FileName = "C:\Test.Txt"
    I did created the file before.
    Last edited by avyrex1926; Apr 5th, 2009 at 12:34 PM.

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Export VB6 MsFlexgrid to NotePad

    what is the code that you are using?
    What is the error that you are getting?

    if you are using Option Explicit, have you declared

    Dim FileName as String

    ???
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    28

    Re: Export VB6 MsFlexgrid to NotePad

    I put that code:
    Code:
    Dim FileName As String
    Now i have:

    Compile error
    Variable not defined

    at this section of the script:
    Code:
     For i = 1 To intRow

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Export VB6 MsFlexgrid to NotePad

    I though you will dim your variables

    okay do this....

    Dim i as Long, j as long
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    28

    Re: Export VB6 MsFlexgrid to NotePad

    Thanks, I will try it Friday and I'll give you the results.

    Thanks again.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    28

    Re: Export VB6 MsFlexgrid to NotePad

    Hello again,

    I did the modification and now theres a error on:

    Code:
      '~~> Open File for output
    
        Open FileName For Output As iFreeFile
    Run-Time error 75:
    Path/File access error.

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Export VB6 MsFlexgrid to NotePad

    Check if the file exists... If it exists, ensure that it is not "read only"

    Try it with a different filename for example

    FileName = "C:\avyrex1926.Txt"

    If the above doesn't work then show me your complete code...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    28

    Re: Export VB6 MsFlexgrid to NotePad

    Good

    I've created a new text file and it transfer the data.

    Now the only thing that I need to fix his to have all information on the same line.

    I'll explain.

    It have to fit these parameters.

    Code:
    vFieldArray = Array(1, 1, 12, 6, 6, 12, 12, 11, 3, 12, 12, 12, 12, 12, 1, 20, 12, 1, 1, 3, 1, 12, 20, 11, 20, 7, 20, 11, 1, 12, 12, 12, 12, 1, 20, 12, 12, 12, 1, 50, 12, 75, 1, 1, 12, 6, 6, 9, 6, 6, 12, 5, 35, 1, 50)
    So lets say that in the grid, column 1, you have the value: H
    Column 2 the value: A.
    Column 3: Text
    Column 4: Test1.

    The final result would be in notepad: HAText Test1

    Code:
    vFieldArray = Array(1, 1, 12, 6)
    HAText Test1 :
    H = use 1 space.
    A = Use 1 space.
    Text = Use 12 space (Text followed by space).
    Test1 = Use 6 space (Test1 followed by 1 space) etc.

    How can I do that.

    Thanks for you precious help.

    avyrex

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width