Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 5th, 2005, 01:57 AM
Registered User
 
Join Date: Oct 2005
Location: Bucharest, , Romania.
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?

Reply With Quote
  #2 (permalink)  
Old October 5th, 2005, 07:03 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

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.AddNew
        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
        objRS.Update
        objRS.Close
        objConn.Close
        i = i + 1
Loop

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

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

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

HTH

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.


mmcdonal
Reply With Quote
  #3 (permalink)  
Old October 5th, 2005, 06:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Melbourne, Vic, Australia.
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That sounds much more complicated than something along the lines of:
Code:
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
Reply With Quote
  #4 (permalink)  
Old October 6th, 2005, 02:43 AM
Registered User
 
Join Date: Oct 2005
Location: Bucharest, , Romania.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank's a lot.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:51 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.