|
-
Oct 4th, 2002, 05:32 AM
#1
Thread Starter
Lively Member
Reading File info into different textboxes...
Hi..I have a file which looks like this
txtBox1|4
txtBox2|7
txtBox3|5
I'd like to read in the file and have the values be read into the Texboxes....
so txtBox1.text has 4
I have the following code....but I need some help as it doesn't work that good...
VB Code:
Public Sub OpenBestand()
On Error GoTo ErrHandle
Dim elements() As String
frmInput.BestandVenster.DialogTitle = "Open file..."
frmInput.BestandVenster.Filter = "TXT File(*.txt)|*.txt|All Files(*.*)|*.*"
frmInput.BestandVenster.FilterIndex = 1
frmInput.BestandVenster.ShowOpen
fnum = FreeFile
FileName = frmInput.BestandVenster.FileName
Open FileName For Input As #fnum
Do Until EOF(1)
Line Input #fnum, data
elements = Split(data, "|")
"frmInput." & elements(UBound(elements) - 1) & ".value" = elements(UBound(elements))
MsgBox EOF(1)
Loop
Close #fnum
Exit Sub
-
Oct 4th, 2002, 05:38 AM
#2
Retired VBF Adm1nistrator
Try this 
VB Code:
Option Explicit
Private Sub Form_Load()
Dim strBuff As String, strArr() As String
Dim i As Long, strArr2() As String
With frmInput.BestandVenster
.DialogTitle = "Open file..."
.Filter = "TXT File(*.txt)|*.txt|All Files(*.*)|*.*"
.FilterIndex = 1
.ShowOpen
If Not .FileName = "" Then
Open .FileName For Binary As #1
strBuff = Space(LOF(1))
Get #1, , strBuff
strArr = Split(strBuff, vbCrLf)
Close #1
For i = 0 To UBound(strArr)
strArr2 = Split(strArr(i), "|")
returnTextBoxWithName(strArr2(0)).Text = strArr2(1)
Next
End If
End With
End Sub
Private Function returnTextBoxWithName(ByVal strName As String) As TextBox
Dim txtBox As TextBox
For Each txtBox In Controls
If txtBox.Name = strName Then
Set returnTextBoxWithName = txtBox
Exit Function
End If
Next
End Function
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Oct 4th, 2002, 05:48 AM
#3
Thread Starter
Lively Member
Doesn't work..
Doesn't seem to work...
If i Get the following line correct mine will work...
VB Code:
"frmInput." & elements(UBound(elements) - 1) & ".value" = elements(UBound(elements))
Thanks anyway
-
Oct 4th, 2002, 05:54 AM
#4
Retired VBF Adm1nistrator
That line will never work.
Trust me. Its just the way VB works.
You can't do things like that.
Why doesn't my code work ?
Do you get an error ?
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Oct 4th, 2002, 06:18 AM
#5
Frenzied Member
Maybe you can try to use the CallByName function for this.
Do a search on it.
Code:
If Question = Incomplete Then
AnswerNextOne
Else
ReplyIfKnown
End If
cu Swatty
-
Oct 4th, 2002, 06:47 AM
#6
Thread Starter
Lively Member
Uhm...
VB Code:
For Each txtBox In Controls
It returns an error here....
I already change it into
VB Code:
For Each txtBox In frmInput.Controls
That works fine for me.....
Btw if someone is intrested...here is the code I wrote for saving the input from all the texboxes...it works delicious..
VB Code:
Public Sub SaveTextboxes()
Dim fnum, TextOpslaanInhoud As String
Dim MijnControl As Control
On Error GoTo ErrHandle
frmInput.BestandVenster.DialogTitle = "Opslaan Als..."
frmInput.BestandVenster.Filter = "TXT Bestand(*.txt)|*.txt|All Files(*.*)|*.*"
frmInput.BestandVenster.FilterIndex = 0
frmInput.BestandVenster.ShowSave
fnum = FreeFile
For Each MijnControl In frmInput.Controls
If TypeOf MijnControl Is TextBox Then
TextOpslaanInhoud = TextOpslaanInhoud & MijnControl.Name & "|" & MijnControl.Text & vbCrLf
End If
Next
Open frmInput.BestandVenster.FileName For Output As #fnum
Print #fnum, TextOpslaanInhoud
Close #fnum
Exit Sub
ErrHandle:
MsgBox "The following error occured- " & vbCrLf & Err.Description & vbCrLf & vbCrLf & "de huidige actie wordt afgebroken.", vbInformation, "Error"
End Sub
-
Oct 4th, 2002, 06:50 AM
#7
Retired VBF Adm1nistrator
Re: Uhm...
Originally posted by Chrissie
VB Code:
For Each txtBox In Controls
It returns an error here....
I already change it into
VB Code:
For Each txtBox In frmInput.Controls
Huh.
Well I had assumed you were running the code from that form itself.
So does it work now ?
It worked when I tried it here...
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Oct 4th, 2002, 06:54 AM
#8
How about:
frmInput.Controls(elements(UBound(elements) - 1)).value = elements(UBound(elements))
What are you programming in? As far as i know, a Textbox doesn't have a Value property in VB. Are you programming in MS Access?
-
Oct 4th, 2002, 07:01 AM
#9
Thread Starter
Lively Member
Nope..
Nope doesn't work...
Gives me Run-Time Error '13' Type mismatch"
Could it be cause by the way I save the info to a file?? As posted in my post before?
Working in VB 6.0
-
Oct 4th, 2002, 07:14 AM
#10
Thread Starter
Lively Member
Thanks all
Thanks...I've got it working now...
VB Code:
fnum = FreeFile
FileName = frmInput.BestandVenster.FileName
Open FileName For Input As #fnum
Do Until EOF(1)
' 'Does this loop until End Of File(EOF) for file number 1.
Line Input #fnum, data
'Read one line and puts it into the varible Data.
If Not data = "" Then
elements = Split(data, "|")
frmInput.Controls(elements(UBound(elements) - 1)).Text = elements(UBound(elements))
End If
'"frmInput." & elements(UBound(elements) - 1) & ".text" = elements(UBound(elements))
'MsgBox EOF(1)
Loop
Close #fnum
Exit Sub
ErrHandle:
MsgBox "Error"
End Sub
-
Oct 4th, 2002, 07:21 AM
#11
Because you end every line with a crlf, the last line is blank.
-
Oct 4th, 2002, 08:08 AM
#12
Frenzied Member
Well done (goed zo)
Could this be done without knowing the name of the form.
Lets say it is also stored in the file.
So you got frmInput, frmChange, frmAccept (anything for that manner)
Would this work
VB Code:
elements = Split(data, "|")
elements(UBound(elements) - 2).Controls(elements(UBound(elements) - 1)).Text = elements(UBound(elements))
Code:
If Question = Incomplete Then
AnswerNextOne
Else
ReplyIfKnown
End If
cu Swatty
-
Oct 4th, 2002, 08:25 AM
#13
Thread Starter
Lively Member
Yeah...
Yeah...Think so...but it doesn't make any sense to store the form name in a file...if all your Textboxes are on 1 form...
-
Oct 4th, 2002, 08:28 AM
#14
Frenzied Member
Of course ( natuurlijk )
Yeah
Just a thought, if this can be done it can be usefull in the future.
I haven't stored any value's in a file to read them back in later.
Normally working with databases.
My mind went thinking , so i followed it.
Code:
If Question = Incomplete Then
AnswerNextOne
Else
ReplyIfKnown
End If
cu Swatty
-
Oct 4th, 2002, 08:31 AM
#15
Provided the form is loaded (only once), you could do something like:
Forms(elements(UBound(elements) - 2)).Controls(elements(UBound(elements) - 1)).Text = elements(UBound(elements))
-
Oct 4th, 2002, 08:45 AM
#16
Thread Starter
Lively Member
What about setting a boolean?
So I can set Form(), Controls()
but what about a Boolean?
Someting like: ?!?
VB Code:
Boolean(elements(UBound(elements) - 1)).Value = elements(UBound(elements))
-
Oct 4th, 2002, 09:25 AM
#17
Lively Member
I know you have it working. We have a function in our program that does the same or very similar thing. Used for address that recuire two lines but are stored as one line in the database with a deliminating character "|" denoted by the variable STRING_TERM (horrible name). Just thought it might be useful to someone if ever searching these threads.
Note its designed for two text boxes and since you pass the two text boxes to it from the form they are on, you don't have to tell it the form name.
used from a form like so:
VB Code:
'Seperates the address into two different fields IF needed.
Call SplitAddress(TextBox1, TextBox2, StringToSplit)
the Function itself
VB Code:
Public Sub SplitAddress(AddressFirstTextBox As TextBox, AddressSecondTextBox As TextBox, Address As String)
Dim intPosition As Integer 'Integer value for the position of the Deliminating character.
Dim intLength As Integer 'Lenght of the Address string.
'OK we take whatever string is in Address
'If the string contains the deliminating character in STRING_TERM
' We put the first part in AddressFirstTextBox
' We put the second part in the AddressSecondTextBox
'If the string does not contain the deliminating character.
' Just puts the string in the AddressFirstTextBox and leaves the other empty.
On Error GoTo ErrorHandler
intPosition = InStr(Address, STRING_TERM)
intLength = Len(Address)
If InStr(Address, STRING_TERM) <> 0 Then
'First Address Box.
AddressFirstTextBox.Text = Left(Address, intPosition - 1)
'Second Address Box.
AddressSecondTextBox.Text = Right(Address, intLength - intPosition)
Else
AddressFirstTextBox.Text = Trim(Address)
AddressSecondTextBox.Text = ""
End If
End Sub
-
Oct 4th, 2002, 09:35 AM
#18
Thread Starter
Lively Member
True...
Mine is reading in all the textboxes on a form....
no matter what their name is...
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
|