Option Explicit 'Note: 'George spotted the SDI Grid at: 'http://www.codeguru.com/Cpp/controls/controls/gridcontrol/article.php/c2283/ 'SDIGrid.ocx is included in the source code download on that page. ' 'Also see http://www.devguru.com/Technologies/vbscript/quickref/vbscript_intro.html 'for VBScript reference - esp file handling 'There is only limited error checking in this code. 'Robert Slack December 04 'Mode is Input (1), Output (2), Random (4), Append (8), or Binary (32) 'Some File Mode Constants Dim forReading,forWriting,forAppending forReading = 1 forWriting = 2 forAppending = 8 '--------------------------------------------------------------- Sub Main() On Error Resume Next Output.Caption = "SDI Grid Example" If TypeName(commondialog1)<>"ICommonDialog" Then MsgBox "Common Dialog Control (comdlg32.ocx) not installed. TypeName(commondialog1)=" & TypeName(commondialog1) End If 'Set the size of the Grid sdigrid1.numrows=500 sdigrid1.numcols=7 End Sub '--------------------------------------------------------------- Sub cmdOpen_Click 'Browse for a comma delimited file to open Dim FileName FileName=ShowOpenDialogue If FileName="" Then Exit Sub ReadTheFileIntoTheGrid FileName End Sub '--------------------------------------------------------------- Function ShowOpenDialogue 'Uses the commonDialog control Dim strTitleOpen Dim strFilters Dim strAppTitle strFilters = "Comma separated files [*.csv]|*.csv|All Files (*.*)|*.*" commondialog1.DialogTitle = "Select a comma delimited Text File" commondialog1.filename="DataTest.csv" commondialog1.Filter = strFilters commondialog1.ShowOpen ShowOpenDialogue=commondialog1.FileName End Function '--------------------------------------------------------------- Sub cmdSave_Click 'Save the Grid data to comma delimited text file Dim FileSystem,File Dim DataFile,FileHandle,RowNumber,RecordData On Error Resume Next DataFile=ShowSaveDialog If DataFile="" Then Exit Sub If isempty(FileSystem) Then Set FileSystem = CreateObject("Scripting.FileSystemObject") End If If isempty(File) Then Set file = FileSystem.OpenTextFile(DataFile, forWriting, True) End If For Rownumber=1 to sdiGrid1.numrows-1 RecordData=ReadGrid(RowNumber) file.WriteLine(RecordData) Next File.Close Set FileSystem=Nothing Set file=Nothing If err<>0 Then MsgBox err.description,"","In cmdSave_Click" End Sub '--------------------------------------------------------------- Function ShowSaveDialog 'Uses the commonDialog control Dim strTitleSave Dim strFilters Dim strAppTitle strTitleSave = "Save Text File" strAppTitle = "" strFilters = "Comma separated files [*.csv]|*.csv|All Files (*.*)|*.*" commondialog1.DialogTitle = strTitleSave commondialog1.Filter = strFilters commondialog1.filename="DataTest.csv" commondialog1.ShowSave ShowSaveDialog=commondialog1.filename End Function '--------------------------------------------------------------- Function ReadGrid(RowNumber) 'Read a row of the grid & add data to a comma delimited record Dim TextLine,ColNumber On Error Resume Next TextLine=sdigrid1.getcellcontents(RowNumber,1) For ColNumber=2 to sdigrid1.numcols-1 TextLine=TextLine & "," & sdigrid1.getcellcontents(RowNumber,ColNumber) Next If err<>0 Then MsgBox err.description ReadGrid=TextLine End Function '--------------------------------------------------------------- Sub cmdPopulateGrid_Click 'Put something in the grid Dim i,j UpdateScreen DoEvents For i=1 to sdiGrid1.NumRows For j=1 to sdigrid1.numcols-1 sdigrid1.SetCellContents i,j,FORMATNUMBER(i+j-1,0) Next Next UpdateScreen DoEvents End Sub '--------------------------------------------------------------- Sub cmdClearGrid_Click sdigrid1.cleargrid updatescreen End Sub '--------------------------------------------------------------- Sub ReadTheFileIntoTheGrid(FileName) 'Read a comma delimited file into the grid Dim FileSystem,File Dim RecordData,RecordFields Dim cnt,i If isempty(FileSystem) Then Set FileSystem = CreateObject("Scripting.FileSystemObject") End If If isempty(file) Then Set file = FileSystem.OpenTextFile(FileName, forReading, False) End If If err<>0 Then MsgBox "2 " & err.description Do until file.atendofstream Cnt=Cnt+1 RecordData=file.ReadLine RecordFields = Split(RecordData,",") For i=0 to ubound(RecordFields) sdigrid1.SetCellContents cnt,i+1, RecordFields(i) Next Loop file.Close Set FileSystem=Nothing Set file=Nothing UpdateScreen DoEvents End Sub '--------------------------------------------------------------- Sub frmMain_Load 'This doesn't seem to be doing anything? frmMain.height=390 frmMain.width=625 doevents updatescreen End Sub '--------------------------------------------------------------- Sub mnuAbout_Click Dim msg msg="Demonstration of:" & vbcrlf & vbcrlf & "SDI Grid & Text file handling." & vbcrlf & vbcrlf & "R Slack. Dec 04" MsgBox msg,vbOkOnly,"About" End Sub '--------------------------------------------------------------- Sub cmdExit_Click 'Perhaps should check whether to save the file Bye End Sub