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.
Printable View
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.
it expects a variable, if you have a constant expression like "c:\Q1.txt" why don't u declare it in your function?VB Code:
s ="c:\Q1.txt" Private Function GetFileContents(ByVal s As String) As String End Function
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.
VB Code:
Sub form_load() [b]Dim strContents As String[/b] s = "h:\proj\Q1.txt" [b]strContents = GetFileContents(s) End Sub[/b] 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
:)
Dont put any function into a sub or function,
handle one at a time.
We are all learning.
VB Code:
Sub form_load() Dim s As String s = "h:\proj\Q1.txt" ' function call Label1.Caption = GetFileContents(s) End Sub 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 ' not so good if an error occures , you are not sure the file will be closed. On Error goto FuncErr iFile = FreeFile GetFileContents = Space FileLen "h:\proj\Q1.txt" Open "h:\proj\Q1.txt" For Binary As #iFile Get #iFile, , GetFileContents FuncEXit: Close #iFile Exit Function FuncErr: Resume FuncEXit End Function
Bit too late.
Still some differences
:DQuote:
Originally posted by Swatty
Bit too late.
Still some differences
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.