exporting to multiple files

C

curos

Hi, I have a table of employees and their contact information. I wan
to export this table but not just the normal export.

I want to export each individual row by row. So if I had a Bob Smit
at 555-3860 in one row and a Eliot Johnson in another at 555-9870. Bo
Smith would go into file 1.doc with his phone number. Eliot Johnso
would go into 2.doc with his phone number. And it would keep doin
this until all the rows are done. So if I had 25 employees total, a
the end, the exported files would go from 1.doc to 25.doc
 
J

John Nurick

Hi Curos,

You'll need to write VBA code that opens a recordset on the table and
works through it line by line, something like this:

Dim dbD As DAO.Recordset
Dim rsR as DAO.Recordset
Dim strBasePath As String
Dim lngRecNo as Long
Dim strFileName As String

Set dbD = CurrentDB()
Set rsR = dbD.OpenRecordset("MyTable")
strBasePath = "D:\Folder\"
lngRecNo = 1

Do Until rsR.EOF
strFileName = strBasePath & CStr(lngRecNo) & ".doc"
'Export first record
...
.MoveNext
lngRecNo = lngRecNo + 1
Loop

rsR.Close
Set rsR = Nothing
Set dbD = Nothing

You say "1.doc". Does this imply you want to export them to Word
documents? If so, exporting each record will mean using Automation
creating a Word document and poke the field values into it (see e.g.

Sample OLE Automation for MS Word and MS Excel
http://support.microsoft.com/?id=123859
Opening a new Word document based on a template through Automation
http://www.mvps.org/access/modules/mdl0043.htm

If you want to export to a text file, you can just assemble the fields
into a string the way you want, and then write it to disk using the
function at the end of this message, e.g.

...
With rsR
strText = .Fields("FirstName") & " " _
& .Fields("LastName") & ": " _
& .Fields("Phone")
End With
lngRetVal = WriteToFile(strText, strFileName)
If lngRetVal <> 0 Then
MsgBox "Couldn't write " & strFileName
End if
...


Function WriteToFile(Var As Variant, _
FileSpec As String, _
Optional Overwrite As Long = True) _
As Long
'Writes Var to a textfile as a string.
'Returns 0 if successful, an errorcode if not.

'Overwrite argument controls what happens
'if the target file already exists:
' -1 or True (default): overwrite it.
' 0 or False: append to it
' Any other value: abort.

Dim lngFN As Long

On Error GoTo Err_WriteToFile
lngFN = FreeFile()
'Change Output in next line to Append to
'append to existing file instead of overwriting
Select Case Overwrite
Case True
Open FileSpec For Output As #lngFN
Case False
Open FileSpec For Append As #lngFN
Case Else
If Len(Dir(FileSpec)) > 0 Then
Err.Raise 58 'File already exists
Else
Open FileSpec For Output As #lngFN
End If
End Select
Print #lngFN, CStr(Nz(Var, ""));
Close #lngFN
WriteToFile = 0
Exit Function
Err_WriteToFile:
WriteToFile = Err.Number
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top