Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Importing Data from Excel Spreadsheet/Text files

Status
Not open for further replies.

DanAuber

IS-IT--Management
Apr 28, 2000
255
FR
I'm using macros to import Excel and Text files into Access, using pre-saved parameters. Unfortunatley I'm finding it difficult to train the people loading the data to give the files the names I am expecting.<br>Is there an easy way to make the import process prompt for file names - at which point users would have to type in the full path and file name of the file they wished to load. Thereafter the load would use the predefined parameters.<br><br>Any help gratefully received <p>Dan Auber<br><a href=mailto:DanAuber@aol.com>DanAuber@aol.com</a><br><a href= Auber's Home Page</a><br>
 
Dan, I have the same issues myself.&nbsp;&nbsp;I require people to report certain information to me in a certain format (which i'm lucky to have them get right), and i like to have a certain name to the file for the same reason.<br>However, even through the use of VB i've not been able to find a way for the user to get prompted for the pathname. but i am a self taught VB hacker, and i'm sure that someone out there has a way to do it.&nbsp;&nbsp;It could be much like calling up the import wizard window to prompt for file location.<br>If you ever figure it out, i'd love to know how you got it to work.<br><br>Best of luck. <p>Brian Famous<br><a href=mailto:bfamous@ncdoi.net>bfamous@ncdoi.net</a><br><a href= > </a><br>
 
I'm sorry, i just figured it out.&nbsp;&nbsp;check this out:<br><b><br>Dim Message, Default, MyValue As String<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim Title<br>&nbsp;&nbsp;&nbsp;&nbsp;Message = &quot;Enter the Excel File name including drive:path&quot; 'Set Prompt<br>&nbsp;&nbsp;&nbsp;&nbsp;Default = &quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;MyValue = InputBox(Message, Title, Default)<br>&nbsp;&nbsp;&nbsp;&nbsp;Docmd.TransferSpreadsheet acImport,acSpreadsheetTypeExcel97,&quot;YourTableName&quot;,MyValue<br></b><br><br>put this in the OnClick property of the button (or whateevr you use) to start the macro (from the build function choose Code Builder).&nbsp;&nbsp;change &quot;YourTableName&quot; to the name of the table you want the data to go into.&nbsp;&nbsp;make sure you keep the quotes.<br><br>that is for an Excel spreadsheet, for a text file replace the last line with:<br><b><br>DoCmd.TransferText acImportFixed, &quot;YourSpecName&quot;, &quot;YourTableName&quot;, MyValue<br></b><br><br>this should work for you. <p>Brian Famous<br><a href=mailto:bfamous@ncdoi.net>bfamous@ncdoi.net</a><br><a href= > </a><br>
 
Thanks for that Brian<br>How does the VB know to use the correct import specification (in text import in particular - the spec holds data separator and field formats)<br><br>Dan <p>Dan Auber<br><a href=mailto:DanAuber@aol.com>DanAuber@aol.com</a><br><a href= Auber's Home Page</a><br>
 
No problem Dan, actually here is something else really cool you might want to try if your users generally have a set number of files they need to import (say three):<br><b><br>&nbsp;&nbsp;&nbsp;&nbsp;Dim Message, Default As String<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim MyValue1, MyValue2, MyValue3 As String<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim Title<br>&nbsp;&nbsp;&nbsp;&nbsp;Message = &quot;Enter the file name and path.&quot; 'Set Prompt<br>&nbsp;&nbsp;&nbsp;&nbsp;Default = &quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;MyValue1 = InputBox(Message, Title, Default) <br>&nbsp;&nbsp;&nbsp;&nbsp;MyValue2 = InputBox(Message, Title, Default)<br>&nbsp;&nbsp;&nbsp;&nbsp;MyValue3 = InputBox(Message, Title, Default)<br>&nbsp;&nbsp;&nbsp;&nbsp;DoCmd.TransferText acImportFixed, &quot;YourSpecName&quot;, &quot;YourTableName&quot;, MyValue1<br>&nbsp;&nbsp;&nbsp;&nbsp;DoCmd.TransferText acImportFixed, &quot;YourSpecName&quot;, &quot;YourTableName&quot;, MyValue2<br>&nbsp;&nbsp;&nbsp;&nbsp;DoCmd.TransferText acImportFixed, &quot;YourSpecName&quot;, &quot;YourTableName&quot;, MyValue3<br></b><br><br>tihs way they can put them all in, and then walk away while they are waiting. <p>Brian Famous<br><a href=mailto:bfamous@ncdoi.net>bfamous@ncdoi.net</a><br><a href= > </a><br>
 
Hi Dan,<br><br>My name is Sherree I am almost having the same problem you're having, I wrote<br>the sample code Brian showed you.&nbsp;&nbsp;It does not work you have to specify the file<br>name and enclose it with quotes.&nbsp;&nbsp;I am trying&nbsp;&nbsp;to import multiple excel worksheets<br>into an Access table.<br><br>This code works but can only import one&nbsp;&nbsp;file at a time.<br><br>DoCmd.TransferSpreadsheet acImport, 8, &quot;Employees&quot;, &quot;A:\Excel\EncounterForms.xls&quot;, True<br>DoCmd.Close<br><br>8 is the code for Excel97 you can write this instead of 8<br>acSpreadsheetTypeExcel 97<br>True&nbsp;&nbsp;allows you to use the first row of the spreadsheet as field names<br>You can also specify the range of the excel spreadsheet by typing &quot;A1:F20&quot;<br>after True if you leave it blank as I did it will read all files.<br>&quot;Employees&quot; is the name of the table in access <br><br>I hope this was useful, If you have any suggestions for me, I greatly appreciate it.
 
When exporting an Excell spreadsheet from VB I found that if you omit the file name in the call Access gives the user a dialog box to select the folder and override the default output file name taken from the table or query.&nbsp;&nbsp;You might see if the import works as well.
 
Thanks harryrich,<br><br>I 've tried your advice, but it does not work.&nbsp;&nbsp;I get this error message:<br><br>The action or method requires a File Name argument.<br><br>You tried to use the TransferSpreadsheet or TransferText action or method.<br>In the File Name argument, enter a file name.<br>
 
sheree, are you saying that you can still not get the import text to work for you?<br>This code does work<br><b><br>Dim Message, Default, MyValue As String<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim Title<br>&nbsp;&nbsp;&nbsp;&nbsp;Message = &quot;Enter the Excel File name including drive:path&quot; 'Set Prompt<br>&nbsp;&nbsp;&nbsp;&nbsp;Default = &quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;MyValue = InputBox(Message, Title, Default)<br>&nbsp;&nbsp;&nbsp;&nbsp;Docmd.TransferSpreadsheet acImport,acSpreadsheetTypeExcel97,&quot;YourTableName&quot;,MyValue<br></b><br><br>you just need to replace &quot;YourTableName&quot; with the name of the table that you want to import the data into.<br>I've used it several times, and i've also used the TransferText method several times.<br>Actually, i just finished figuring out some code for it to link the text file, then run an append query&nbsp;&nbsp;(actually done through SQL - it was my first true SQL trial) to append a random sample of data to an existing Access table.<br><br>If you have trouble getting the above statement to work let me know, i'd be glad to help (the prompt for file name of it was yours to begin with anyway) <p>Brian Famous<br><a href=mailto:bfamous@ncdoi.net>bfamous@ncdoi.net</a><br><a href= > </a><br>
 
Brian,<br><br>I've tried you're advice once more.&nbsp;&nbsp;And guess what it works!<br>Thank you very much :)<br><br>Now I have to try an excel file that has multiple spreadsheets to import.<br><br>
 
i'm not sure how to designate which spreadsheet from an Excel file to import, but i'm willing to bet it's where you put the range.<br>ie. &quot;Sheet1:A1:F12&quot;<br>but don't quote me on that part.<br><br>i'd be interested to know if you figure it out. <p>Brian Famous<br><a href=mailto:bfamous@ncdoi.net>bfamous@ncdoi.net</a><br><a href= > </a><br>
 
I am trying to import excel spreadsheets into an existing access table , but keep getting the error:

&quot;an error has occured trying to import the file d:\ .... .xls. The file was not imported&quot;

1. The access table is not keyed,
2. The access table fields are of length 255 and text
3. The excel field are all text, and are of length 5 , 10, 10.

 
Hi,

Any idea how to do the same thing from VB code. The Docmd function is not available in VB.Its working from access but i wanna do it from my VB application.

thanks..
Pr
 
I know this has been asked to death, but I know nothing about VB and am trying to get these to work.

Basically I need to import a spreadsheet everyday into access. The spreadsheet has a diff name everday (for the date) and every month it is put into a new folder on the network. I would like to be able to choose the file to be imported and cannot get the suggestions made on the forum to work.

Does anyone have something new to report on this? We are using Access 2002. Typing the entire path everyday would also be a hassle because it is embedded about 8-10 folders deep.

Thank you!!!
Dianna
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top