Results 1 to 8 of 8

Thread: byval statement

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    London, Uk.
    Posts
    72

    Arrow byval statement

    Can anyone tell me what is wrong with this sentence as I get an error and i can't figure it out:

    private Function GetFileContents byval "c:\Q1.txt" as String as String

    thanks.

  2. #2
    Swatty
    Guest
    VB Code:
    1. s ="c:\Q1.txt"
    2. Private Function GetFileContents(ByVal s As String) As String
    3.  
    4. End Function
    it expects a variable, if you have a constant expression like "c:\Q1.txt" why don't u declare it in your function?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    London, Uk.
    Posts
    72
    Got another error somewhere now, but can't figure out (i'm still a beginner at vb6, unlike you experts!)

    Here's the code - any suggestions?

    Sub form_load()
    s = "h:\proj\Q1.txt"
    Private Function GetFileContents(ByVal s As String) As String

    ' if file does not exist, ignore the error and return an empty string

    Dim iFile As Integer
    On Error Resume Next

    iFile = FreeFile
    GetFileContents = Space
    FileLen "h:\proj\Q1.txt"
    Open "h:\proj\Q1.txt" For Binary As #iFile
    Get #iFile, , GetFileContents
    Close #iFile

    ' function call

    Label1.Caption = GetFileContents

    End Function
    End Function

    Thanks.

  4. #4
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    VB Code:
    1. Sub form_load()
    2. [b]Dim strContents As String[/b]
    3. s = "h:\proj\Q1.txt"
    4. [b]strContents = GetFileContents(s)
    5. End Sub[/b]
    6.  
    7. Private Function GetFileContents(ByVal s As String) As String
    8.  
    9. ' if file does not exist, ignore the error and return an empty string
    10.  
    11. Dim iFile As Integer
    12. On Error Resume Next
    13.  
    14. iFile = FreeFile
    15. GetFileContents = Space
    16. FileLen "h:\proj\Q1.txt"
    17. Open "h:\proj\Q1.txt" For Binary As #iFile
    18. Get #iFile, , GetFileContents
    19. Close #iFile
    20.  
    21. ' function call
    22.  
    23. Label1.Caption = GetFileContents
    24.  
    25. End Function

    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  5. #5
    Swatty
    Guest
    Dont put any function into a sub or function,
    handle one at a time.
    We are all learning.
    VB Code:
    1. Sub form_load()
    2.   Dim s As String
    3.   s = "h:\proj\Q1.txt"
    4.   ' function call
    5.   Label1.Caption = GetFileContents(s)
    6. End Sub
    7.  
    8.  
    9. Private Function GetFileContents(ByVal s As String) As String
    10.  
    11. ' if file does not exist, ignore the error and return an empty string
    12.  
    13. Dim iFile As Integer
    14. 'On Error Resume Next
    15. ' not so good if an error occures , you are not sure the file will be closed.
    16. On Error goto FuncErr
    17.  
    18. iFile = FreeFile
    19. GetFileContents = Space
    20. FileLen "h:\proj\Q1.txt"
    21. Open "h:\proj\Q1.txt" For Binary As #iFile
    22. Get #iFile, , GetFileContents
    23.  
    24. FuncEXit:
    25.    Close #iFile
    26.    Exit Function
    27. FuncErr:
    28.  Resume FuncEXit
    29.  
    30. End Function

  6. #6
    Swatty
    Guest
    Bit too late.
    Still some differences

  7. #7
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    Originally posted by Swatty
    Bit too late.
    Still some differences
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2002
    Location
    London, Uk.
    Posts
    72
    Does this code apply the same thing, it works, but if i want two separate .txt files to be read into two separate label boxes would I have to repeat the code but change the q2.txt and label2 or can i embed to existing code?

    'Sub form_load()

    'Dim filenum As Integer
    'Dim filecontent As String

    'filenum = FreeFile
    'Open "h:\proj\Q1.txt" For Input As #1
    'Input #1, filecontent
    'Close #1

    'Label1.Caption = filecontent

    'End Sub

    ' code when the quit command button (cmd_quit) is selected

    Sub cmd_quit_click()

    End

    End Sub

    Thanks for your time and help again.

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