Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Access: Recordset, File I/O Example
By Richard Rost   Richard Rost on LinkedIn Email Richard Rost   18 years ago

Q: Hello, I need to insert a lot of of separate .HTML files into Access. Each HTML file would have to be inserted in a memo field. So that the memo field contains the HTML code. Until now I have been doing this with a keyboard macro which opens the HTML source code and copies the content into a memo field in an Access database. This is very time consuming and certainly not error free. I need to insert about 60000 HTML files which are all between 4 and 7 kb. Thank you for your time, Arthur


A: If these are stored as files on your hard drive (MyFile.HTM) then I would use basic File I/O to insert them into your database, using a RecordSet to do the actual input (you can't use a simple INSERT INTO query because of invalid characters like quotation marks).

I would do something like this:


    ' You must have Microsoft DAO x.x Object Library installed
    ' In your VB Editor, go to Tools > References.
    ' Make sure DAO is checked and has a HIGHER PRIORITY than
    ' the Microsoft ActiveX Data Objects x.x Library.
    
    Dim FN As String
    Dim S As String
    Dim MyString As String
    Dim db As database
    Dim rs As Recordset
    
    Set db = CurrentDb()
    Set rs = db.openrecordset("MyTable", dbopendynaset)
    
    DoCmd.SetWarnings False
    FN = Dir("C:\*.TXT", vbNormal)
    While FN <> ""
        Open "C:\" & FN For Input As #1
            MyString = ""
            While Not EOF(1)
                Line Input #1, S
                MyString = MyString & S & vbNewLine
            Wend
            rs.AddNew
            rs!MyMemoField = MyString
            rs.Update
        Close #1
        FN = Dir
    Wend
    DoCmd.SetWarnings True



A bit of an explanation... first you have to make sure you have the Microsoft DAO (Data Access Objects) library reference installed. I'm a little old-school. I like DAO better than ADO (ActiveX Data Objects). Yes, yes, yes, I'll cover both in my class, but when I need to throw something together quick, I still use DAO.

Then we DIM a bunch of local variables and set up a DB database connection and RS recordset connection to a table called MyTable (change to whatever your table is).

Next I turn off Access' warnings - otherwise each time you insert a record in a table you'll get a "you're about to insert 1 record" warning.

Then, I use the DIR command to loop through all of the TXT files in my C:\ folder. Now, if you're doing HTML files, you'll need to set this to either *.HTM or *.HTML whichever you're working with, and of course type in the complete folder name, like "C:\My Documents\*.HTML" or whatever.

Next, I open each file as we come to it, read in all of its lines, and save that information into the table using the recordset connection (rs.Addnew stuff).

Close the file, loop to the next record, and then you're done when you're out of files.

This whole thing took me about 10 minutes to write... and HALF AN HOUR to explain. Ha ha ha. Yes, I'll cover all of this in more depth when I cover RECORDSETS in my next Access tutorial.

I do cover the basic File I/O commands in my VISUAL BASIC 201 and 202 classes http://www.599cd.com?GOVB201 but I will also be going over them in the Access tutorials as well, since I have a lot of Access students who don't take (or need) just the VB lessons.

Hope this answers your question.

Comments for Access: Recordset, File I/O Example
 
Age Subject From
18 yearsNo SubjectRichard Rost
18 yearsNo SubjectAlan Hill

 

Start a NEW Conversation
 
Only students may post on this page. Click here for more information on how you can set up an account. If you are a student, please Log On first. Non-students may only post in the Visitor Forum.
 
Subscribe
Subscribe to Access: Recordset, File I/O Example
Get notifications when this page is updated
 
 
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/2/2026 12:40:44 AM. PLT: 0s
Keywords: access recordset file io dao ado tips  PermaLink  Access: Recordset, File I/O Example