{"id":65153,"date":"2007-04-04T20:49:00","date_gmt":"2007-04-04T20:49:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/04\/04\/how-can-i-import-an-excel-spreadsheet-into-an-access-database\/"},"modified":"2007-04-04T20:49:00","modified_gmt":"2007-04-04T20:49:00","slug":"how-can-i-import-an-excel-spreadsheet-into-an-access-database","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-import-an-excel-spreadsheet-into-an-access-database\/","title":{"rendered":"How Can I Import an Excel Spreadsheet Into an Access Database?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I import an Excel spreadsheet into an Access database?<BR><BR>&#8212; IE<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, IE. Wait! Don\u2019t tell us; let us guess. Just a moment \u2026 we\u2019re picking up the vibrations now \u2026 the sensation is getting stronger and more distinct \u2026 OK, got it: you\u2019d like to know how to import an Excel spreadsheet into an Access database, wouldn\u2019t you? Are we right? That\u2019s what we thought.<\/P>\n<P>Granted, some of you don\u2019t seem too impressed by that. \u201cBig deal,\u201d you say. \u201c<I>Anyone<\/I> could \u2018predict\u2019 what question IE was going to ask; all you have to do is read the question at the start of the column.\u201d Oh ye of little faith. Sure, it\u2019s true that we <I>could<\/I> have simply read IE\u2019s question ahead of time and then made a \u201cprediction.\u201d However, how do you explain <I>this<\/I>: in <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr07\/hey0403.mspx\"><B><I>yesterday\u2019s<\/I><\/B><B> column<\/B><\/A> we predicted that today we would cover importing an Excel spreadsheet into an Access database. It\u2019s true; just take a look at this quote from the April 3<SUP>rd<\/SUP> column:<\/P>\n<P>\u201cDoes that mean that we could use a similar script to <I>import<\/I> data? As a matter of fact it does, and in tomorrow\u2019s column we\u2019ll show you how to do that very thing.\u201d<\/P>\n<P>That\u2019s OK, no need to apologize; we\u2019re used to dealing with skeptics. And for those of you wondering <I>how<\/I> we were able to make such an uncanny prediction, well, to tell you the truth, we don\u2019t know; it\u2019s just a gift. But don\u2019t worry: the Scripting Guys vow to use this gift only for good, never for evil.<\/P>\n<P>Well, maybe a little evil. But not much.<\/P>\n<P>Of course, throughout the ages seers and psychics have been a dime-a-dozen; does the name Nostradamus ring a bell with anyone? But there\u2019s one thing that separates the Scripting Guys from their fellow prophets and soothsayers: as near as we can tell, Nostradamus never wrote a script that imports an Excel spreadsheet into an Access database. Score one for the Scripting Guys:<\/P><PRE class=\"codeSample\">Const acImport = 0\nConst acSpreadsheetTypeExcel9 = 8<\/p>\n<p>Set objAccess = CreateObject(&#8220;Access.Application&#8221;)\nobjAccess.OpenCurrentDatabase &#8220;C:\\Scripts\\Test.mdb&#8221;<\/p>\n<p>objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _\n    &#8220;Employees&#8221;, &#8220;C:\\Scripts\\Employees.xls&#8221;, True\n<\/PRE>\n<P>Guess what: we <I>knew<\/I> you were going to take one look at this and think \u201cWow; that\u2019s easy.\u201d And you\u2019re right: it is. As you can see, we start out by defining a constant name acImport and setting the value to 0; this tells the script that we want to import data (as opposed to exporting it). We then define a second constant, this one named acSpreadsheetTypeExcel9, and set the value to 8; this tells the script that we\u2019re planning to import an Excel 2003 worksheet. Are there other spreadsheet formats that we can import into Access? You bet there are:<\/P>\n<TABLE class=\"dataTable\" id=\"EDE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Constant<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel12<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">9<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel12Xml<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">10<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel3<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel4<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">6<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel5<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">5<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel7<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">5<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel8<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">8<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel9<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">8<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeLotusWJ2<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">4<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeLotusWK1<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">2<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeLotusWK3<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">3<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeLotusWK4<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">7<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After we define our constants we then use these two lines of code to create an instance of the <B>Access.Application<\/B> object and open the database C:\\Scripts\\Test.mdb (something we do by calling the <B>OpenCurrentDatabase<\/B> method):<\/P><PRE class=\"codeSample\">Set objAccess = CreateObject(&#8220;Access.Application&#8221;)\nobjAccess.OpenCurrentDatabase &#8220;C:\\Scripts\\Test.mdb&#8221;\n<\/PRE>\n<P>At that point all we have to do is invoke the <B>TransferSpreadsheet<\/B> method and import the spreadsheet data into the database:<\/P><PRE class=\"codeSample\">objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _\n    &#8220;Employees&#8221;, &#8220;C:\\Scripts\\Employees.xls&#8221;, True\n<\/PRE>\n<P>Ah, yes, we had a feeling you were going to ask that; here\u2019s what the parameters passed to the TransferSpreadsheet method represent:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>acImport<\/B>, the constant that tells the script that we want to import data rather than export it.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>acSpreadsheetTypeExcel9<\/B>, the constant that specifies the file format for the imported data.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>\u201cEmployees\u201d<\/B>, the name of the table in Test.mdb we want to add the spreadsheet data to. Suppose the database doesn\u2019t contain a table named Employees; what then? Well, in that case the script will automatically create a new table (named Employees, of course) for you.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>\u201cC:\\Scripts\\Employees.xls\u201d<\/B>, the full path to the Excel spreadsheet we want to import.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>True<\/B>, the HasFieldNames parameter, which tells the script that the first row in the spreadsheet file consists of field names. As you might expect, the import is more likely to work if the field names in your database match the field names in your spreadsheet. But what if they don\u2019t match? Well, in that case you might simply import the data to a new table. Alternatively, you can set the HasFieldNames parameter to False; when you do that the script will simply match the first column of data in the spreadsheet with the first field in the database table, then match the second column with the second field, etc. That works just fine, but you do need to make sure that the field order in the database is the same as the column order in the spreadsheet. If your database table has fields named FirstName, LastName, and Address, in that order, then your spreadsheet needs to have columns listing first names, last names, and addresses, in that same order.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>We should also add that it\u2019s important that the data types match as well. If you have a database field named EmployeeID, a field that only accepts numeric data, don\u2019t try importing spreadsheet data that includes non-numeric data (e.g., an employee ID of 1234ABC). That\u2019s not going to work.<\/P>\n<P>Oh, and here\u2019s another thing we should add: there\u2019s actually an optional sixth parameter (following HasFieldNames) that enables you to limit your import to a specified range of data. For example, suppose your spreadsheet has 6 columns of data (columns A through F) and you only want to import the first 25 rows in that spreadsheet. Okey-doke. That means that the range of data to be imported runs from cells A1 through cell F25 (<B>A1:F25<\/B> in Excel-speak). In turn, that means you can import just those 25 records by using this command:<\/P><PRE class=\"codeSample\">objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _\n    &#8220;Employees&#8221;, &#8220;C:\\Scripts\\Employees.xls&#8221;, True, &#8220;A1:F25&#8221;\n<\/PRE>\n<P>We\u2019ve already thought of that: suppose you <I>would<\/I> like to import the spreadsheet into a brand-new database (as opposed to importing the data into an existing database). Is that possible? Not if you\u2019re Nostradamus. But if you\u2019re the Scripting Guys, well, then it\u2019s a different story:<\/P><PRE class=\"codeSample\">Const acImport = 0\nConst acSpreadsheetTypeExcel9 = 8<\/p>\n<p>Set objAccess = CreateObject(&#8220;Access.Application&#8221;)\nobjAccess.NewCurrentDatabase &#8220;C:\\Scripts\\New.mdb&#8221;<\/p>\n<p>objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _\n    &#8220;Employees&#8221;, &#8220;C:\\Scripts\\Employees.xls&#8221;, True\n<\/PRE>\n<P>You\u2019re right: this <I>does<\/I> look a lot like the first script we showed you, doesn\u2019t it? In fact, there\u2019s only one difference: this time we don\u2019t use the OpenCurrentDatabase method in order to open an existing database file. Instead, we use the <B>NewCurrentDatabase<\/B> method to create a brand-new database (C:\\Scripts\\New.mdb):<\/P><PRE class=\"codeSample\">objAccess.NewCurrentDatabase &#8220;C:\\Scripts\\New.mdb&#8221;\n<\/PRE>\n<P>At this point in time it\u2019s hard to believe that anyone could possibly doubt the Scripting Guys and their power of clairvoyance. Just in case, however, we might note that the Scripting Guy who writes this column won the annual <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/mar07\/hey0319.mspx\"><B>NCAA college basketball challenge<\/B><\/A> by, among other things, correctly foreseeing a Florida victory over Ohio State. (Sorry; over <I>the<\/I> Ohio State University. Sheesh.) Furthermore, he already has a prediction for next year\u2019s championship game: the University of Washington 78, North Carolina 65. Remember, you read it here first.<\/P>\n<P>Unless it turns out that we\u2019re wrong. In that case, you didn\u2019t read it here at all, and we have no idea what you\u2019re talking about.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I import an Excel spreadsheet into an Access database?&#8212; IE Hey, IE. Wait! Don\u2019t tell us; let us guess. Just a moment \u2026 we\u2019re picking up the vibrations now \u2026 the sensation is getting stronger and more distinct \u2026 OK, got it: you\u2019d like to know how to import an [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[710,54,48,49,3,5],"class_list":["post-65153","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-excel-spreadsheet","tag-microsoft-access","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I import an Excel spreadsheet into an Access database?&#8212; IE Hey, IE. Wait! Don\u2019t tell us; let us guess. Just a moment \u2026 we\u2019re picking up the vibrations now \u2026 the sensation is getting stronger and more distinct \u2026 OK, got it: you\u2019d like to know how to import an [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=65153"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65153\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=65153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}