multiple functions?

M

Mike

I have some code I am working on. One function reads a file using fso, then
I have multiple functions using the string read from the file. Each
function splits the lines up and will return one value.

Is there a better way? Is handling this many array which are basically the
same going to cause some server distress?


Thanks
Mike




Code:

Function ReadFile(strFileName)
Dim objFSO, ts, s
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ts = objFSO.OpenTextFile(strFileName, 1)
s = ts.Readall
ts.Close
Set objFSO = Nothing
ReadFile = s
End Function

Function GetBarCode(strBarcodeLine)
Dim Arline1, Arline2, ArCell1, strBarcode
Arline1 = Split(strBarcodeLine, vbCrLf)
Arline2 = Split(Arline1(0), ",")
ArCell1 = Split(Arline2(0), "=")
strBarcode = Trim(ArCell1(1))
GetBarCode = strBarcode
End Function

Function GetPlateFormat(strPlateFormatLine)
Dim Arline1, Arline2, ArCell1, strPlateFormat
Arline1 = Split(strPlateFormatLine, vbCrLf)
Arline2 = Split(Arline1(1), ",")
ArCell1 = Split(Arline2(0), "=")
strPlateFormat = Trim(ArCell1(1))
GetPlateFormat = strPlateFormat
End Function


Function GetPlateDate(strPlateDateLine)
Dim Arline1, Arline2, ArCell1, datPlateDate
Arline1 = Split(strPlateDateLine, vbCrLf)
Arline2 = Split(Arline1(3), ",")
ArCell1 = Split(Arline2(0), "=")
datPlateDate = Trim(ArCell1(1))
GetPlateDate = datPlateDate
End Function

Function GetReceivingLab(strReceivingLabLine)
Dim Arline1, Arline2, ArCell1, strReceivingLab
Arline1 = Split(strReceivingLabLine, vbCrLf)
Arline2 = Split(Arline1(5), ",")
ArCell1 = Split(Arline2(0), "=")
strReceivingLab = Trim(ArCell1(1))
GetReceivingLab = strReceivingLab
End Function






Dim strFileRead

strFileRead = ReadFile(strImportFiles)
 
R

Richard K Bethell

Mike said:
I have some code I am working on. One function reads a file using fso, then
I have multiple functions using the string read from the file. Each
function splits the lines up and will return one value.

Is there a better way? Is handling this many array which are basically the
same going to cause some server distress?

Are these operations all performed all at once, or one at a time? Better to
read the file each time if they happen one at a time. However, if you are
calling these all at once, read the file once, and pass it around as
function arguments.
 
W

William Morris

If you can do the split once, it'll make the process more efficient. I do
similar stuff in my own code - if there's going to be an instance where I'm
opening - closing - opening - closing...I try very very hard to open and
close only once. The same goes for any process where I could potentially
duplicate the same action/set of actions several times.

- Wm
 
M

Mike

I am reading the file only once and setting it to a variable. That variable
is then split multiple times.

Thanks
Mike
 
D

dlbjr

strFilePath = 'Set file path here.
Set I = New Info
I.ParseFile(strFilePath)
strBarcode = I.BarCode
strPlateFormat = I.PlateFormat
strPlateDate = I.PlateDate
strReceivingLab = I.ReceivingLab
Set I = Nothing


Class Info
Private maryLines
Private mdblLines
Private mstrBarCode
Private mstrPlateFormat
Private mstrPlateDate
Private mstrReceivingLab

Private Sub Class_Initialize()
mdblLines = 0
mstrBarCode = ""
mstrPlateFormat = ""
mstrPlateDate = ""
mstrReceivingLab = ""
End Sub

Private Sub Class_Terminate()

End Sub

Public Property Get BarCode()
BarCode = mstrBarCode
End Property

Public Property Get PlateFormat()
PlateFormat = mstrPlateFormat
End Property

Public Property Get PlateDate()
PlateDate = mstrPlateDate
End Property

Public Property Get ReceivingLab()
ReceivingLab = mstrReceivingLab
End Property

Public Sub ParseFile(strPath)
On Error Resume Next
Set FSO = CreateObject("Scripting.FileSystemObject")
Set File = FSO.OpenTextFile(strPath, 1)
strData = File.ReadAll
File.Close
Set File = Nothing
Set FSO = Nothing
If Len(strData) > 0 Then
maryLines = Split(strData,vbCrLf)
mdblLines = UBound(maryLines) + 1
mstrBarCode = ParseLine(0)
mstrPlateFormat = ParseLine(1)
mstrPlateDate = ParseLine(3)
mstrReceivingLab = ParseLine(5)
End If
End Sub

Private Function ParseLine(intLine)
If CDbl(mdblLines) > CDbl(intLine) Then
arData1 = Split(maryLines(intLine),",")
arData2 = Split(arData1(0),"=")
ParseLine = Trim(arData2(1))
End If
End Function
End Class

-dlbjr

Discerning resolutions for the alms
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top