Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 5th, 2005, 01:57 AM
Registered User
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default writing and reading files on HDD from VBA

I need to write a file on the HDD something like C:\myFile.txt from VBA (including the creation of the file if it does not exist) and than to read the content of this file from VBA. I know how to do this in other languages like C++ or Java, but I don't know if this can be done from within MS Access. Can you help me, please?

Old October 5th, 2005, 07:03 AM
Friend of Wrox
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts

Yes, this can be done as well. I parse data from a txt file into one of my apps. This is a lot of code, but it is showing how to check for the file, then open it for reading, then overwriting it with a blank copy (so data is not imported twice). Try this:

'-----Code Starts-----
Dim i As Integer
Dim strYYYY As String
Dim strLine As String
Dim strDate As String
Dim strMM As String
Dim strDD As String
Dim strYY As String
Dim strTime As String
Dim strHR As String
Dim strMin As String
Dim strSSN As String
Dim strL500 As Integer
Dim strR500 As Integer
Dim strL1000 As Integer
Dim strR1000 As Integer
Dim strL2000 As Integer
Dim strR2000 As Integer
Dim strL3000 As Integer
Dim strR3000 As Integer
Dim strL4000 As Integer
Dim strR4000 As Integer
Dim strL6000 As Integer
Dim strR6000 As Integer
Dim strL8000 As Integer
Dim strR8000 As Integer
Dim fso As Variant
Dim objStream As Variant
Dim objConn As ADODB.Connection
Dim objRS As ADODB.Recordset
Dim objFile As Variant
Dim AudioArray As Variant
Dim strSQL As String

i = 0

strYYYY = DatePart("yyyy", Date)

'Create FileSystemObject

Set fso = CreateObject("Scripting.FileSystemObject")

'Check for file existence and open if there

If fso.FileExists("C:\data.txt") Then
    Set objStream = fso.OpenTextFile("C:\data.txt", 1, False, 0)
End If

'Read File and parse data (in my file, not yours)

Do While Not objStream.AtEndOfStream
    strLine = objStream.ReadLine
    AudioArray = Split(strLine, ",")
    strDate = Right(AudioArray(0), 6)
        strMM = Left(strDate, 2)
        strDD = Right(Left(strDate, 4), 2)
        strYY = Left(strYYYY, 2) & Right(strDate, 2)
        strDate = strMM & "/" & strDD & "/" & strYY
    strTime = AudioArray(1)
        strHR = Left(strTime, 2)
        strMin = Right(strTime, 2)
        strTime = strHR & ":" & strMin
    strSSN = AudioArray(2)
    strL500 = AudioArray(9)
    strR500 = AudioArray(10)
    strL1000 = AudioArray(11)
    strR1000 = AudioArray(12)
    strL2000 = AudioArray(13)
    strR2000 = AudioArray(14)
    strL3000 = AudioArray(15)
    strR3000 = AudioArray(16)
    strL4000 = AudioArray(17)
    strR4000 = AudioArray(18)
    strL6000 = AudioArray(19)
    strR6000 = AudioArray(20)
    strL8000 = AudioArray(21)
    strR8000 = AudioArray(22)

'Connect to back end and send data

    Set objConn = CreateObject("ADODB.Connection")
    Set objRS = CreateObject("ADODB.Recordset")
    objConn.Open "DSN=Audiometer;"
    objRS.Open "SELECT * FROM tblAudiometer", objConn, 3, 3
        objRS("Date") = strDate
        objRS("Time") = strTime
        objRS("SSN") = strSSN
        objRS("L500") = strL500
        objRS("R500") = strR500
        objRS("L1000") = strL1000
        objRS("R1000") = strR1000
        objRS("L2000") = strL2000
        objRS("R2000") = strR2000
        objRS("L3000") = strL3000
        objRS("R3000") = strR3000
        objRS("L4000") = strL4000
        objRS("R4000") = strR4000
        objRS("L6000") = strL6000
        objRS("R6000") = strR6000
        objRS("L8000") = strL8000
        objRS("R8000") = strR8000
        i = i + 1

'Create new file, overwriting old file and eliminating old data

Set objFile = fso.CreateTextFile("C:\Data.txt")

MsgBox i & " File(s) Transferred"
'-----Code Ends-----


BTW, I haven't gotten around to moving the connection info off the loop, so it is opened and closed on each loop. That adds time. YOu can move the connection info before the loop to save time.

Old October 5th, 2005, 06:14 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts

That sounds much more complicated than something along the lines of:
Open "C:\myFile.txt" For Output as #1
Print #1, "something"
Print #1, "something else"
Close #1
Access help has more info

I am a loud man with a very large hat. This means I am in charge
Old October 6th, 2005, 02:43 AM
Registered User
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts

Thank's a lot.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing works then reading fails childoftv XSLT 3 April 22nd, 2006 04:09 AM
Reading / Writing from Serial Port ashu_from_india General .NET 1 April 29th, 2005 03:50 AM
Reading data from files and writing it to database enggalok VS.NET 2002/2003 1 February 24th, 2004 12:33 AM
Reading/Creating/Writing Excel FIles p_nut33 C# 1 September 2nd, 2003 12:17 PM
Reading and Writing Objects Capi JSP Basics 2 July 29th, 2003 03:26 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.