Results 1 to 18 of 18

Thread: Reading File info into different textboxes...

  1. #1

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

    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:
    1. Public Sub OpenBestand()
    2.    
    3.     On Error GoTo ErrHandle
    4.   Dim elements() As String
    5.  
    6.     frmInput.BestandVenster.DialogTitle = "Open file..."
    7.     frmInput.BestandVenster.Filter = "TXT File(*.txt)|*.txt|All Files(*.*)|*.*"
    8.     frmInput.BestandVenster.FilterIndex = 1
    9.     frmInput.BestandVenster.ShowOpen
    10.        
    11.     fnum = FreeFile
    12.     FileName = frmInput.BestandVenster.FileName
    13.     Open FileName For Input As #fnum
    14.    
    15.  
    16.     Do Until EOF(1)
    17.     Line Input #fnum, data
    18.        
    19.         elements = Split(data, "|")
    20.         "frmInput." &  elements(UBound(elements) - 1) & ".value" = elements(UBound(elements))
    21.         MsgBox EOF(1)
    22.     Loop
    23.  
    24. Close #fnum
    25.      
    26. Exit Sub

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Try this

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim strBuff As String, strArr() As String
    5.     Dim i As Long, strArr2() As String
    6.     With frmInput.BestandVenster
    7.         .DialogTitle = "Open file..."
    8.         .Filter = "TXT File(*.txt)|*.txt|All Files(*.*)|*.*"
    9.         .FilterIndex = 1
    10.         .ShowOpen
    11.         If Not .FileName = "" Then
    12.             Open .FileName For Binary As #1
    13.                 strBuff = Space(LOF(1))
    14.                 Get #1, , strBuff
    15.                 strArr = Split(strBuff, vbCrLf)
    16.             Close #1
    17.             For i = 0 To UBound(strArr)
    18.                 strArr2 = Split(strArr(i), "|")
    19.                 returnTextBoxWithName(strArr2(0)).Text = strArr2(1)
    20.             Next
    21.         End If
    22.     End With
    23. End Sub
    24.  
    25. Private Function returnTextBoxWithName(ByVal strName As String) As TextBox
    26.     Dim txtBox As TextBox
    27.     For Each txtBox In Controls
    28.         If txtBox.Name = strName Then
    29.             Set returnTextBoxWithName = txtBox
    30.             Exit Function
    31.         End If
    32.     Next
    33. End Function
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3

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

    Doesn't work..

    Doesn't seem to work...

    If i Get the following line correct mine will work...

    VB Code:
    1. "frmInput." &  elements(UBound(elements) - 1) & ".value" = elements(UBound(elements))

    Thanks anyway

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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]

  5. #5
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    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

  6. #6

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

    Uhm...

    VB Code:
    1. For Each txtBox In Controls
    It returns an error here....
    I already change it into

    VB Code:
    1. 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:
    1. Public Sub SaveTextboxes()
    2. Dim fnum, TextOpslaanInhoud As String
    3. Dim MijnControl As Control
    4.  
    5.     On Error GoTo ErrHandle
    6.  
    7.         frmInput.BestandVenster.DialogTitle = "Opslaan Als..."
    8.         frmInput.BestandVenster.Filter = "TXT Bestand(*.txt)|*.txt|All Files(*.*)|*.*"
    9.         frmInput.BestandVenster.FilterIndex = 0
    10.         frmInput.BestandVenster.ShowSave
    11.         fnum = FreeFile
    12.        
    13.         For Each MijnControl In frmInput.Controls
    14.             If TypeOf MijnControl Is TextBox Then
    15.             TextOpslaanInhoud = TextOpslaanInhoud & MijnControl.Name & "|" & MijnControl.Text & vbCrLf
    16.             End If
    17.         Next
    18.        
    19.         Open frmInput.BestandVenster.FileName For Output As #fnum
    20.         Print #fnum, TextOpslaanInhoud
    21.         Close #fnum
    22.  
    23. Exit Sub
    24.    
    25. ErrHandle:
    26.     MsgBox "The following error occured- " & vbCrLf & Err.Description & vbCrLf & vbCrLf & "de huidige actie wordt afgebroken.", vbInformation, "Error"
    27.  
    28. End Sub

  7. #7
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Re: Uhm...

    Originally posted by Chrissie


    VB Code:
    1. For Each txtBox In Controls
    It returns an error here....
    I already change it into

    VB Code:
    1. 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]

  8. #8
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    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?

  9. #9

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

    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

  10. #10

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

    Thanks all

    Thanks...I've got it working now...

    VB Code:
    1. fnum = FreeFile
    2.     FileName = frmInput.BestandVenster.FileName
    3.     Open FileName For Input As #fnum
    4.  
    5.  
    6.     Do Until EOF(1)
    7. '    'Does this loop until End Of File(EOF) for file number 1.
    8.  Line Input #fnum, data
    9.         'Read one line and puts it into the varible Data.
    10.        
    11.         If Not data = "" Then
    12.         elements = Split(data, "|")
    13.         frmInput.Controls(elements(UBound(elements) - 1)).Text = elements(UBound(elements))
    14.         End If
    15.         '"frmInput."  &  elements(UBound(elements) - 1) & ".text" = elements(UBound(elements))
    16.       'MsgBox EOF(1)
    17.  Loop
    18.  
    19. Close #fnum
    20.      
    21. Exit Sub
    22.        
    23. ErrHandle:
    24.     MsgBox "Error"
    25.  
    26. End Sub

  11. #11
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Because you end every line with a crlf, the last line is blank.

  12. #12
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    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:
    1. elements = Split(data, "|")
    2. 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

  13. #13

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

    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...

  14. #14
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478

    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

  15. #15
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    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))

  16. #16

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

    What about setting a boolean?

    So I can set Form(), Controls()

    but what about a Boolean?
    Someting like: ?!?

    VB Code:
    1. Boolean(elements(UBound(elements) - 1)).Value = elements(UBound(elements))

  17. #17
    Lively Member
    Join Date
    Oct 2001
    Location
    Florida
    Posts
    98
    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:
    1. 'Seperates the address into two different fields IF needed.
    2.             Call SplitAddress(TextBox1, TextBox2, StringToSplit)

    the Function itself

    VB Code:
    1. Public Sub SplitAddress(AddressFirstTextBox As TextBox, AddressSecondTextBox As TextBox, Address As String)
    2.    
    3.     Dim intPosition As Integer      'Integer value for the position of the Deliminating character.
    4.     Dim intLength As Integer        'Lenght of the Address string.
    5.    
    6.     'OK we take whatever string is in Address
    7.     'If the string contains the deliminating character in STRING_TERM
    8.     '   We put the first part in AddressFirstTextBox
    9.     '   We put the second part in the AddressSecondTextBox
    10.     'If the string does not contain the deliminating character.
    11.     '   Just puts the string in the AddressFirstTextBox and leaves the other empty.
    12.    
    13.     On Error GoTo ErrorHandler
    14.    
    15.     intPosition = InStr(Address, STRING_TERM)
    16.     intLength = Len(Address)
    17.    
    18.     If InStr(Address, STRING_TERM) <> 0 Then
    19.         'First Address Box.
    20.         AddressFirstTextBox.Text = Left(Address, intPosition - 1)
    21.         'Second Address Box.
    22.         AddressSecondTextBox.Text = Right(Address, intLength - intPosition)
    23.     Else
    24.         AddressFirstTextBox.Text = Trim(Address)
    25.         AddressSecondTextBox.Text = ""
    26.     End If
    27.    
    28. End Sub

  18. #18

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

    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
  •  



Click Here to Expand Forum to Full Width