Results 1 to 13 of 13

Thread: [RESOLVED] Calling a function and it's arguments

  1. #1

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Resolved [RESOLVED] Calling a function and it's arguments

    How would i go about calling this function

    VB Code:
    1. Private Function SaveFileFromTB(TxtBox As Object, _
    2.    FilePath As String, Optional Append As Boolean = False) _
    3.    As Boolean

    Jenova

  2. #2
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    Re: Calling a function and it's arguments

    If SaveFileFromTB(Text1,"c:\mypath") = True Then
    ' do something
    Else
    ' do something else
    End If

  3. #3
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Re: Calling a function and it's arguments

    Dim ReturnValue as boolean

    ReturnValue = SaveFileFromTB(textbox, "c:\Path")

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Calling a function and it's arguments

    VB Code:
    1. If SaveFileFromTB(Text1, MyPathString) Then
    2.     ' do something
    3. End if
    4.  
    5. ' or
    6.  
    7. If SaveFileFromTB(Text1, MyPathString, True) Then
    8.     ' do something
    9. End if

  5. #5

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Calling a function and it's arguments

    Sorry, should have explianed better. This is the function:

    VB Code:
    1. Private Function OpenFileToTB(TxtBox As Object, _
    2.    FilePath As String, Optional Append As Boolean = False) _
    3.    As Boolean
    4.    
    5. Dim iFile       As Integer
    6. Dim s           As String
    7.  
    8. If Dir(FilePath) = "" Then Exit Function
    9.  
    10. On Error GoTo ErrorHandler:
    11. s = TxtBox.Text
    12.  
    13. iFile = FreeFile
    14. Open FilePath For Input As #iFile
    15. s = Input(LOF(iFile), #iFile)
    16. If Append Then
    17.     TxtBox.Text = TxtBox.Text & s
    18. Else
    19.     TxtBox.Text = s
    20. End If
    21.  
    22. LoadFileToTB = True
    23.  
    24. ErrorHandler:
    25. If iFile > 0 Then Close #iFile
    26.  
    27. End Function

    I need to call it from the form's load event. Or does anyone have an easier way to open a htm/txt file to a text box.
    Last edited by Jenova; Mar 14th, 2006 at 01:18 PM.

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Calling a function and it's arguments

    It is useless to call that function from the form's load event since it uses the contents of a textbox and that textbox can't at that point have anything in it.

  7. #7

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Calling a function and it's arguments

    I am tring to open a htm file so it's practically source when viewed in my program. I am using this code to do it

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3. On Error Resume Next
    4.  
    5.     Dim handle As Integer
    6.     Dim file As String
    7.    
    8.     handle = FreeFile
    9.     Open "C:\Documents and Settings\Carl\Desktop\index.htm" For Input As handle
    10.         Do While Not EOF(handle)
    11.             Text1.TextRTF = file
    12.         Loop
    13.     Close #handle
    14.  
    15. End Sub

    Slight drawback, it crashes when i run the program. Any ideas on how i can make this work

  8. #8
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Calling a function and it's arguments

    Slight drawback, it crashes when i run the program.
    It doesn't "crash", you have an infinite loop. EOF(handle) will never be true because you are not moving through the file.

    You need to retrieve the file contents using Input or Line Input statements.

  9. #9

  10. #10

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Calling a function and it's arguments

    Apologies.

    On error resume next was to see if the that would solve it but i't didn't. Thanks for the advice on an infinite loop but if i use Line Input or Input then it will give me the last line of the HTML file which is </html>.

    Jenova

  11. #11

  12. #12

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Calling a function and it's arguments

    lol sorted.

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     Dim handle As Integer
    4.     Dim file As String
    5.    
    6.     handle = FreeFile
    7.     Open "C:\Documents and Settings\Carl\Desktop\index.htm" For Input As handle
    8.         Do While Not EOF(handle)
    9.             file = Input(LOF(handle), #handle)
    10.             Text1.TextRTF = file
    11.         Loop
    12.     Close #handle
    13.  
    14. End Sub

    Thanks guys

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Calling a function and it's arguments

    You can now help us by pulling down the Thread Tools menu and clicking the Mark Thread Resolved button which will let everyone know that you have your answer.

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