473,383 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

TransferSpreadsheet Excel xls error, but xlsx works fine

547 512MB
I want to be able to import either xls or xlsx files into the same table.
I use the following code and it works 100% for xlsx but gives an error with xls files when importing
Expand|Select|Wrap|Line Numbers
  1. Dim strPath As String
  2.  
  3. With Me
  4.     strPath = Left(.lblFile.Caption, InStrRev(.lblFile.Caption, "\"))
  5.     strPath = fsbrowse(strStart:=strPath, _
  6.                        lngType:=msoFileDialogFilePicker, _
  7.                        strPattern:="MS Excel,*.XLS; *.XLSX")
  8.  
  9.     If strPath > "" Then
  10.         .lblFile.Caption = strPath
  11.         Call DoCmd.TransferSpreadsheet(acImport, acSpreadsheetTypeExcel9, "tblDepotInvImport1", strPath, True, "")
  12.     End If
  13. End With
the module i use
Expand|Select|Wrap|Line Numbers
  1. Public Function fsbrowse(Optional strStart As String = "", _
  2.                          Optional lngType As MsoFileDialogType = _
  3.                              msoFileDialogFolderPicker, _
  4.                          Optional strPattern As String = "MS Excel,*.XLS; *.XLSX")
  5.  
  6.     Dim varEntry As Variant
  7.     Dim fdf As FileDialogFilter
  8. 'acSpreadsheetTypeExcel12xml
  9.     fsbrowse = ""
  10.     With Application.FileDialog(dialogType:=lngType)
  11.         'Set the title to match the type used from the list
  12.         .Title = "Browse for "
  13.         Select Case lngType
  14.         Case msoFileDialogOpen
  15.             .Title = .Title & "File to open"
  16.         Case msoFileDialogSaveAs
  17.             .Title = .Title & "File to SaveAs"
  18.         Case msoFileDialogFilePicker
  19.             .Title = .Title & "File"
  20.         Case msoFileDialogFolderPicker
  21.             .Title = .Title & "Folder"
  22.         End Select
  23.  
  24.         Call .Filters.clear
  25.         For Each varEntry In Split(strPattern, "~")
  26.             Call .Filters.add(Description:=Split(varEntry, ",")(0), _
  27.                               Extensions:=Split(varEntry, ",")(1))
  28.         Next varEntry
  29.         'Set some default settings
  30.         .InitialFileName = strStart
  31.         .AllowMultiSelect = False
  32.         .InitialView = msoFileDialogViewDetails
  33.         'Only return a value from the FileDialog if not cancelled.
  34.         If .Show Then fsbrowse = .SelectedItems(1)
  35.     End With
  36. End Function
Error message " external table is not in the expected format" The xls file also do not preview, as xlsx does when clicking on it to import.
Please advise its is driving me to ......
Aug 18 '14 #1
4 3198
twinnyfo
3,653 Expert Mod 2GB
.xls and .xlsx are different types of files, so the import engine is expecting certain things based on the type of file you designate.

If you know you are going to be importing a .xls file, try using the parameter of acSpreadsheetTypeExcel8. There are various ways you can have the VBA determine the file type.
Aug 18 '14 #2
NeoPa
32,556 Expert Mod 16PB
Neels,

At this stage I'm surprised you're posting a question with so much unnecessary information and distraction in it. You can easily determine if the value in strPath matches what you expect it to, and thus realise posting all the code related to FSBrowse() is irrelevant to your problem. If that string had been wrong after being returned from FSBrowse() that would be another matter. That is debugging first base. You should have done at least that much before posting the question.

As for the problem, look at line #11 of the (tidied version of the) first code section of your post.
acSpreadsheetTypeExcel9 is the second parameter. If you read the documentation you'll see that this parameter (named SpreadsheetType) specifies the file format of the file that's being dealt with (in this case read from). For files of type .XLS you will need to select a value that matches that type of file. I expect a good place to start would be acSpreadsheetTypeExcel8, but suck it and see.

While we're happy to help where we can, it's generally a good idea to do a bit of work first trying to resolve your problem. When, as in this case, it appears that you haven't done even the very basics for yourself before posting the question, you might find people less willing in future to spend their time trying to help you.
Aug 18 '14 #3
neelsfer
547 512MB
Hi Neopa. I have tried for 2 solid evenings using all sorts of different options and acspreadsheettypes, and researched the net. I use Excel 2007 and it only imports xlsx files with this code, although i can view xls or xlsx file names, when opening it with the "msoFileDialogFilePicker". The error message is then :"external table is not in the expected format".

I would prefer to only import xls as the default format, because i receive the Excel files like that. I now have to open and re-save in xlsx within Excel, in order to import. Its a pain. I have also set the reference to the Microsoft Excel 12.0 Object library with no success.

I will include 2 screen shots. In the problem xls one, the file will not preview while the other xlsx it does. This file in question does however open in Excel 2007 in compatibility mode.
NeoPa its not lack of effort, i am totally stuck! Please advise.

Attached Images
File Type: jpg excel issue.jpg (29.4 KB, 943 views)
File Type: jpg excel is fine.jpg (41.4 KB, 965 views)
Aug 20 '14 #4
NeoPa
32,556 Expert Mod 16PB
I hear you Neels. If you say you did try then I'm happy to believe you, but I'm a little surprised that after a long time working in Access you don't seem to be using some of the basic approaches we've been telling you about for a long time.

Debugging.
With a little basic debugging (See Debugging in VBA) you should be able to see very easily that the problem is purely and simply with the TransferSpreadsheet() line of code. I cannot emphasise too much how important debugging is in the life-cycle of a code project. Without it you can only ever be a small fraction as effective as a developer. The tools are there so use them. I'm sure you'll love it when you do.

For all the trying and trouble you're having - you haven't replied to the suggestion made, both by Twinnyfo & myself, that you try using acSpreadsheetTypeExcel8 as the second (SpreadsheetType) parameter. Have you tried that? If so, what did you see? Did anything change? If so, what?

You will find - I suspect you've already found - that however much time and effort you put into solving your problem, will be so much wasted time and effort until you stop running round in circles and start applying intelligent reasoning to the problem. I suspect the answer is there on the screen in front of you, but you've been so heavily into the problem as you understand it that you haven't even seen it.

I suspect we've all been in that same place. Worried so much about something that we haven't let our intelligence lead us through the easiest path. That is to say we expend so much effort everywhere in our confusion that we don't have any left for seeing the next logical step - even though that next step would lead us straight to the way out of the maze.

NB.
Remember, when dealing with people on a forum it always makes sense to respond to every suggestion. Other than common politeness, it also allows them to understand they can move forward or look elsewhere. Without any response it will generally be assumed that we are still waiting on you to test it.
Aug 22 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Adrian Manic | last post by:
H We have a strange problem which I can not get my head around The entry point of the application is a file called home.asp. First it includes some files that define constants, then is resets /...
0
by: Mike D | last post by:
Not really an asp error .. or is it? I have an asp page that queries a db and displays the data in an html table. This page has a hyperlink that goes to another asp page that is meant to load...
14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
4
by: anon | last post by:
Hello , I have a web application in which the pages have an "export to Excel" facility....while this works fine on the development machine, when the files are transferred to the server, the export...
2
by: no-spamJim | last post by:
Hi All, I'm having trouble showing aspx pages to the outside world. When I browse in my IIS6 Service Manager the page shows fine (also in a separate IE on this machine) but as soon as I look on...
3
by: Mike | last post by:
I am getting an error on a page that uses asp to query a db then opens the page on the client in excel. The error is only occuring on XP boxes only. If I comment out the cache killers it opens...
2
by: Rico | last post by:
Hello, I have a web application that I developed in ASP.NET on one machine and I'm trying to deploy it on a Windows 2003 Server. The application runs fine on the development workstation...
1
by: dasayu | last post by:
Hi, I have a custom object called gridWidget. I am consistantly getting an error in FireFox when I click on an href, which calls a function defined on the object. The generated link looks similar...
0
by: David | last post by:
Using ASP.Net 2.0 and I have identical code for exporting a GridView to Excel in 2 web sites. One works fine and the 2nd one gives me the error "RegisterForEventValidation can only be called...
13
by: Stever1975 | last post by:
I'm working on something similiar to a shopping cart item page. There is a table of items. Each item has an image, a textbox for the qty and an image for the add button. When the add image is...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.