Checking contents of a text file

  • Thread starter Thread starter Steve C
  • Start date Start date
S

Steve C

Hello,

Can I make access check the contents of a text file?

If I have a text file, say, test.txt. Can I have Access
2003 check to see that it contains "THISISATEST"?
Then, if it DOES contain it, do one thing, and if it
DOESN'T do another?

It would be best if it will only report an EXACT match
(for e.g. the text file ONLY contains the specified
words), but any way is better than no way!!!

Thanks!
 
You can open and read lines from a text file in ACCESS using VBA.

Dim varArray As Variant
Dim strLine As String
Const strFile As String = "D:\Ken.txt"
Const strDelimiter As String = "|"
Open strFile For Input As #1
Do While EOF(1) = False

' USE ONLY ONE OF THESE NEXT TWO CODE BLOCKS

' reads until it finds a "comma" (delimiter)
Input #1, strLine

' reads entire line
Line Input #1, strLine
varArray = Split(strLine, strDelimiter)

' Do something with the information that has been read
' ....

Loop
Close #1
 
Hi Steve,

The FileContents() function below reads the contents of a textfile into
a variable in VBA. If your text files aren't too big (a few hundred
kilobytes shouldn't be a problem) just paste it into a module in your
database. Then use something like this air code:

Dim strFileSpec As String
Dim strTarget As String

strFileSpec = "D:\Folder\test.txt"
strTarget = "THISISATEST"
If InStr(FileContents(strFileSpec), strTarget) > 0 Then
'Do something

Else
'Do something else

End If


Function FileContents(FileSpec As Variant, _
Optional ReturnErrors As Boolean = False, _
Optional ByRef ErrCode As Long) As Variant
'Retrieves contents of file as a string
'Silently returns Null on error unless
' ReturnErrors is true, in which case
' uses CVErr() to return an error value.
' Optionally, you can retrieve the error
' code in the ErrCode argument

Dim lngFN As Long

On Error GoTo Err_FileContents
If IsNull(FileSpec) Then
FileContents = Null
Else
lngFN = FreeFile()
Open FileSpec For Input As #lngFN
FileContents = Input(LOF(lngFN), #lngFN)
End If
ErrCode = 0
GoTo Exit_FileContents

Err_FileContents:
ErrCode = Err.Number
If ReturnErrors Then
FileContents = CVErr(Err.Number)
Else
FileContents = Null
End If
Err.Clear
Exit_FileContents:
Close #lngFN
End Function
 
Sorry - where abouts should I put that code?

Thanks for your help so far! Very much appreciated!

Steve
-----Original Message-----
Hi Steve,

The FileContents() function below reads the contents of a textfile into
a variable in VBA. If your text files aren't too big (a few hundred
kilobytes shouldn't be a problem) just paste it into a module in your
database. Then use something like this air code:

Dim strFileSpec As String
Dim strTarget As String

strFileSpec = "D:\Folder\test.txt"
strTarget = "THISISATEST"
If InStr(FileContents(strFileSpec), strTarget) > 0 Then
'Do something

Else
'Do something else

End If


Function FileContents(FileSpec As Variant, _
Optional ReturnErrors As Boolean = False, _
Optional ByRef ErrCode As Long) As Variant
'Retrieves contents of file as a string
'Silently returns Null on error unless
' ReturnErrors is true, in which case
' uses CVErr() to return an error value.
' Optionally, you can retrieve the error
' code in the ErrCode argument

Dim lngFN As Long

On Error GoTo Err_FileContents
If IsNull(FileSpec) Then
FileContents = Null
Else
lngFN = FreeFile()
Open FileSpec For Input As #lngFN
FileContents = Input(LOF(lngFN), #lngFN)
End If
ErrCode = 0
GoTo Exit_FileContents

Err_FileContents:
ErrCode = Err.Number
If ReturnErrors Then
FileContents = CVErr(Err.Number)
Else
FileContents = Null
End If
Err.Clear
Exit_FileContents:
Close #lngFN
End Function



Hello,

Can I make access check the contents of a text file?

If I have a text file, say, test.txt. Can I have Access
2003 check to see that it contains "THISISATEST"?
Then, if it DOES contain it, do one thing, and if it
DOESN'T do another?

It would be best if it will only report an EXACT match
(for e.g. the text file ONLY contains the specified
words), but any way is better than no way!!!

Thanks!

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.
.
 
The FileContents() function should go into a standard module in your
database. For the rest, it depends on when you want the text file to be
examined. If you want the user to initiate the process by clicking a
button, put a CommandButton on a form. In its Properties sheet, set the
On Click event to [Event Procedure] and click the ... button alongside
to display the skeleton code for the procedure; paste the code in there
and then modify it to fit your exact situation.

Sorry - where abouts should I put that code?

Thanks for your help so far! Very much appreciated!

Steve
-----Original Message-----
Hi Steve,

The FileContents() function below reads the contents of a textfile into
a variable in VBA. If your text files aren't too big (a few hundred
kilobytes shouldn't be a problem) just paste it into a module in your
database. Then use something like this air code:

Dim strFileSpec As String
Dim strTarget As String

strFileSpec = "D:\Folder\test.txt"
strTarget = "THISISATEST"
If InStr(FileContents(strFileSpec), strTarget) > 0 Then
'Do something

Else
'Do something else

End If


Function FileContents(FileSpec As Variant, _
Optional ReturnErrors As Boolean = False, _
Optional ByRef ErrCode As Long) As Variant
'Retrieves contents of file as a string
'Silently returns Null on error unless
' ReturnErrors is true, in which case
' uses CVErr() to return an error value.
' Optionally, you can retrieve the error
' code in the ErrCode argument

Dim lngFN As Long

On Error GoTo Err_FileContents
If IsNull(FileSpec) Then
FileContents = Null
Else
lngFN = FreeFile()
Open FileSpec For Input As #lngFN
FileContents = Input(LOF(lngFN), #lngFN)
End If
ErrCode = 0
GoTo Exit_FileContents

Err_FileContents:
ErrCode = Err.Number
If ReturnErrors Then
FileContents = CVErr(Err.Number)
Else
FileContents = Null
End If
Err.Clear
Exit_FileContents:
Close #lngFN
End Function



Hello,

Can I make access check the contents of a text file?

If I have a text file, say, test.txt. Can I have Access
2003 check to see that it contains "THISISATEST"?
Then, if it DOES contain it, do one thing, and if it
DOESN'T do another?

It would be best if it will only report an EXACT match
(for e.g. the text file ONLY contains the specified
words), but any way is better than no way!!!

Thanks!

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.
.
 
Back
Top