In the following code, I'm wanting to export a table to text format, but in my efforts so far, I'm getting a file name to the extent of filename#txt instead of filename.txt. Any ideas as to what is causing this?
The code resides within a form, and is run off the Button Click for the named button.
Thanks in advance for any thoughts or suggestions.
The code resides within a form, and is run off the Button Click for the named button.
Code:
Private Sub cmdExportText_Click()
Dim vrtSelectedItem As Variant
Dim strSQL As String
Dim strTableName As String, strCount As String, strTables As String
Dim strFilePath() As String, strFileName() As String
Dim strNewFilePath As String, strNewFileName As String
Dim x As Integer [GREEN]'For Looping through path string array[/GREEN]
txtTable.SetFocus
strTables = txtTable.Text
strTableName = txtTable.Text & "_Random_" & Format(Date, "yyyymmdd")
txtSampleCount.SetFocus
strCount = txtSampleCount.Text
cmdExportText.SetFocus
strSQL = "SELECT TOP " & strCount & " [" & strTables & "].* " & _
"INTO [" & strTableName & "] " & _
"FROM [" & strTables & "] " & _
"ORDER BY [" & strTables & "].Random;"
Debug.Print strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
strFilePath = Split(FileName, "\") [GREEN]'Pulling FileName from the form's recordset, pointing at the tblImports table[/GREEN]
strFileName = Split(strFilePath(UBound(strFilePath)), ".")
For x = 0 To UBound(strFilePath) - 1
If strNewFilePath = vbNullString Then
strNewFilePath = strFilePath(x) & "\"
Else
strNewFilePath = strNewFilePath & strFilePath(x) & "\"
End If
Next x
Select Case strFileName(UBound(strFileName))
Case "txt", "csv"
[GREEN]'Don't need to add any worksheet/table name, since a text file IS the table/sheet/data set.[/GREEN]
Case "xls", "xlsx", "xlt"
strNewFileName = strFileName(0) & ObjectName 'Add worksheet name if applicable
Case "mdb"
strNewFileName = strFileName(0) & ObjectName 'Add table name if applicable
Case Other
[GREEN]'For now, do nothing, but going forward, perhaps can create a log file of all imported file types in case
'Anything new pops up - 20100127[/GREEN]
End Select
strNewFileName = strFileName(0) & Chr(46) & "txt" 'adds .txt extension to file name, regardless of what original was.
[GREEN]'Chr(46) is the ACII Decimal code for a period.
'This use is to overcome period being changed to # - 20100518 - did not make any difference[/GREEN]
strNewFilePath = strNewFilePath & strNewFileName
Debug.Print strNewFilePath
DoCmd.TransferText acExportDelim, "ExportTextSpec", strTableName, strNewFilePath
[GREEN] 'Might be good to open an Explorer Window showing the new file after it's been exported...
'and maybe check to be sure the folder path isn't already open first... [/GREEN]
vrtSelectedItem = Empty
strSQL = vbNullString
Erase strFilePath
Erase strFileName
End Sub
Thanks in advance for any thoughts or suggestions.