getting data from CSV file into a Flexgrid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ArmageddonAsh
    New Member
    • Feb 2008
    • 1

    getting data from CSV file into a Flexgrid

    I'm trying to make an application that will allow the user to enter data into a flexgrid (that's done) and then save the data from that flexgrid into a CSV file but even though the file is made none of the data from the flexgrid goes into the CSV file and so I have a couple of questions.

    1, How do I make the application load the data from a specific CSV file?
    2, How do I make it so that the user is able to add information to the data and then save that information and overwrite the current CSV file?
    3, How do I make it so that I can delete data from the flexgrid and make all the information below move up (so there is no space where the information used to be)?

    This is the code I currently have for saving the data from the flexgrid into a CSV file:

    [CODE=vb]Private Sub cmd_Save_Click( )
    Dim strGameName As String
    Dim strGenre As String
    Dim strAge_Rating As String
    Dim strStock As String
    Dim strFormat As String
    Dim strTextout_1 As String
    Dim strTextout_2 As String
    Dim strTextout_3 As String
    Dim srrTextout_4 As String
    Dim strTextout_5 As String

    With CommonDialog1
    ' Sets the default directory/folder
    .InitDir = "N:\Unit 29"
    ' Sets the Dialog Title to Open File
    .DialogTitle = "Please Save File"
    ' Sets the File List box to Word and Excel documents
    'make sure the following is all on one line
    .Filter = "CSV (*.CSV)|*.CSV"
    ' Set the default files type to Word Documents
    .FilterIndex = 1
    ' Sets the flags - File must exist and Hide Read only
    .Flags = cdlOFNFileMustE xist + cdlOFNHideReadO nly
    ' Set dialog box so an error occurs if the dialog box is cancelled
    .CancelError = True

    End With

    ' Enables error handling to catch cancel error
    On Error Resume Next
    ' display the dialog box
    CommonDialog1.S howSave
    If Err Then
    ' This code runs if the dialog was cancelled
    MsgBox "Dialog Cancelled"
    Exit Sub
    End If

    Open CommonDialog1.F ileName For Output As #1
    Write #1, strGameName, strGenre, strAge_Rating, str_Stock, strFormat
    MsgBox "You selected " & CommonDialog1.F ileName
    strTextout_1 = strGameName
    strTextout_2 = strGenre
    strTextout_3 = strAge_Rating
    srrTextout_4 = strStock
    strTextout_5 = strFormat

    'Write #1, strTextout_1, strTextout_2, strTextout_3
    Close #1

    End Sub[/CODE]
    and this is what I currently have for loading from CSV file to flexgrid:

    [CODE=vb]Private Sub Form_Load()

    Dim strGameName As String
    Dim strGenre As String
    Dim strAge_Rating As String
    Dim strStock As String
    Dim strFormat As String

    Open "Game Rent" For Output As #1
    strTextout_1 = strGameName
    strTextout_2 = strGenre
    strTextout_3 = strAge_Rating
    srrTextout_4 = strStock
    strTextout_5 = strFormat
    Write #1, strGameName, strGenre, strAge_Rating, str_Stock, strFormat
    Close #1

    'With CommonDialog1
    ' ' Sets the default directory/folder
    ' .InitDir = "C:\"
    ' ' Sets the Dialog Title to Open File
    ' .DialogTitle = "This is my Open File dialog"
    '
    ' ' Sets the File List box to Word and Excel documents
    ''make sure the following is all on one line
    ' .Filter = "Notepad (*.txt)|*.txt"
    '
    ' ' Set the default files type to Word Documents
    ' .FilterIndex = 1
    ' ' Sets the flags - File must exist and Hide Read only
    ' .Flags = cdlOFNFileMustE xist + cdlOFNHideReadO nly
    ' ' Set dialog box so an error occurs if the dialog box is cancelled
    ' .CancelError = True
    'End With
    '
    '' Enables error handling to catch cancel error
    'On Error Resume Next
    '' display the dialog box
    'CommonDialog1. ShowOpen
    'If Err Then
    ' ' This code runs if the dialog was cancelled
    ' MsgBox "Dialog Cancelled"
    ' Exit Sub
    'End If
    '' Displays a message box.

    Open "Game Rent" For Input As #1
    Input #1, strGameName, strGenre, strAge_Rating, str_Stock, strFormat
    'Print strTextout_1, strTextout_2, strTextout_3, strTextout_4, strTextout_5
    MSFlexGrid1.Tex tMatrix(intRowN umber, 0) = strGameName
    MSFlexGrid1.Tex tMatrix(intRowN umber, 1) = strGenre
    MSFlexGrid1.Tex tMatrix(intRowN umber, 2) = str_Age_Rating
    MSFlexGrid1.Tex tMatrix(intRowN umber, 3) = str_Stock
    MSFlexGrid1.Tex tMatrix(intRowN umber, 4) = strFormat

    Close #1

    End Sub[/CODE]

    Both sets of code have commented out sections as I was trying to find out why it wouldn't work.

    I found the following code but I'm not sure how it works as I don't know where to put : SaveCSV("N:\Uni t 29\Assignment 1", MSFlexGrid1) <------the file name of the CSV file the code is:

    [CODE=vb]Private Sub saveCSV(ByVal strFilename As String, ByRef msFlex As MSFlexGrid)
    Const SEPARATOR_CHAR As String = ","

    Dim intFreeFile As Integer
    Dim strLine As String
    Dim r As Integer
    Dim c As Integer

    intFreeFile = FreeFile

    Open strFilename For Output As #intFreeFile

    With msFlex
    ' Every row
    For r = 0 To .Rows - 1
    strLine = ""

    ' Every column
    For c = 0 To .Cols - 1
    strLine = strLine & IIf(c = 0, "", _
    SEPARATOR_CHAR) & .TextMatrix(r, c)
    Next c

    Print #intFreeFile, strLine
    Next r
    End With

    Close #intFreeFile
    End Sub[/CODE]

    Any help towards making the CSV data to be loaded into the Flexgrid or saving the data from the flexgrid to the CSV file would be very grateful.
    Last edited by Killer42; Feb 15 '08, 02:19 AM. Reason: Added CODE=vb tags
  • Bum
    New Member
    • Jan 2008
    • 19

    #2
    'herer's an example of getting the data from a csv and putting in a flexgrid:
    'keep in mind this was mad for MY csv file, you must customize it for yours.

    Public Sub Initial_Read_CS V()
    Dim strMiddleInitia l, strLastName, objUser
    Combo1.Clear
    On Error GoTo errhandler 'Resume Next

    strExcelPath = "\Review.csv"'p ut your path here


    ' Iterate through the rows of the spreadsheet after the first, until the
    ' first blank entry in the first column. For each row, bind to the user
    ' specified in the first column and set attributes.

    FG.Clear
    FG.FormatString = " |<Item No |<Officer |<Issue |<Suspense |<Notes |<Status|" '<Category |"

    Dim intcount
    intcount = 0

    'reset flexgrid
    FG.Cols = 7
    FG.Rows = 1


    'cycle through the appropriate sheets
    Combo1.AddItem "TOGETHER"
    ReDim AllMatrix(7, 100000)

    intRow = 2

    Open strExcelPath For Input As #1

    ''remove headings
    Input #1, stritemno
    Input #1, strofficer '= objSheet.Cells( intRow, 2).Value
    Input #1, strIssue '= objSheet.Cells( intRow, 3).Value
    Input #1, strSuspense '= objSheet.Cells( intRow, 4).Value
    Input #1, strStatus '= objSheet.Cells( intRow, 5).Value
    Input #1, strOCT '= objSheet.Cells( intRow, 6).Value

    Do Until EOF(1)
    Input #1, stritemno
    Input #1, strofficer '= objSheet.Cells( intRow, 2).Value
    Input #1, strIssue '= objSheet.Cells( intRow, 3).Value
    Input #1, strSuspense '= objSheet.Cells( intRow, 4).Value
    Input #1, strStatus '= objSheet.Cells( intRow, 5).Value
    Input #1, strOCT '= objSheet.Cells( intRow, 6).Value
    strsheet = "ALL"
    'Input #1, tstr

    intcount = intcount + 1
    AllMatrix(1, intcount) = stritemno
    AllMatrix(2, intcount) = strofficer
    AllMatrix(3, intcount) = strIssue
    AllMatrix(4, intcount) = strSuspense
    AllMatrix(5, intcount) = strStatus
    AllMatrix(6, intcount) = strsheet
    AllMatrix(7, intcount) = strOCT

    ''have to add the space so vb doesn't treat it like a number for the sorting
    FG.AddItem " " & intcount & vbTab & stritemno & vbTab & strofficer & vbTab & strIssue & vbTab & strSuspense & vbTab & strStatus & vbTab & strOCT
    intRow = intRow + 1

    ''now let's see if we need to add somehting to the combo
    j = 0
    i = InStr(j + 1, strofficer, "/")
    If i = 0 Then
    tstr = strofficer
    GoTo 11:
    End If

    i = 1
    mthano = 0
    Do Until i = 0
    k = 0
    j = i
    i = InStr(j + 1, strofficer, "/")
    mthano = mthano + 1
    If mthano > 1 Then k = 1
    If i <> 0 Then tstr = Mid(strofficer, j + k, i - j - k)
    If i = 0 Then tstr = Mid(strofficer, j + k, Len(strofficer) - j)

    '''check to see if our tstr is in our combo list
    11:
    For ccount = 1 To Combo1.ListCoun t - 1
    If UCase(Trim(tstr )) = UCase(Trim(Comb o1.List(ccount) )) Then
    GoTo 54
    End If
    Next
    Combo1.AddItem UCase(Trim(tstr ))
    54:
    Loop
    Loop

    Close (1)


    ReDim Preserve AllMatrix(7, intcount)
    MatCount = intcount
    CurFlexCount = MatCount
    Combo1.ListInde x = 0
    Exit Sub

    errhandler:
    Close (1)
    MsgBox ("Problem, tell Brad")

    End Sub

    Comment

    • Bum
      New Member
      • Jan 2008
      • 19

      #3
      I also have code to put a textbox in front of the grid being used and then it replaces the data. If you post your email, I will email you the whole project.

      Hope it helps,

      B

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by Bum
        I also have code to put a textbox in front of the grid being used and then it replaces the data. If you post your email, I will email you the whole project.
        If you want to exchange e-mail addresses, do it via Private Message. The site Posting Guidelines prohibit posting your e-mail address in the forum. This is to protect you from scammers and spammers.

        If anyone is interested, I am working on a VB function which will accept a line of CSV formatted data and return the fields as a string array. Not quite finished, but should be working in a few days. (Not that there's much to it, I just don't have time to work on it right now).

        Comment

        Working...