Dim sFileText as StringDim iFileNo as IntegeriFileNo = FreeFile'open the file for readingOpen "C:\Test.txt" For Input As #iFileNo'change this filename to an existing file! (or run the example below first)
'read the file until we reach the endDo While Not EOF(iFileNo)Input #iFileNo, sFileText'show the text (you will probably want to replace this line as appropriate to your program!)MsgBox sFileTextLoop
'close the file (if you dont do this, you wont be able to open it again!)Close #iFileNo(note: an alternative to Input # is Line Input # , which reads whole lines).
An example of writing a file:Dim sFileText as StringDim iFileNo as IntegeriFileNo = FreeFile'open the file for writingOpen "C:\Test.txt" For Output As #iFileNo'please note, if this file already exists it will be overwritten!
'write some example text to the filePrint #iFileNo, "first line of text"Print #iFileNo, " second line of text"Print #iFileNo, "" 'blank linePrint #iFileNo, "some more text!"
'close the file (if you dont do this, you wont be able to open it again!)Close #iFileNo(note: an alternative to Print # is Write # , which adds commas between items, and also puts the " character around strings).
You can read files in a much faster way too, by simply loading the entire file at once. To do this you need to use binary mode, like so:Function FileText(ByVal filename As String) As StringDim handle As Integer
' ensure that the file existsIf Len(Dir$(filename)) = 0 ThenErr.Raise 53 ' File not foundEnd If
' open in binary modehandle = FreeFileOpen filename$ For Binary As #handle' read the string and close the fileFileText = Space$(LOF(handle))Get #handle, , FileTextClose #handleEnd Function
Reading/writing files can also be acheived using FSO (FileSystemObject) as shown below. Please note that using FSO requires an extra reference (under "Project"->"References"), and therefore extra files to be installed with your application.Option Explicit'Set a reference to "Microsoft Scripting Runtime"Private Sub Command1_Click()'declare and initiate required objectsDim fs As FileSystemObjectDim ts As TextStream
Set fs = New FileSystemObject
'To writeSet ts = fs.OpenTextFile("C:\mytestfile.txt", ForWriting, True)ts.WriteLine "I Love"ts.WriteLine "VB Forums"ts.Close
'To ReadIf fs.FileExists("C:\mytestfile.txt") ThenSet ts = fs.OpenTextFile("C:\mytestfile.txt")
Do While Not ts.AtEndOfStreamMsgBox ts.ReadLineLoopts.CloseEnd If
'clear memory used by FSO objectsSet ts = NothingSet fs = NothingEnd Sub
Wednesday, February 20, 2008
Monday, February 18, 2008
Number of sundays in a period function
Public Function BizDateDiff(ByVal varDateStart As Date, ByVal varDateEnd As Date, DayNumber as Integer ) As Integer ‘ DayNumber (sunday =1,monday=2…)
Dim varNextDate As Date
'‘This function calculates the weekdays between two dates.
‘Exit if variables not a valid date
If Not IsDate(varDateStart) Or Not IsDate(varDateEnd) Then
BizDateDiff = 0
Exit Function
End If
varNextDate = varDateStart
BizDateDiff = 0
While Not varDateEnd< varNextDate
If Weekday(DateValue(varNextDate)) = DayNumber Then
BizDateDiff = BizDateDiff + 1 ' Number of Day number Days (eg: sunday)
varNextDate = DateAdd("d", 7, varNextDate)
Else
varNextDate = DateAdd("d", 1, varNextDate)
End If
Wend
BizDateDiff = Day(varDateEnd) - BizDateDiff ' Number of working days or excluded the Day number day
End Function
Dim varNextDate As Date
'‘This function calculates the weekdays between two dates.
‘Exit if variables not a valid date
If Not IsDate(varDateStart) Or Not IsDate(varDateEnd) Then
BizDateDiff = 0
Exit Function
End If
varNextDate = varDateStart
BizDateDiff = 0
While Not varDateEnd< varNextDate
If Weekday(DateValue(varNextDate)) = DayNumber Then
BizDateDiff = BizDateDiff + 1 ' Number of Day number Days (eg: sunday)
varNextDate = DateAdd("d", 7, varNextDate)
Else
varNextDate = DateAdd("d", 1, varNextDate)
End If
Wend
BizDateDiff = Day(varDateEnd) - BizDateDiff ' Number of working days or excluded the Day number day
End Function
Subscribe to:
Comments (Atom)