HunterN
Active Member
- Joined
- Mar 19, 2002
- Messages
- 479
Hi all,
I am using the following code to write out an Excel file from my ACCESS database:
The reason I am using the SQL statement is because the database table has one field that I don't want to be copied into Excel.
This writes my Excel file correctly. And it uses the FileExtStr = "xlsx" and FileFormatNum = 51.
So my problem is when trying to read this file back out into Access. I use the following to try to import it back. But it does not pull anything back into Access.
I think it has to do with the spreadsheet types. I have tried to research this for a couple of days now and have come up with no good results. Can anyone help on reading back in a 'xlsx' file that I created.
I'd appreciate any suggestions.
Thanks, Nancy
I am using the following code to write out an Excel file from my ACCESS database:
Rich (BB code):
Sub ExporttoExcel()
'Working in Excel 97-2010
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim myCol As String
Dim iCols As Long
Dim sName As String
Dim i As Long
Dim outFile As String
Dim holdName As String
Dim appExcel As Excel.Application
Dim wbk As Excel.Workbook
Dim wks As Excel.Worksheet
Dim dbs As DAO.Database 'Data access object(DAO)
Dim rst As DAO.Recordset
Dim sSQL As String
Const cTabTwo As Byte = 1
If NewSup Then
outFile = curPath & hold_Year & "\" & supName
Else
If SpiderFile Then
'* Take out SPIDER from the file name
holdName = Left(shortFileN, InStr(shortFileN, "SPIDER") - 2)
outFile = curPath & hold_Year & "\" & holdName
Else
outFile = curPath & hold_Year & "\" & shortFileN
End If
End If
sSQL = "SELECT [VarName], [Size], [LongDescription], [StartPosition], " _
& "[StopPosition], [Action], [ShortDesc], [EditedUniverse], " _
& "[ValidEntries], [DataType], [Variable], [Start_MM], [Start_YY], " _
& "[Stop_MM], [Stop_YY], [Weight], [Source] " _
& "FROM Puffin_db_Table"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(sSQL, dbOpenSnapshot)
Set appExcel = Excel.Application
Set wbk = appExcel.Workbooks.Add
appExcel.DisplayAlerts = True
appExcel.Visible = True
Set wks = appExcel.Worksheets(cTabTwo)
appExcel.ScreenUpdating = True
'* Copy the Field Names to the Excel sheet and Align and Bold them
For iCols = 0 To rst.Fields.Count - 1
wks.Cells(5, iCols + 1).Value = rst.Fields(iCols).Name
myCol = ColumnNumberToLetters(iCols + 1)
wks.Columns(myCol).VerticalAlignment = xlVAlignTop
Next
wks.Range(wks.Cells(5, 1), _
wks.Cells(5, rst.Fields.Count)).Font.Bold = True
Range("A2").Value = "Public Use File"
If SpiderFile Then
Range("A3").Value = holdName
Else
Range("A3").Value = ShortN
End If
wks.Range("A6").CopyFromRecordset rst
Set Sourcewb = ActiveWorkbook
With Sourcewb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End With
' *************************************************************************
'* Save the new workbook and close it
TempFilePath = curPath & hold_Year & "\"
If SpiderFile Then
TempFileName = holdName
Else
TempFileName = ShortN
End If
With Sourrcewb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
.Close SaveChanges:=True
End With
appExcel.Quit
appExcel.DisplayAlerts = True
MsgBox "You can find the new file in " & TempFilePath
End Sub
The reason I am using the SQL statement is because the database table has one field that I don't want to be copied into Excel.
This writes my Excel file correctly. And it uses the FileExtStr = "xlsx" and FileFormatNum = 51.
So my problem is when trying to read this file back out into Access. I use the following to try to import it back. But it does not pull anything back into Access.
Rich (BB code):
If Right(wbFilename, 4) = "xlsx" Then
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, _
"Puffin_db_Table", mySelectedFile, True, "A5:Q" & FinalRow
I think it has to do with the spreadsheet types. I have tried to research this for a couple of days now and have come up with no good results. Can anyone help on reading back in a 'xlsx' file that I created.
I'd appreciate any suggestions.
Thanks, Nancy