|
-
Dec 3rd, 2002, 03:50 AM
#1
Thread Starter
Lively Member
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
-
Dec 3rd, 2002, 04:11 AM
#2
Thread Starter
Lively Member
This piece of code does exactly what I want...but what if I want to use Controls which are on other forms...
VB Code:
TextInfo = TextInfo & MyControl.Parent.Name & "." & MyControl.Name & "(" & MyControl.Index & ")" & "|" & MyControl.Text & vbCrLf
Last edited by Chrissie; Dec 3rd, 2002 at 04:15 AM.
-
Dec 8th, 2002, 02:03 AM
#3
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:
public sub DumpControls(myForm as form)
Public MyControl As Control
For Each MyControl In myForm.Controls
....Yada Yada Yada
next
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|