I have files that have extension of TSV which are text files but viewable in exel. I figured out a way for the user to click on a button in Access which does the following
1. Run Macro in Excel: The macro prompts the user to select the TSV file. After selection, macro opens the employee.tsv file in the excel (with excel being invisible) and saves it as employee.xls
The code for the macro may be its helpful for anyone else:
The code for running Excel Macro from Access
2. Imports the Excel file (employee.xls) into two tables: tblEmployee and tblDepartment using the following codes.
Everything is working flawless except that the user has to select the file three times
1 time for the tsv
2 times for the xls file
Is there a way that the user can select the file only once (tsv file) or
at least only twice one of the tsv file and the other for the xls file?
Thank you
1. Run Macro in Excel: The macro prompts the user to select the TSV file. After selection, macro opens the employee.tsv file in the excel (with excel being invisible) and saves it as employee.xls
The code for the macro may be its helpful for anyone else:
Code:
Sub SaveTSVtoXLS()
Dim myPath As String
Dim myString As Variant
Application.DisplayAlerts = False
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Show
myPath = .SelectedItems(1)
End With
Workbooks.Open Filename:=myPath
myString = Split(myPath, ".")
myPath = myString(0)
ActiveWorkbook.SaveAs Filename:=myPath & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
Application.DisplayAlerts = True
End Sub
Code:
'---------------------------------------------------------------------------------------
' Procedure : RunXLSMacro
' Author : CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Open the specifed Excel workbook and run the specified macro and then
' close the workbook.
' Copyright : The following may be altered and reused as you wish so long as the
' copyright notice is left unchanged (including Author, Website and
' Copyright). It may not be sold/resold or reposted on other sites (links
' back to this site are allowed).
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2010-Sep-09 Initial Release
'---------------------------------------------------------------------------------------
Sub RunExcelMacro()
Dim xl As Object
'Step 1: Start Excel, then open the target workbook.
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open ("H:\ConvertTSVtoXLSX.xlsm")
'Step 2: Make Excel visible
xl.Visible = True
'Step 3: Run the target macro
xl.Run "SaveTSVtoXLS"
'Step 4: Close and save the workbook, then close Excel
xl.ActiveWorkbook.Close (True)
xl.Quit
'Step 5: Memory Clean up.
Set xl = Nothing
End Sub
Code:
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "qryDepartment", selectFile, True - 1, "A1:C2"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "qryEmployee", selectFile, True - 1, "A1:AE2"
1 time for the tsv
2 times for the xls file
Is there a way that the user can select the file only once (tsv file) or
at least only twice one of the tsv file and the other for the xls file?
Thank you