New, Open, Save, Save As, Close

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andras Gilicz

    New, Open, Save, Save As, Close

    Hi VB fans

    I'm working on a relatively large project in VB6 with about a dozen
    forms, including graphs, labels, text boxes, etc. The software itself
    is actually a flow simulator with more or less complex technical
    calculations, several input variables. I would like to equipp the
    starting panel with the usual New, Open, Save, Save As, Close etc.
    menus (like in Excel, or Word, etc.) What is the best way to
    accomplish Save, or Save As? Should be the variables behind the entry
    fields be written to a binary file, or can the forms be saved directly
    with all the controls and it contents to a file, or is there any other
    solution? How professionals do it?

    Any advice is highly appreciated. Tx in advance

    Andras
  • Steve Gerrard

    #2
    Re: New, Open, Save, Save As, Close


    "Andras Gilicz" <agilicz@chello .hu> wrote in message
    news:729e2243.0 409060938.7500b c1b@posting.goo gle.com...
    | Hi VB fans
    |
    | I'm working on a relatively large project in VB6 with about a dozen
    | forms, including graphs, labels, text boxes, etc. The software itself
    | is actually a flow simulator with more or less complex technical
    | calculations, several input variables. I would like to equipp the
    | starting panel with the usual New, Open, Save, Save As, Close etc.
    | menus (like in Excel, or Word, etc.) What is the best way to
    | accomplish Save, or Save As? Should be the variables behind the entry
    | fields be written to a binary file, or can the forms be saved directly
    | with all the controls and it contents to a file, or is there any other
    | solution? How professionals do it?
    |

    I would certainly save the values of the entry variables, not make an
    attempt to save the forms themselves.

    You can choose an ordinary file, or even use an Access database if it
    suits you.

    For a file, I would write all variables in plain text, not in actual
    binary. It makes debugging, etc. much easier. Using Input# and Write# to
    read and write the file will produce this result, and take care of the
    formatting. (Write# does not handle strings with embedded quotes,
    however).




    Comment

    • build

      #3
      Re: New, Open, Save, Save As, Close

      Andras Gilicz wrote:
      [color=blue]
      > Hi VB fans
      >
      > I'm working on a relatively large project in VB6 with about a dozen
      > forms, including graphs, labels, text boxes, etc. The software itself
      > is actually a flow simulator with more or less complex technical
      > calculations, several input variables. I would like to equipp the
      > starting panel with the usual New, Open, Save, Save As, Close etc.
      > menus (like in Excel, or Word, etc.) What is the best way to
      > accomplish Save, or Save As? Should be the variables behind the entry
      > fields be written to a binary file, or can the forms be saved directly
      > with all the controls and it contents to a file, or is there any other
      > solution? How professionals do it?
      >
      > Any advice is highly appreciated. Tx in advance
      >
      > Andras[/color]
      G'day Andras,
      search the Help file for "Using Menus in Your Application" this topic is
      well documened there.
      and/or
      Go to >Tools >Menu Editor
      cheers,
      build

      Comment

      • Andras Gilicz

        #4
        Re: New, Open, Save, Save As, Close

        build <build02@datafa st.net.au> wrote in message news:<413ff59c@ news.alphalink. com.au>...[color=blue]
        > Andras Gilicz wrote:
        >[color=green]
        > > Hi VB fans
        > >
        > > I'm working on a relatively large project in VB6 with about a dozen
        > > forms, including graphs, labels, text boxes, etc. The software itself
        > > is actually a flow simulator with more or less complex technical
        > > calculations, several input variables. I would like to equipp the
        > > starting panel with the usual New, Open, Save, Save As, Close etc.
        > > menus (like in Excel, or Word, etc.) What is the best way to
        > > accomplish Save, or Save As? Should be the variables behind the entry
        > > fields be written to a binary file, or can the forms be saved directly
        > > with all the controls and it contents to a file, or is there any other
        > > solution? How professionals do it?
        > >
        > > Any advice is highly appreciated. Tx in advance
        > >
        > > Andras[/color]
        > G'day Andras,
        > search the Help file for "Using Menus in Your Application" this topic is
        > well documened there.
        > and/or
        > Go to >Tools >Menu Editor
        > cheers,
        > build[/color]

        Ok, tx build, I have no problems with menus, I've got a lot of them,
        but the question was which code to put behind the Save or Save As
        menus, i.e. how professionally the entry field variables on several
        forms can be saved up and reloaded if needed later. How Excel does it
        for e.g.? Steve's solution above is sympathetic, tx Steve!

        regards

        Andras

        Comment

        • Calan

          #5
          Re: New, Open, Save, Save As, Close

          Andras,

          One technique that I've used in the past is to use an Access database (coded
          with ADO in VB), and then put the name of each field in it's corresponding
          input control's Tag property. You can then use a For Each Control type of
          loop to easily get the control's value and store it to the field named in
          it's tag property. The same process works in reverse to load a previously
          saved group of input values.

          To take this a step further, if you have combo boxes with a list of values
          to select from, you can write a generic routine to load the lists and then
          use a text file to store all the values for each listbox. This makes adding
          or removing an item from a listbox a simple matter of editing the text file.
          Again, use the tag property to correlate the control with it's list of
          values in the file.

          Here are some routines I wrote a few years ago to do this. These are copied
          straight from my projects, so you'll need to tweak them, but you'll get the
          idea. (This project stored engineering design data, so records are loaded
          and stored based on a "Design ID" field. It's just an Access AutoNumber
          field.

          (InputDatabase is a global variable that points to the Access database)

          *************** **

          Public Sub LoadDesignFromD B(Activeform As Form, DesignID As Long)

          ' This routine loads the contents of all controls on the main form that
          contain a tag value
          ' from the selected record in the design database

          Dim CurrentControl As Control
          Dim f As ADODB.Field
          Dim Msg As String
          Dim dbInputDatabase As New ADODB.Connectio n
          Dim rs As New ADODB.Recordset
          Dim SQLStatement As String

          On Error GoTo ErrHandler

          SQLStatement = "SELECT * FROM InputData WHERE DesignID = " & DesignID

          Screen.MousePoi nter = vbHourglass

          dbInputDatabase .Open (ProviderString & ";Data Source=" & InputDatabase)
          rs.Open SQLStatement, dbInputDatabase , adOpenKeyset, adLockOptimisti c

          For Each f In rs.Fields
          For Each CurrentControl In Activeform.Cont rols
          If CurrentControl. Tag = f.Name Then
          If f.Type = adBoolean Then
          CurrentControl = Abs(f.Value)
          Else
          CurrentControl = f.Value
          End If
          End If
          Next
          Next

          rs.Close
          dbInputDatabase .Close
          Set rs = Nothing
          Set dbInputDatabase = Nothing

          Screen.MousePoi nter = vbDefault

          MsgBox "Design data loaded sucessfully", vbOKOnly + vbInformation, "Load
          Design Data"

          Exit Sub

          ErrHandler:

          Screen.MousePoi nter = vbDefault
          Select Case Err
          Case Else
          Msg = "Unable to load design data. Error Number: " & Err.Number &
          vbCrLf & Err.Description
          End Select
          MsgBox Msg, vbCritical + vbOKOnly, "Load Design Data"

          On Error Resume Next

          rs.Close
          dbInputDatabase .Close
          Set rs = Nothing
          Set dbInputDatabase = Nothing

          End Sub

          *************** **

          Public Sub SaveDesignToDB( Activeform As Form, SaveMode As String)

          ' This routine saves the contents of all controls on the form that
          contain a tag value
          ' to a the Design database

          Dim Msg As String
          Dim Style As Integer
          Dim Title As String
          Dim Response As Integer

          Dim CurrentControl As Control
          Dim ControlTag As String

          Dim dbInputDatabase As New ADODB.Connectio n
          Dim rs As New ADODB.Recordset

          Dim SQLStatement As String

          On Error GoTo ErrHandler

          ' If DesignID has been reset, save as new; otherwise, update current
          record
          If SaveMode = "NEW" Then
          Msg = "Save current data as new design in database?"
          SQLStatement = "InputData"
          Else
          Msg = "Overwrite current design in database?"
          SQLStatement = "SELECT * FROM InputData WHERE DesignID = " &
          CurrentDesignID & ""
          End If

          Style = vbQuestion + vbOKCancel '+ vbDefaultButton 2
          Title = "Save Design Data"
          Response = MsgBox(Msg, Style, Title)

          If Response <> vbOK Then
          Exit Sub
          End If

          Screen.MousePoi nter = vbHourglass

          dbInputDatabase .Open (ProviderString & ";Data Source=" & InputDatabase)
          rs.Open SQLStatement, dbInputDatabase , adOpenKeyset, adLockOptimisti c

          If SaveMode = "NEW" Then
          rs.AddNew
          End If

          For Each CurrentControl In Activeform.Cont rols
          ControlTag = CurrentControl. Tag
          If ControlTag <> "" Then
          rs.Fields(Contr olTag) = CurrentControl
          End If
          Next
          rs.Update
          rs.MoveFirst

          MsgBox "Design data saved successfully.", vbOKOnly, "Save Design Data"

          rs.Close
          dbInputDatabase .Close
          Set rs = Nothing
          Set dbInputDatabase = Nothing

          Screen.MousePoi nter = vbDefault

          Exit Sub

          ErrHandler:

          Screen.MousePoi nter = vbDefault
          Select Case Err
          Case Else
          Msg = "Unable to save design data. Error Number: " & Err.Number &
          vbCrLf & Err.Description
          End Select
          MsgBox Msg, vbCritical + vbOKOnly, "Save Design Data"

          On Error Resume Next

          rs.Close
          dbInputDatabase .Close
          Set rs = Nothing
          Set dbInputDatabase = Nothing

          End Sub

          *************** **

          Public Sub LoadLists(Activ eform As Form, ListFileName As String)

          ' This routine loads each combo box from the "ListData.d at" file
          ' in the App.Path\config directory. This is a very fast, easy way
          ' of maintaining combo lists.

          ' This function works by searching the main form for a control with an
          ' assigned tag property that matches a tag entered in the listdata file.
          ' If one is found, it's list is populated with the values following the
          tag
          ' entry.

          Dim FileHandle As Integer
          Dim FileName As String
          Dim TextInput As String
          Dim ItemToAdd As String
          Dim Counter As Integer
          Dim Msg As String

          Dim ControlTag As String
          Dim DefaultListInde x As Integer
          Dim CurrentControl As Control

          On Error GoTo ErrHandler

          FileHandle = FreeFile
          FileName = App.Path & "\config\ListDa ta.dat"

          ' Load lists for combo boxes
          Open FileName For Input As #1
          Do Until EOF(FileHandle)
          Line Input #FileHandle, TextInput
          If UCase(TextInput ) = "CONTROL" Then
          ItemToAdd = ""
          Input #FileHandle, ControlTag, DefaultListInde x
          For Each CurrentControl In Activeform.Cont rols
          If CurrentControl. Tag = ControlTag Then
          Do Until UCase(ItemToAdd ) = "EOL"
          Line Input #FileHandle, ItemToAdd
          If UCase(ItemToAdd ) = "EOL" Then Exit Do
          CurrentControl. AddItem ItemToAdd
          Loop
          CurrentControl. ListIndex = DefaultListInde x
          Exit For
          End If
          Next
          End If
          Loop
          Close #FileHandle

          Exit Sub

          ErrHandler:

          Close #FileHandle
          Screen.MousePoi nter = vbDefault
          Select Case Err
          Case Else
          Msg = "Error in ListData.dat file. " & Err.Description & "."
          End Select
          MsgBox Msg, vbCritical + vbOKOnly, "Load List Controls"

          End Sub

          *************** **

          Sample ListData.dat file (greatly shortened):

          *************** **

          Combo/List control data file for Visual Basic projects.
          Only the lines following the word "CONTROL" and up to "EOL"
          are read. The format is as follows:

          CONTROL
          control tag, default list index
          list data 1
          list data 2
          ....
          ....
          EOL

          -----------------------------------------------

          ----State----
          CONTROL
          "State",0
          AL
          AK
          AZ
          AR
          CA
          WV
          WI
          WY
          EOL

          ----Nominal OD----
          CONTROL
          "NominalOD" ,18
          3.00
          4.00
          4.13
          4.25
          48.00
          EOL

          ----Bore Dia----
          CONTROL
          "BoreDia",5
          1.13
          1.50
          2.00
          EOL

          *************** **

          Enjoy!

          Calan




          Comment

          Working...