Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / December 2005

Tip: Looking for answers? Try searching our database.

Open/Read file in Vb

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ben - 28 Dec 2005 16:42 GMT
is it possible to open and read txt files in vba? the files im reading arent
to be used as imports for tables, they just contain text that i need to
search.

thanks for any help
ben
Dirk Goldgar - 28 Dec 2005 16:52 GMT
> is it possible to open and read txt files in vba? the files im
> reading arent to be used as imports for tables, they just contain
> text that i need to search.

VB has built-in I/O support;  look up the Open, Input, and Close
statements, and the Input() and FreeFile() functions, in the VB online
help.

Here's a simple example to read the complete contents of a file (no more
than 2GB in size) into a String variable:

   Dim strFileName As String
   Dim strFileText As String
   Dim intFileNo As Integer

   strFileName = "C:\Temp\TestReport.txt"

   intFileNo = FreeFile()
   Open strFileName For Input As #intFileNo
   strFileText = Input(LOF(intFileNo), intFileNo)
   Close #intFileNo

Note: you can also use methods from the Microsoft Scripting Library to
do this sort of thing, but for simple I/O, using the native methods is
more efficient and reliable.

Signature

Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Tom Lake - 28 Dec 2005 19:29 GMT
> is it possible to open and read txt files in vba? the files im reading
> arent
> to be used as imports for tables, they just contain text that i need to
> search.

The usual BASIC syntax works in VBA in Access

Open "myfile.txt" For Input As #1
Do While Not Eof(1)
   Line Input #1, a$
Loop

will work.
Douglas J. Steele - 28 Dec 2005 20:46 GMT
Never a good idea to hard code the file number:

Dim intFile As Integer
Dim strInput As String

intFile = FreeFile()
Open "myfile.txt" For Input As #intFile
Do While Not Eof(intFile)
   Line Input #intFile, strInput
Loop
Close #intFile

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

>> is it possible to open and read txt files in vba? the files im reading
>> arent
[quoted text clipped - 9 lines]
>
> will work.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.