Results 1 to 3 of 3

Thread: Saving values to Textfile - Index...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96

    Saving values to Textfile - Index...

    I'm having few troubles by writing to textfiles...The code below is the code I wrote a few months ago. It simply writes Textboxes, OptionButtons, Checkboxes and Comboboxes to a specified file.

    Text1|Valuetext
    Text2|Blabla
    CboBla|2
    ChkBlabla|1

    But I want the code not only to enter the name of the textbox...but also the Name of the form it belongs to. If the control is a control-array I also want his index....So it should look like this...

    frmInput.Text1|Valuetext
    frmInput.Text2(0)|BlaOne
    frmInput.Text2(1)|BlaTwo

    Hopefully someone can give me a hand...

    VB Code:
    1. Option Explicit
    2. Public MyControl As Control
    3. Public FileName As String
    4. Public TextInfo As String
    5.  
    6. Public Sub Command1_Click()
    7. Dim fnum As String
    8.  
    9.     On Error GoTo ErrHandle
    10.  
    11.         frmInput.FileWindow.DialogTitle = "Save As"
    12.         frmInput.FileWindow.Filter = "MYFILE(*.myf)|*.myf|All Files(*.*)|*.*"
    13.         frmInput.FileWindow.FilterIndex = 0
    14.         frmInput.FileWindow.ShowSave
    15.        
    16.         fnum = FreeFile
    17.        
    18.         TextInfo = ""
    19.        
    20.         For Each MyControl In frmInput.Controls
    21.             If TypeOf MyControl Is TextBox Then
    22.             TextInfo = TextInfo & MyControl.Name & "|" & MyControl.Text & vbCrLf
    23.             ElseIf TypeOf MyControl Is OptionButton Then
    24.             TextInfo = TextInfo & MyControl.Name & "|" & MyControl.Value & vbCrLf
    25.             ElseIf TypeOf MyControl Is CheckBox Then
    26.             TextInfo = TextInfo & MyControl.Name & "|" & MyControl.Value & vbCrLf
    27.             ElseIf TypeOf MyControl Is ComboBox Then
    28.             TextInfo = TextInfo & MyControl.Name & "|" & MyControl.ListIndex & vbCrLf
    29.             End If
    30.         Next
    31.  
    32.         If Not frmInput.FileWindow.FileName = "" Then
    33.         Open frmInput.FileWindow.FileName For Output As #fnum
    34.         Print #fnum, TextInfo
    35.         Close #fnum
    36.        
    37.         MsgBox "File: (" & frmInput.FileWindow.FileName & ") saved correctly", vbInformation, "save"
    38.         End If
    39. Exit Sub
    40.    
    41. ErrHandle:
    42.     If frmInput.FileWindow.CancelError = True Then
    43.         MsgBox "No file saved", vbCritical, "Save"
    44.     Else
    45.         MsgBox "Error Bla Bla", vbCritical, "Error"
    46.     End If
    47. End Sub

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Location
    The Netherlands
    Posts
    96
    This piece of code does exactly what I want...but what if I want to use Controls which are on other forms...

    VB Code:
    1. TextInfo = TextInfo & MyControl.Parent.Name & "." & MyControl.Name & "(" & MyControl.Index & ")" & "|" & MyControl.Text & vbCrLf
    Last edited by Chrissie; Dec 3rd, 2002 at 04:15 AM.

  3. #3
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    Not too sure I know what you meant, but here goes...

    Create a function/sub in a module that receives a form object then use the object in you enumerations.



    VB Code:
    1. public sub DumpControls(myForm as form)
    2. Public MyControl As Control
    3.  
    4. For Each MyControl In myForm.Controls
    5.  
    6. ....Yada Yada Yada
    7.  
    8. next
    9.  
    10. end sub

    Call this sub/function from each of your forms

    DumpControls Me

    This should work.
    Last edited by randem; Dec 8th, 2002 at 02:38 AM.

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