Okay , here's a rough outline for you. It should give you general idea of how I was thinking of handling the problem, but please keep in mind I'm by no means an expert. ^_^;; <-(sheepish look) ((baa!))
1. I'm assuming you're importing the original text file into Access as a table.
2. I'm naming this table [tblData]
2a. I'm assuming [tblData] has two fields (in this mockup), [CustID] and [CustName]
2b. I'm assuming that [CustID] is a Long (autonumber) and [CustName] is a string.
2c. I'm assuming the [CustID] is unique and that [CustName] may have duplicates. (ie. two John Smith's)
3. I'm creating a throw-away query called [qrySQL]
So with these assumptions:
- Private Sub ExportFile()
-
Dim rsCustID As DAO.Recordset
-
Dim strSQL As String
-
Dim strFileName As String
-
-
'First we need to get a list of the unique CustIDs
-
strSQL = "SELECT DISTINCT tblData.CustID"
-
strSQL = strSQL & " FROM tblData"
-
strSQL = strSQL & " ORDER BY tblData.CustID;"
-
-
'Open a copy of this query as a recordset
-
Set rsCustID = CurrentDb.OpenRecordset(strSQL)
-
-
'I always assume an empty recordset is possible, so...
-
If Not ((rsCustID.EOF) And (rsCustID.BOF)) Then
-
rsCustID.MoveFirst
-
-
'Loop until you hit the end of the query/recordset
-
Do While Not (rsCustID.EOF)
-
-
'First we need to filter the table to the current CustID
-
strSQL = "SELECT tblData.CustID, tblData.CustName"
-
strSQL = strSQL & " FROM tblData"
-
strSQL = strSQL & " WHERE (((tblData.CustID)=" & rsCustID!CustID & "));"
-
CurrentDb.QueryDefs("qrySQL").SQL = strSQL
-
-
'Since we pulled the unique CustID from the table, it should be safe to
-
' assume that there will be data to export
-
strFileName = "c:\my documents\" & rsCustID!CustName & ".csv"
-
DoCmd.TransferText acExportDelim, , "qrySQL", strFileName
-
-
rsCustID.MoveNext
-
Loop
-
-
MsgBox "Export Complete."
-
End If
-
-
End Sub
-
-
Now obviously there isn't as much error trapping in this as there should be, but that should give you a rough idea of what I was thinking. ^_^