469,892 Members | 2,110 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,892 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmd_Save_Click()
  2. Dim strGameName As String
  3. Dim strGenre As String
  4. Dim strAge_Rating As String
  5. Dim strStock As String
  6. Dim strFormat As String
  7. Dim strTextout_1 As String
  8. Dim strTextout_2 As String
  9. Dim strTextout_3 As String
  10. Dim srrTextout_4 As String
  11. Dim strTextout_5 As String
  12.  
  13. With CommonDialog1
  14.     ' Sets the default directory/folder
  15.   .InitDir = "N:\Unit 29"
  16.     ' Sets the Dialog Title to Open File
  17.     .DialogTitle = "Please Save File"
  18.         ' Sets the File List box to Word and Excel documents
  19. 'make sure the following is all on one line
  20.     .Filter = "CSV (*.CSV)|*.CSV"
  21.             ' Set the default files type to Word Documents
  22.     .FilterIndex = 1
  23.         ' Sets the flags - File must exist and Hide Read only
  24.     .Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  25.       ' Set dialog box so an error occurs if the dialog box is cancelled
  26.     .CancelError = True
  27.  
  28. End With
  29.  
  30. ' Enables error handling to catch cancel error
  31. On Error Resume Next
  32. ' display the dialog box
  33. CommonDialog1.ShowSave
  34. If Err Then
  35.     ' This code runs if the dialog was cancelled
  36.     MsgBox "Dialog Cancelled"
  37.     Exit Sub
  38. End If
  39.  
  40. Open CommonDialog1.FileName For Output As #1
  41. Write #1, strGameName, strGenre, strAge_Rating, str_Stock, strFormat
  42. MsgBox "You selected " & CommonDialog1.FileName
  43. strTextout_1 = strGameName
  44. strTextout_2 = strGenre
  45. strTextout_3 = strAge_Rating
  46. srrTextout_4 = strStock
  47. strTextout_5 = strFormat
  48.  
  49. 'Write #1, strTextout_1, strTextout_2, strTextout_3
  50. Close #1
  51.  
  52. End Sub
and this is what I currently have for loading from CSV file to flexgrid:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3. Dim strGameName As String
  4. Dim strGenre As String
  5. Dim strAge_Rating As String
  6. Dim strStock As String
  7. Dim strFormat As String
  8.  
  9. Open "Game Rent" For Output As #1
  10. strTextout_1 = strGameName
  11. strTextout_2 = strGenre
  12. strTextout_3 = strAge_Rating
  13. srrTextout_4 = strStock
  14. strTextout_5 = strFormat
  15. Write #1, strGameName, strGenre, strAge_Rating, str_Stock, strFormat
  16. Close #1
  17.  
  18. 'With CommonDialog1
  19. '    ' Sets the default directory/folder
  20. ' .InitDir = "C:\"
  21. '    ' Sets the Dialog Title to Open File
  22. '    .DialogTitle = "This is my Open File dialog"
  23. '
  24. '    ' Sets the File List box to Word and Excel documents
  25. ''make sure the following is all on one line
  26. '    .Filter = "Notepad (*.txt)|*.txt"
  27. '
  28. '        ' Set the default files type to Word Documents
  29. '    .FilterIndex = 1
  30. '        ' Sets the flags - File must exist and Hide Read only
  31. '    .Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  32. '      ' Set dialog box so an error occurs if the dialog box is cancelled
  33. '    .CancelError = True
  34. 'End With
  35. '
  36. '' Enables error handling to catch cancel error
  37. 'On Error Resume Next
  38. '' display the dialog box
  39. 'CommonDialog1.ShowOpen
  40. 'If Err Then
  41. '    ' This code runs if the dialog was cancelled
  42. '    MsgBox "Dialog Cancelled"
  43. '    Exit Sub
  44. 'End If
  45. '' Displays a message box.
  46.  
  47. Open "Game Rent" For Input As #1
  48. Input #1, strGameName, strGenre, strAge_Rating, str_Stock, strFormat
  49. 'Print strTextout_1, strTextout_2, strTextout_3, strTextout_4, strTextout_5
  50. MSFlexGrid1.TextMatrix(intRowNumber, 0) = strGameName
  51. MSFlexGrid1.TextMatrix(intRowNumber, 1) = strGenre
  52. MSFlexGrid1.TextMatrix(intRowNumber, 2) = str_Age_Rating
  53. MSFlexGrid1.TextMatrix(intRowNumber, 3) = str_Stock
  54. MSFlexGrid1.TextMatrix(intRowNumber, 4) = strFormat
  55.  
  56. Close #1
  57.  
  58. End Sub
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:\Unit 29\Assignment 1", MSFlexGrid1) <------the file name of the CSV file the code is:

Expand|Select|Wrap|Line Numbers
  1. Private Sub saveCSV(ByVal strFilename As String, ByRef msFlex As MSFlexGrid)
  2.     Const SEPARATOR_CHAR As String = ","
  3.  
  4.     Dim intFreeFile As Integer
  5.     Dim strLine As String
  6.     Dim r As Integer
  7.     Dim c As Integer
  8.  
  9.     intFreeFile = FreeFile
  10.  
  11.     Open strFilename For Output As #intFreeFile
  12.  
  13.     With msFlex
  14.         ' Every row
  15.         For r = 0 To .Rows - 1
  16.             strLine = ""
  17.  
  18.             ' Every column
  19.             For c = 0 To .Cols - 1
  20.                 strLine = strLine & IIf(c = 0, "", _
  21.                     SEPARATOR_CHAR) & .TextMatrix(r, c)
  22.             Next c
  23.  
  24.             Print #intFreeFile, strLine
  25.         Next r
  26.     End With
  27.  
  28.     Close #intFreeFile
  29. End Sub
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.
Feb 13 '08 #1
3 5329
Bum
19
'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_CSV()
Dim strMiddleInitial, strLastName, objUser
Combo1.Clear
On Error GoTo errhandler 'Resume Next

strExcelPath = "\Review.csv"'put 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.ListCount - 1
If UCase(Trim(tstr)) = UCase(Trim(Combo1.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.ListIndex = 0
Exit Sub

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

End Sub
Feb 14 '08 #2
Bum
19
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
Feb 14 '08 #3
Killer42
8,435 Expert 8TB
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).
Feb 15 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Deva | last post: by
7 posts views Thread by tombsy | last post: by
reply views Thread by ali mootab | last post: by
1 post views Thread by Bails | last post: by
3 posts views Thread by sweevil | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.