visual basic 6 dialog box code needed to work save as and open dialog boxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • melgringo
    New Member
    • Jan 2012
    • 2

    visual basic 6 dialog box code needed to work save as and open dialog boxes

    Hello I need the codes to make the save and open dialog boxes work in my programme. I have got to the stage where I can show the save as dialogue box, but when I hit the save button, it doesnt actually save anything to c:/drive. I cant understand why. I am trying to write a football system programme and I need to save my project to enter information on my teams on different days and I need a way to save all the data in the text boxes and not start from scratch every time I want to use it. Any help as to what code I should be writing would be greatly appreciated.
    Thanks in advance.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    The common dialogue control doesn't save anything. It provides the interface which allows the user to select a file, and returns the result to your code. What happens then is up to you.

    Comment

    • melgringo
      New Member
      • Jan 2012
      • 2

      #3
      save as and open dialogue boxes

      Originally posted by Killer42
      The common dialogue control doesn't save anything. It provides the interface which allows the user to select a file, and returns the result to your code. What happens then is up to you.
      Hi killer 42, dont quite understand your reply. Are you saying that the dialogue boxes dont save or open anything ? Cant see the point of them if this is the case. I thought if I put the right code in the save control, it would save my work. Is there any way I can save my data in the 80 text boxes I have in my form, so that I dont have to start from scratch each time I close my vb programme. I would want to save all the info I enter into my text boxes, then when I return,say next day after switching my computer off, I can open the programme and the data is there that I previously entered. I am finding when I come out of vb6 and my programme, when I open up the programme again, all the text boxes are returned to blank. Many thanks for your help as I am quite new to all this.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        That's right. The common dialog control doesn't actually have anything to do with loading or saving. It provides a file-selection dialog for the user to choose a file, and return its name to your program. What you do with the file is up to you.

        If you think about it, to be able to save or load your data, it would need to know all about your data; what goes in the file, in what order, in what format, where to put it when you load it back, and so on.

        In your particular case, it sounds as though you probably don't even need this file selection function - you could probably just make up and use a known file name in your code.

        I'll whip up a quick sample (this is in VB6 - what version are you using?) which will accept a form and a file name as parameters. It will look through the controls on the form, and save the contents of each textbox into the file. (I'll do a second routine to read them back, of course).

        Give me a few minutes...

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Ok, perhaps more than a few. Anyway...

          Just create a new module in a VB6 project, and paste this code into it (replace the entire module).
          Code:
          Option Explicit
          DefLng A-Z
          
          ' A pair of routines developed for Bytes.com by member Killer42, 19/01/2012
          ' All care has been taken, but no responsibility is accepted.
          ' Use at your own risk.
          ' May be freely copied, shared and modified.
          ' Provided as a simple demonstration for other developers to build on.
          
          ' SaveTextBoxes accepts a file name and a form. It scans the form for
          ' text boxes and saves their contents (just the text, not any other properties
          ' of the control) to the file.
          
          ' LoadTextBoxes is a complementary routine which will read the specified file,
          ' and load the text back into the controls from which it was saved.
          ' Note it will only work fully if the textbox controls on the form are the
          ' same as when the values were saved. If they aren't, I don't think it
          ' will do anything nasty, but it won't be very successful either.
          
          ' These routines are self-contained, except that they rely on the two
          ' private functions defined further down. These determine whether the
          ' specified control is part of a control array, and generate a string
          ' which is saved in the file to identify the control (and index) from
          ' which data was saved.
          
          
          Public Sub SaveTextBoxes(ByVal FileName As String, frm As Form)
            ' Save into a text file the values of all textbox controls on this form.
          
            Dim ctl As Control
            Dim FileNum As Long
            Dim TempName As String, TempValue As String
          
            If FileName = "" Then
              Beep
              Exit Sub
            End If
            
            FileNum = FreeFile() ' Use next available file number.
            Open FileName For Output Access Write Lock Write As #FileNum
            Close #FileNum
            Open FileName For Binary Access Write Lock Write As #FileNum
            For Each ctl In frm.Controls
              If TypeOf ctl Is TextBox Then
                TempName = ControlName(ctl)
                TempValue = ctl.Text
                Put #FileNum, , CLng(Len(TempName))
                Put #FileNum, , TempName
                Put #FileNum, , CLng(Len(TempValue))
                Put #FileNum, , TempValue
              End If
            Next
            Close #FileNum
          End Sub
          
          
          
          Public Sub LoadTextBoxes(ByVal FileName As String, frm As Form)
            ' Read a text file, load the values back into the textbox
            ' controls they were saved from. Will only work properly if
            ' the controls are the same (name and index) as when saved.
          
            Dim FileNum As Long
            Dim ctl As Control
            Dim TempLen As Long
            Dim TempName As String, TempValue As String
            Dim TextBoxDetails As New Collection
            Dim ErrNum As Long
          
            If FileName = "" Then
              Beep
              Exit Sub
            End If
          
            FileNum = FreeFile() ' Use next available file number.
            On Error Resume Next
            Err.Clear
            Open FileName For Input Access Read Shared As #FileNum
            ErrNum = Err.Number
            On Error GoTo 0 ' Turn off error handling.
            Select Case ErrNum
              Case 0    ' No error.
                Close #FileNum
              Case 53   ' File not found.
                Beep
                Exit Sub
              Case Else ' Any other error.
                Err.Raise ErrNum ' Repeat the error so it can be dealt with.
            End Select
            
            Open FileName For Binary Access Read Shared As #FileNum
            
            ' Read all the name/value pairs form the file, load into a collection.
            Do Until Seek(FileNum) >= LOF(FileNum)
              Get #FileNum, , TempLen
              TempName = Space$(TempLen)
              Get #FileNum, , TempName
              Get #FileNum, , TempLen
              TempValue = Space$(TempLen)
              Get #FileNum, , TempValue
              TextBoxDetails.Add TempValue, TempName
            Loop
            Close #FileNum
            
            For Each ctl In frm.Controls
              If TypeOf ctl Is TextBox Then
                On Error Resume Next
                ctl.Text = TextBoxDetails(ControlName(ctl))
                On Error GoTo 0
              End If
            Next
          
            Do While TextBoxDetails.Count
              TextBoxDetails.Remove 1
            Loop
          
          End Sub
          
          
          
          
          Private Function ControlIsAnArray(src As Control) As Boolean
            Static TempIndex As Long
            On Local Error GoTo Whoops
            TempIndex = src.Index
            ControlIsAnArray = True
            Exit Function
          
          Whoops:
            If Err.Number = 343 Then ' Error 343 = control is not an array!
              On Error GoTo 0
              ControlIsAnArray = False
              Exit Function
            Else
              ' Some other error happened. Let VB deal with it normally.
              On Error GoTo 0
              Resume
            End If
          End Function
          
          
          
          
          Private Function ControlName(src As Control) As String
            ' The control name written to the file will be in the format: NNNXXXX
            '   NNN indicates the index if it's a control array, otherwise "@@@".
            '   XXXX indicates the actual name of the control.
            ' For example the textbox Text7(15) would produce "015Text7".
            
            If ControlIsAnArray(src) Then
              ControlName = Format$(src.Index, "000")
            Else
              ControlName = "@@@"
            End If
            ControlName = ControlName & src.Name
          End Function
          To try it out, just create a form (or go to any existing form), and create a couple of command buttons - for convenience, let's set the captions to "Save" and "Load". In the Click events for these buttons, put these two commands respectively...
          Code:
          SaveTextBoxes "MyTextBoxes.txt", Me
          
          LoadTextBoxes "MyTextBoxes.txt", Me
          Note I've just arbitrarily made up the file name "MyTextboxes.tx t". It's up to you what you call the file. Make sure the form has some textbox controls on it, and have a play. Whatever's in the textboxes when you hit Save should reappear (wiping out whatever else is there, so take care) when you hit Load.

          A nice touch that you might add is to have these two routines call a common dialog control to prompt the user for a file name if the FileName parameter is empty, instead of just beeping and returning.

          Comment

          Working...