A command button (Access) that saves the current record in a file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

The command button which would simply create a text file that would contain
some information on the current record selected. For example, the current
record selected (say record no4), contains a Lot ID (say, N12453.1), a device
Name, (AD8618ARCU-SHRT), and Qty (say 200units). I would like to save these
three entries automatically in a text file by simply clicking the command
button.

The result is after clicking the button a new file was created say it was
stored at C:\sample.txt. The sample.txt should contain:
N12453.1
AD8618ARCU-SHRT
200
 
Try this.

================= Start Code ============================
'First set a reference to "Microsoft Scripting Runtime"
'In the onclick event of command button
'try this code.
'Make changes as appropriate
Private Sub cmdInsert_Click()
Dim fso As New FileSystemObject
Dim ts As TextStream

Dim strFilePathAndName As String
strFilePathAndName = "C:\Sample.TXT"

Dim rst As DAO.Recordset

'Assuming your current data is in a subForm control
Set rst = Me.frmTextFileDemoSub.Form.RecordsetClone

'set recordset object rst to current record
rst.Bookmark = Me.frmTextFileDemoSub.Form.Bookmark

'Create a new file for writing
'If text file already exists,it will be overwritten
'otherwise a new file will be created
Set ts = fso.CreateTextFile(strFilePathAndName, True)

'Write field values in text file
ts.WriteLine rst!LotID
ts.WriteLine rst!DeviceName
ts.WriteLine rst!Qty

'Close the file.
ts.Close

'Close the recordset
Set rst = Nothing
End Sub
============================= End Code ============================

Surendran

SurendranSNV at Hotmail dot com
 
Back
Top