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:
Option Explicit
Public MyControl As Control
Public FileName As String
Public TextInfo As String
Public Sub Command1_Click()
Dim fnum As String
On Error GoTo ErrHandle
frmInput.FileWindow.DialogTitle = "Save As"
frmInput.FileWindow.Filter = "MYFILE(*.myf)|*.myf|All Files(*.*)|*.*"
frmInput.FileWindow.FilterIndex = 0
frmInput.FileWindow.ShowSave
fnum = FreeFile
TextInfo = ""
For Each MyControl In frmInput.Controls
If TypeOf MyControl Is TextBox Then
TextInfo = TextInfo & MyControl.Name & "|" & MyControl.Text & vbCrLf
ElseIf TypeOf MyControl Is OptionButton Then
TextInfo = TextInfo & MyControl.Name & "|" & MyControl.Value & vbCrLf
ElseIf TypeOf MyControl Is CheckBox Then
TextInfo = TextInfo & MyControl.Name & "|" & MyControl.Value & vbCrLf
ElseIf TypeOf MyControl Is ComboBox Then
TextInfo = TextInfo & MyControl.Name & "|" & MyControl.ListIndex & vbCrLf
End If
Next
If Not frmInput.FileWindow.FileName = "" Then
Open frmInput.FileWindow.FileName For Output As #fnum
Print #fnum, TextInfo
Close #fnum
MsgBox "File: (" & frmInput.FileWindow.FileName & ") saved correctly", vbInformation, "save"
End If
Exit Sub
ErrHandle:
If frmInput.FileWindow.CancelError = True Then
MsgBox "No file saved", vbCritical, "Save"
Else
MsgBox "Error Bla Bla", vbCritical, "Error"
End If
End Sub