Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: Create a File Automatically


Message #1 by "George Oro" <george@c...> on Mon, 23 Sep 2002 18:51:14 +0400
Hi Guys,

Can I create a file using VBA? What I mean is, once I run the code it will create a file, e.g. TestFile.txt and it will be save
automatically in C:\My Documents.

TIA,
George

Message #2 by "Gerald, Rand" <RGerald@u...> on Mon, 23 Sep 2002 12:37:36 -0500
Hi George,

Here is a code sample which may help.

' =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D begin code 
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Public Sub CopyTableToText()

    Dim mydb As DAO.Database
    Dim AMI As DAO.Recordset
   
    Dim Msg As String, PMH As String, CallForHelp As String, strInData 
As
String, strOutData As String
   
    strInData =3D "tblMyTable"    'the name of the table/query which 
contains
the data fields named as below
    strOutData =3D "C:\TEMP\MyTestTable2.csv"  ' the name of the output 
text
file
    Set mydb =3D CurrentDb()
    Set AMI =3D mydb.OpenRecordset(strInData)
   
    Open strOutData For Output As #1 'the output file
   
    With AMI
        While Not .EOF
            Print #1, Chr$(34) & ![ID] & Chr$(34) & Chr$(44);
            Print #1, Chr$(34) & Format$(![MyDate], "dd/mm/yyyy") & 
Chr$(34)
& Chr$(44);
            .MoveNext
        Wend
        .Close
    End With
   
    Close #1
    Set AMI =3D Nothing
    Set mydb =3D Nothing
    Msg =3D "All records exported to file " & strOutData
    MsgBox Msg, vbInformation, "Export complete"
      
End Sub

' =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D end code 
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=3D=3D

Good Luck!  Let me know if it works OK.

Rand E Gerald
Database Specialist
Information Services / Operations
Bah=E1'=ED National Office
1233 Central St.
Evanston IL 60201
(xxx) xxx-xxxx

-----Original Message-----
From: George Oro [mailto:george@c...]
Sent: Monday, September 23, 2002 9:51 AM
To: Access
Subject: [access] Create a File Automatically

Hi Guys,

Can I create a file using VBA? What I mean is, once I run the code it 
will
create a file, e.g. TestFile.txt and it will be save
automatically in C:\My Documents.

TIA,
George


Message #3 by John Fejsa <John.Fejsa@h...> on Tue, 24 Sep 2002 08:42:26 +1000
Hi George,

You could use something like this...

Open "TESTFILE" For Output As #1 ' Open file for output.
Print #1, "This is a test" ' Print text to file.
Print #1, ' Print blank line to file.
Print #1, "Zone 1"; Tab ; "Zone 2" ' Print in two print zones.
Print #1, "Hello" ; " " ; "World" ' Separate strings with space.
Print #1, Spc(5) ; "5 leading spaces " ' Print five leading spaces.
Print #1, Tab(10) ; "Hello" ' Print word at column 10.

' Assign Boolean, Date, Null and Error values.
Dim MyBool, MyDate, MyNull, MyError
MyBool = False : MyDate = #February 12, 1969# : MyNull = Null
MyError = CVErr(32767)
' True, False, Null, and Error are translated using locale settings of 
' your system. Date literals are written using standard short date 
' format.
Print #1, MyBool ; " is a Boolean value"
Print #1, MyDate ; " is a date"
Print #1, MyNull ; " is a null value"

Print #1, MyError ; " is an error value"
Close #1 ' Close file.

Check Access help for more info...

Hope that helps.

____________________________________________________

John Fejsa
Systems Analyst/Computer Programmer
Hunter Centre for Health Advancement
Locked Bag 10, WALLSEND NSW 2287
Phone: (02) 4924 6336 Fax: (02) 4924 6209
www.hcha.org.au
____________________________________________________

The doors we open and close each day decide the lives we live
____________________________________________________

CONFIDENTIALITY & PRIVILEGE NOTICE
The information contained in this email message is intended for the named addressee only.  If you are not the intended recipient you
must not copy, distribute, take any action reliant on, or disclose any details of the information in this email to any other person
or organisation.  If you have received this email in error please notify us immediately.

>>> george@c... 24/09/2002 0:51:14 >>>
Hi Guys,

Can I create a file using VBA? What I mean is, once I run the code it will create a file, e.g. TestFile.txt and it will be save
automatically in C:\My Documents.

TIA,
George



Message #4 by "Lorenzo Amado Zaragoza" <enzaux@g...> on Tue, 24 Sep 2002 09:06:18 +0800
	Yes, try looking about the Open, Input, Print statement.

Enzo

-----Original Message-----
From: George Oro [mailto:george@c...]
Sent: Monday, September 23, 2002 10:51 PM
To: Access
Subject: [access] Create a File Automatically


Hi Guys,

Can I create a file using VBA? What I mean is, once I run the code it will create a file, e.g. TestFile.txt and it will be save
automatically in C:\My Documents.

TIA,
George





Message #5 by "George Oro" <george@c...> on Tue, 24 Sep 2002 11:34:29 +0400
Perfect!!!

Appreciated...
Cheers,
George


-----Original Message-----
From: Gerald, Rand [mailto:RGerald@u...]
Sent: Monday, September 23, 2002 9:38 PM
To: Access
Subject: [access] RE: Create a File Automatically
Importance: High


Hi George,

Here is a code sample which may help.

' =========== begin code ==============

Public Sub CopyTableToText()

    Dim mydb As DAO.Database
    Dim AMI As DAO.Recordset

    Dim Msg As String, PMH As String, CallForHelp As String, strInData As
String, strOutData As String

    strInData = "tblMyTable"    'the name of the table/query which contains
the data fields named as below
    strOutData = "C:\TEMP\MyTestTable2.csv"  ' the name of the output text
file
    Set mydb = CurrentDb()
    Set AMI = mydb.OpenRecordset(strInData)

    Open strOutData For Output As #1 'the output file

    With AMI
        While Not .EOF
            Print #1, Chr$(34) & ![ID] & Chr$(34) & Chr$(44);
            Print #1, Chr$(34) & Format$(![MyDate], "dd/mm/yyyy") & Chr$(34)
& Chr$(44);
            .MoveNext
        Wend
        .Close
    End With

    Close #1
    Set AMI = Nothing
    Set mydb = Nothing
    Msg = "All records exported to file " & strOutData
    MsgBox Msg, vbInformation, "Export complete"

End Sub

' ================ end code ==========================

Good Luck!  Let me know if it works OK.

Rand E Gerald
Database Specialist
Information Services / Operations
Bahá'í National Office
1233 Central St.
Evanston IL 60201
(xxx) xxx-xxxx

-----Original Message-----
From: George Oro [mailto:george@c...]
Sent: Monday, September 23, 2002 9:51 AM
To: Access
Subject: [access] Create a File Automatically

Hi Guys,

Can I create a file using VBA? What I mean is, once I run the code it will
create a file, e.g. TestFile.txt and it will be save
automatically in C:\My Documents.

TIA,
George





  Return to Index