• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 131
  • Last Modified:

CREATING A SAVE BUTTON

I have a form that does certain things. Every time you exit the program the form clears and next time you enter the program theres no data there. I want to make it so that when the user clicks on the save menu command, the form they are working on is somehow saved, and if they want to work on a new form, they can click on new (and a copy of the origional form is made or however it is done), and if they press on save there will now be 2 saved forms, and when they run the program again, they can somehow access either form.    

this would really help me if anyone has the answer, please comment.
Thanks in advance.

--Krazy
0
CRAZY
Asked:
CRAZY
  • 3
  • 2
1 Solution
 
mcriderCommented:
Save all of your fields to a file.  Then you can load the fields back at a later time by reading the file.


Cheers!
0
 
CRAZYAuthor Commented:
That is an option, but I don't know how to do that. If anyone can tell me how i'd appreciate that information as well.
0
 
CRAZYAuthor Commented:
That is an option, but I don't know how to do that. If anyone can tell me how i'd appreciate that information as well.
0
 
mcriderCommented:
Paste the following code into a module and you will be able to save all of the data on a specified form to a file using the SaveFormData function and load it again using the LoadFormData function.

The functions will save/load the TEXT, VALUE, or CAPTION information for all types of controls (except CommandButtons), no matter how many controls you have on the form.


To save the control information do:

     SaveFormData Form1, "c:\windows\desktop\fsave1.txt"

To load the control information again, do:

     LoadFormData Form1, "c:\windows\desktop\fsave1.txt"

Hope this helps you!  By the way, I think this answer is worth more than 20 points...


THE CODE:

'-------------------------------------------------------------------------------
Sub LoadFormData(FormName As Form, lPath As String)
    Dim Ctrl As Control
    Dim fNum As Long
    Dim lfName As String
    Dim lName As String
    Dim lIndex As Long
    Dim ltIndex As Long
    Dim lVal As Variant
    Dim lType As Integer
    Dim lRec As String
    On Error Resume Next
    fNum = FreeFile
    Open lPath For Input Access Read As fNum
    While Not EOF(fNum)
        Line Input #fNum, lRec
        lfName = StringGet(lRec, "|", 1)
        lType = CInt(StringGet(lRec, "|", 2))
        lVal = StringGet(lRec, "|", 3)
        If Not InStr(1, lfName, ">") = 0 Then
            lName = StringGet(lfName, ">", 1)
            lIndex = CLng(StringGet(lfName, ">", 2))
        Else
            lName = lfName
            lIndex = -1
        End If
        For Each Ctrl In FormName.Controls
            With Ctrl
                If .Name = lName Then
                    Err = 0: ltIndex = .Index
                    If Not Err = 0 Then ltIndex = -1
                    If lIndex = ltIndex Then
                        Select Case lType
                            Case 0 'TEXT
                                .Text = lVal
                            Case 1 'VALUE
                                .Value = lVal
                            Case 2 'CAPTION
                                .Caption = lVal
                        End Select
                        Exit For
                    End If
                End If
            End With
        Next
    Wend
    Close fNum
End Sub
Sub SaveFormData(FormName As Form, lPath As String)
    Dim Ctrl As Control
    Dim fNum As Long
    Dim lIndex As Long
    Dim lVal As Variant
    Dim lType As Integer
    On Error Resume Next
    fNum = FreeFile
    Open lPath For Output Access Write As fNum
    For Each Ctrl In FormName.Controls
        If Not TypeOf Ctrl Is CommandButton Then
            With Ctrl
                Err = 0: lType = 0: lVal = .Text
                If Not Err = 0 Then
                    Err = 0: lType = 1: lVal = .Value
                    If Not Err = 0 Then
                        Err = 0: lType = 2: lVal = .Caption
                    End If
                End If
                If Err = 0 Then
                    lIndex = .Index
                    If Err = 0 Then
                        Print #fNum, .Name + ">"; CStr(.Index) + "|" + CStr(lType) + "|" + CStr(lVal)
                    Else
                        Print #fNum, .Name + "|" + CStr(lType) + "|" + CStr(lVal)
                    End If
                End If
            End With
        End If
    Next
    Close fNum
End Sub
Function StringGet(Source As String, Delimiter As String, Optional Position As Variant) As String
    Dim iVal As Integer
    Dim jVal As Integer
    Dim kVal As Integer
    Dim tbuf As String
    jVal = 0
    kVal = 1
    tbuf = Source + Delimiter
    If IsMissing(Position) = False Then
        For iVal = 1 To Val(Position) - 1
            jVal = InStr(jVal + 1, tbuf, Delimiter)
            If jVal = 0 Then
                StringGet = ""
                Exit Function
            End If
        Next iVal
        kVal = jVal + 1
    End If
    iVal = InStr(jVal + 1, tbuf, Delimiter)
    If iVal = 0 Then
        StringGet = ""
    Else
        StringGet = Mid$(tbuf, kVal, iVal - kVal)
    End If
End Function
'-------------------------------------------------------------------------------


Cheers!
0
 
mcriderCommented:
By the way, in the LoadFormData subroutine I gave you, add the following line after the "Open lPath..." line:

   If Not Err = 0 Then Exit Sub

That way, if the file you're trying to open does not exist or has some other problem, the subroutine will exit properly.


Cheers!
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now