The CSV files all output data but I need them to output as a text datatype. Right now when I open the CSV file in EXCEL the datatype is GENRAL. Is there a way when I am exporting to CSV to make sure that the datatype is text.
The reason is, I have to then take these files and upload them as a text csv file.
I made sure that the table that is being exported is a text field.
Please see the code below
Anyone have any ideas?
Private Sub cmdSplit_Click()
'************************************************************************************************************
'Event Name: cmdSplit_Click()
'Purpose: Process will import OLQ file user selected in cmdBrowse_Click() and export data into
' individual files by state with the required fields. Also populate filename in listbox.
'Created Date: 02-01-2006
'Last Updated: NA
'************************************************************************************************************
'On Error GoTo Err_cmdSplit_Click
'Warnings Off
DoCmd.SetWarnings False
'Make sure user selected an import file during browse mode
txtImportFile.SetFocus
If txtImportFile.Text <> "" Then
'Show status window
DoCmd.OpenForm "frmStatus", acNormal
'Declare for importing tables
Dim ShtName As String
Dim objSrc As Object
Dim X As Long
Dim tblName As String
'Declare for splitting files
Dim dbs As DAO.Database
Dim rstStates, rstOutput As DAO.Recordset
Dim FileHandle As Integer
Set dbs = CurrentDb()
'********************************************************************************************************
'IMPORT OLQ FILE AND RUN QUERIES
'********************************************************************************************************
DoCmd.RunSQL "DELETE * FROM tbl_OLQ_IMPORT;"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tbl_OLQ_IMPORT", txtImportFile.Text
'Update Status window
[Forms]![frmStatus]![lblImportingDone].Visible = True
'Update Status window
[Forms]![frmStatus]![lblPulling].Visible = True
DoCmd.OpenQuery "qry_OLQ_AGTS", acViewNormal
DoCmd.OpenQuery "qry_OLQ_CORP", acViewNormal
'Update Status window
[Forms]![frmStatus]![lblPullingDone].Visible = True
'********************************************************************************************************
'AGENTS
'********************************************************************************************************
'Update Status window
[Forms]![frmStatus]![lblSplitting].Visible = True
'Split files
Set rstStates = dbs.OpenRecordset("SELECT DISTINCT STATE FROM tbl_OLQ_AGTS;", dbOpenDynaset)
'Empty Listbox on main form
lstFiles.SetFocus
For i = 0 To lstFiles.ListCount - 1
If i > lstFiles.ListCount - 1 Then
lstFiles.RemoveItem (i)
lstFiles.Requery
End If
Next i
'Create folder for CSV files
strPath = Dir("L:\Finance\AMAccessApplications\ExpiredLicUpload\SplitOLQ\" & Format(Date, "yyyymmdd") & "\", vbDirectory)
If strPath <> "" Then
' It Exists
strPath = "L:\finance\AMAccessApplications\ExpiredLicUpload\SplitOLQ\" & Format(Date, "yyyymmdd") & "\"
Else
' Does not Exist
MkDir ("L:\Finance\AMAccessApplications\ExpiredLicUpload\SplitOLQ\" & Format(Date, "yyyymmdd")) & "\"
strPath = "L:\finance\AMAccessApplications\ExpiredLicUpload\SplitOLQ\" & Format(Date, "yyyymmdd") & "\"
End If
'Split files
Do Until rstStates.EOF
If rstStates![State] <> "" Then
FileHandle = FreeFile
Open strPath & Format(Date, "yyyymmdd") & "_AGT_" & rstStates![State] & ".csv" For Output As #FileHandle
Set rstOutput = dbs.OpenRecordset("SELECT * FROM tbl_OLQ_AGTS WHERE STATE = '" & rstStates![State] & "';", dbOpenDynaset)
'If ShtName = "CORP" Then
Do Until rstOutput.EOF
Write #FileHandle, rstOutput![SSN], rstOutput![Name]
rstOutput.MoveNext
Loop
lstFiles.AddItem strPath & Format(Date, "yyyymmdd") & "_AGT_" & rstStates![State] & ".csv"
Close #FileHandle
End If
rstStates.MoveNext
Loop
'********************************************************************************************************
'CORP
'********************************************************************************************************
'Get all states from OLQ file
Set rstStates = dbs.OpenRecordset("SELECT DISTINCT STATE FROM tbl_OLQ_CORP;", dbOpenDynaset)
'Create folder for CSV files
strPath = Dir("L:\Finance\AMAccessApplications\ExpiredLicUpload\SplitOLQ\" & Format(Date, "yyyymmdd") & "\CORP\", vbDirectory)
If strPath <> "" Then
' It Exists
strPath = "L:\finance\AMAccessApplications\ExpiredLicUpload\SplitOLQ\" & Format(Date, "yyyymmdd") & "\CORP\"
Else
' Does not Exist
MkDir ("L:\Finance\AMAccessApplications\ExpiredLicUpload\SplitOLQ\" & Format(Date, "yyyymmdd") & "\CORP\")
strPath = "L:\finance\AMAccessApplications\ExpiredLicUpload\SplitOLQ\" & Format(Date, "yyyymmdd") & "\CORP\"
End If
'Split files
Do Until rstStates.EOF
If rstStates![State] <> "" Then
FileHandle = FreeFile
Open strPath & Format(Date, "yyyymmdd") & "_CORP_" & rstStates![State] & ".csv" For Output As #FileHandle
Set rstOutput = dbs.OpenRecordset("SELECT * FROM tbl_OLQ_CORP WHERE STATE = '" & rstStates![State] & "';", dbOpenDynaset)
'If ShtName = "CORP" Then
Do Until rstOutput.EOF
Write #FileHandle, rstOutput![SSN]
rstOutput.MoveNext
Loop
lstFiles.AddItem strPath & Format(Date, "yyyymmdd") & "_CORP_" & rstStates![State] & ".csv"
Close #FileHandle
End If
rstStates.MoveNext
Loop
'Update Status window
[Forms]![frmStatus]![lblSplittingDone].Visible = True
[Forms]![frmStatus]![cmdClose].Enabled = True
[Forms]![frmStatus].Caption = "Process Successful!"
Else
'Error Message to User
MsgBox "You must browse for an import file.", vbCritical, "Error..."
End If
'Warnings On
DoCmd.SetWarnings True
'Exit_cmdSplit_Click:
' Exit Sub
'Err_cmdSplit_Click:
' MsgBox "Error Number: " & Err.Number & vbCrLf & vbCrLf & "Error Description: " & Err.Description
' Resume Exit_cmdSplit_Click
End Sub