Results 1 to 3 of 3

Thread: Save & Open a file (RESOLVED)

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    32

    Save & Open a file (RESOLVED)

    Could someone help me ? First off how do i code 3 optButton responce to see which one was selected ( optYes ; optNo ; optMayBe ) then save the answer ?

    This is what i have sofar! I don't know how to get the answers back on the form in the right spot.

    Also how do i make sure when i save/open that the For/Next counters start the same ?

    VB Code:
    1. Private Sub cmdSave_Click()
    2.        
    3.         Dim fs As New FileSystemObject
    4.         Dim ts As TextStream
    5.         Dim blankFolder As Boolean
    6.         Dim stFolder As String
    7.         Dim SSNum As String
    8.         Dim txtObject As Control
    9.         Dim bState As Boolean
    10.         'Consintrate strings
    11.         SSNum = txtSSNum1.Text & "-" & txtSSNum2.Text & "-" & txtSSNum3.Text
    12.         stFolder = "C:\My Documents\" & SSNum
    13.     '***********************************************
    14.     'Check to see if file name was entered
    15.     '***********************************************
    16.         If txtYourName.Text = "" Then MsgBox "You forgot the Name"
    17.         txtYourName.SetFocus
    18.         If txtYourName.Text = "" Then Exit Sub
    19.    '************************************************
    20.    'Check to see if folder ID was entered
    21.    '************************************************
    22.         If Len(SSNum) <= 10 Then _
    23.         MsgBox "You forgot your Social Security Number"
    24.         txtSSNum1.SetFocus
    25.         If Len(SSNum) <= 10 Then Exit Sub
    26.    '************************************************
    27.     'Check to see if folder exist
    28.    '************************************************
    29.         blankFolder = fs.FolderExists(stFolder)
    30.         If blankFolder = True Then MsgBox "Folder Exist"
    31.         If blankFolder = True Then txtYourName.SetFocus
    32.         If blankFolder = True Then Exit Sub
    33.    '************************************************  
    34.    'Check That all txtBox's are filled in
    35.    '************************************************
    36.         bState = True
    37.         For Each txtObject In Me.Controls
    38.             If TypeOf txtObject Is TextBox Then
    39.                 If txtObject.Text = "" Then bState = False
    40.                 txtObject.SetFocus
    41.                 Else
    42.                 End If
    43.         Next
    44.             If bState = False Then MsgBox "You forgot to fill in an answer"
    45.             If bState = False Then Exit Sub
    46.     '************************************************  
    47.     '   Make the folder and name it
    48.     '************************************************
    49.         MkDir ("c:/My Documents/" & SSNum)
    50.             MsgBox "Created the folder" & SSNum
    51.    '************************************************
    52.    '    Create the file name and put something in it
    53.    '************************************************
    54.         Set ts = fs.CreateTextFile("C:/My Documents/" & SSNum & "/" & txtYourName.Text & ".txt")
    55.             MsgBox "Created the file " & txtYourName.Text
    56.    '************************************************
    57.    '    Write to file 22 responces
    58.    '    Write the texBoxes to File  
    59.    '************************************************
    60.         For Each txtObject In Me.Controls
    61.             If TypeOf txtObject Is TextBox Then
    62.                 ts.WriteLine (txtObject.Text)
    63.             End If
    64.    '************************************************
    65.    '    THE FIRST THREE BUTTON OPT.
    66.    '************************************************                    
    67.    '    optInPerson ; optOverThePhone ; optJobShow
    68.    '************************************************
    69.    'How do you "If" three to see which one is set?
    70.    '
    71.    '
    72.    '************************************************
    73.    '            THE FIRST TWO BUTTON OPT/
    74.    '************************************************
    75.    '    optAdYes ; optAdNo
    76.    '************************************************   
    77.     If optAdYes.Value = True Then
    78.         ts.WriteLine "True"
    79.     Else
    80.         ts.WriteLine"False"
    81.     End If
    82.    '************************************************
    83.    '    THE SECOUND TWO BUTTON OPT.
    84.    '************************************************
    85.    '    optBenefitsYes ; optBenefitsNo
    86.    '************************************************
    87.     If optBenefitsYes.Value = True Then
    88.         ts.WriteLine "True"
    89.     Else
    90.         ts.WriteLine"False"
    91.     End If
    92.    '************************************************
    93.    '    THE SECOUND THREE BUTTON OPT.
    94.    '************************************************
    95.    'How do you "If" three to see which one is set?
    96.    '
    97.    '
    98.    '************************************************
    99.    '    Tell you wrote to the file
    100.    '************************************************
    101.             MsgBox "Wrote to file " & txtYourName.Text
    102.         ts.Close
    103.        
    104. End Sub
    105. '---------------------------------------------------------------------------------------------
    106. Private Sub cmdOpen_Click()
    107.    
    108.         Dim fs As New FileSystemObject
    109.         Dim ts As TextStream
    110.         Dim blankFolder As Boolean
    111.         Dim blankFile As Boolean
    112.         Dim SSNum As String
    113.         Dim txtObject As Control
    114.         Dim stFolder As String
    115.         Dim StFile As String
    116.    '**************************************************    
    117.    '    Consentrate strings
    118.    '**************************************************    
    119.          SSNum = txtSSNum1.Text & "-" & txtSSNum2.Text & "-" & txtSSNum3.Text
    120.         stFolder = "c:\My Documents\" & SSNum
    121.         StFile = stFolder & "\" & txtYourName.Text & ".txt"
    122.    '*************************************************
    123.    'Check to see if file name was entered
    124.    '*************************************************
    125.         If txtYourName.Text = "" Then MsgBox "You forgot the Name"
    126.         txtYourName.SetFocus
    127.         If txtYourName.Text = "" Then Exit Sub
    128.    '*************************************************    
    129.    'Check to see if folder ID was entered
    130.    '*************************************************
    131.         If Len(SSNum) <= 10 Then MsgBox "You forgot your Social Security Number"
    132.         txtSSNum1.SetFocus
    133.         If Len(SSNum) <= 10 Then Exit Sub
    134.    '*************************************************
    135.    'Check to see if Folder Exist
    136.    '*************************************************
    137.         blankFolder = fs.FolderExists(stFolder)
    138.         If blankFolder = False Then MsgBox "Folder does not Exist"
    139.         'Clear the txtBoxes
    140.         If blankFolder = False Then txtYourName.Text = ""
    141.         If blankFolder = False Then txtSSNum1.Text = ""
    142.         If blankFolder = False Then txtSSNum2.Text = ""
    143.         If blankFolder = False Then txtSSNum3.Text = ""
    144.         'Reset focus
    145.         If blankFolder = False Then txtYourName.SetFocus
    146.         'Exit the Sub
    147.         If blankFolder = False Then Exit Sub
    148.    '*************************************************
    149.    'Check to see if File Exist
    150.    '*************************************************
    151.         blankFile = fs.FileExists(StFile)
    152.         If blankFile = False Then MsgBox "File does not Exist"
    153.         'Clear the txtBoxes
    154.         If blankFile = False Then txtYourName.Text = ""
    155.         If blankFile = False Then txtSSNum1.Text = ""
    156.         If blankFile = False Then txtSSNum2.Text = ""
    157.         If blankFile = False Then txtSSNum3.Text = ""
    158.         'Reset focus
    159.         If blankFile = False Then txtYourName.SetFocus
    160.         'Exit the Sub
    161.         If blankFile = False Then Exit Sub
    162.    '***************************************************
    163.    'Open the file and get the contents
    164.    '***************************************************
    165.         Set ts = fs.OpenTextFile("C:\My Documents\" & SSNum & "\" & txtYourName.Text & ".Txt")
    166.        
    167.         For Each txtObject In Me.Controls
    168.             If TypeOf txtObject Is TextBox Then
    169.                 ts.ReadLine (txtObject.Text)
    170.             End If
    171.         Next
    172.   '*************************************************
    173.   ' NOW TO GET OPT RESPONCES
    174.   *************************************************
    175.    '    THE FIRST THREE BUTTON OPT.
    176.    '************************************************                    
    177.    '    optInPerson ; optOverThePhone ; optJobShow
    178.    '************************************************
    179.    'How do you "If" three to see which one is set?
    180.    '
    181.    '
    182.    '************************************************
    183.    '            THE FIRST TWO BUTTON OPT/
    184.    '************************************************
    185.    '    optAdYes ; optAdNo
    186.    '************************************************   
    187.    '
    188.    '
    189.    '
    190.    '************************************************
    191.    '    THE SECOUND TWO BUTTON OPT.
    192.    '************************************************
    193.    '    optBenefitsYes ; optBenefitsNo
    194.    '************************************************
    195.    '
    196.    '
    197.    '
    198.    '************************************************
    199.    '    THE SECOUND THREE BUTTON OPT.
    200.    '************************************************
    201.    'How do you "If" three to see which one is set?
    202.    '
    203.    '
    204.    '************************************************          
    205.    '    Let them know you opened the file
    206.    '************************************************
    207.             MsgBox " Opened file " & txtYourName.Text
    208.         ts.Close
    209.        
    210. End Sub
    Last edited by mikrow; Dec 8th, 2002 at 05:15 PM.
    Mike Rowland

  2. #2
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    I never liked using if statement to test dozens of option buttons to see which ones where selected. An easier way is to set a variable when the user clicks the option button

    VB Code:
    1. '************************************************
    2. ' optInPerson ; optOverThePhone ; optJobShow
    3. '************************************************
    4. 'How do you "If" three to see which one is set?
    5. Dim strInterviewType As String
    6.  
    7. Private Sub optInPerson_Click()
    8.     If optInPerson Then strInterviewType = "In Person"
    9. End Sub
    10. Private Sub optOverThePhone_Click()
    11.     If optOverThePhone Then strInterviewType = "Over the Phone"
    12. End Sub
    13. Private Sub optJobShow_Click()
    14.     If optJobShow Then strInterviewType = "Job Show"
    15. End Sub
    16. Private Sub Command1_Click()
    17.     MsgBox strInterviewType
    18. End Sub
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  3. #3
    New Member
    Join Date
    Sep 2002
    Posts
    1
    create all your option buttons in control arrays :

    optColor(0), optColor(1), optColor(2)....
    optLocation(0), optLocation(1)... etc.

    create a module level variable for each control array :

    Private m_Color as integer
    private m_Location as integer

    assign each variable the control array's selected index in click event:

    private sub optColor_click(Index as integer)
    m_Color = index
    end sub

    private sub optLocation_click(Index as integer)
    m_Location = index
    end sub

    when saving, all you have to to is save the variables in your file :

    ts.WriteLine m_color
    ts.writeline m_Location

    when loading :

    optColor(cint(ts.ReadLine)).value=true
    optLocation(cint(ts.ReadLine)).value=true

    if user doesn't click the option buttons all values will be 0, which is the first option button (assuming 0 is the lower bound). remember to reset the module level variables if you are gonna re-use the form without unloading first.

    Hope this is what you were looking for.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width