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.
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.
Comment