469,271 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Import to access from excel

rcollins
234 100+
What I have is a group of Employment Consultants that help our clients with jobs. They have to document all of the contact inf. For this, we will be using palm pilots, with an excel spreadsheet that I had set up for them. What we need to do, is import all of the info from the spreadsheet to the database. There is a page for every day of the month in every one of the speadsheets, and there are a total of 15 workbooks. I need to be able to import these all at once. Please help
Apr 2 '07 #1
22 6611
cgrider
19
What I have is a group of Employment Consultants that help our clients with jobs. They have to document all of the contact inf. For this, we will be using palm pilots, with an excel spreadsheet that I had set up for them. What we need to do, is import all of the info from the spreadsheet to the database. There is a page for every day of the month in every one of the speadsheets, and there are a total of 15 workbooks. I need to be able to import these all at once. Please help

what version of access and version of excel are you doing this in? also you have 15 files and in those files you have upto 31 sheets in them? and thirdly you want to do this as a one time thing or on a continoius basis? if a one time you can via file->get external date->import and follow the wizzard. if you want to do this on an on going basis then you will have to do some macros or some vba coding. if the later i can show you the code I wrote on this to see if this will help you or not.

thanks
CG
Apr 2 '07 #2
rcollins
234 100+
what version of access and version of excel are you doing this in? also you have 15 files and in those files you have upto 31 sheets in them? and thirdly you want to do this as a one time thing or on a continoius basis? if a one time you can via file->get external date->import and follow the wizzard. if you want to do this on an on going basis then you will have to do some macros or some vba coding. if the later i can show you the code I wrote on this to see if this will help you or not.

thanks
CG
I am using office 2003
If I was the one importing, this would be fine to import external data. What the intention here is to (hopefully) have a button on the main form that the person using the database would click on after all 15 workbooks are turned in and the info get put in the appropriate table. The table the info is very basic, and all of the workbooks put the info in the same place. Coding is the way to go on this one, the workbooks are turned in on a monthly basis. Your help is greatly appreciated.
Apr 4 '07 #3
rcollins
234 100+
So after more research, I feel I may be getting closer. Since I have the 15 files that I want to import at the same time, I modified the code, which opens a window where you can pick the multiple files. This should be ok, (even though I would rather have it automatically pick all of the files in that folder) but the macro errors with this code. In the macro I have it set to call the function Get_FileName(), which is my module2. Here is the code in my module. I am confused on how to make the transferspreadsheet work. As this is now, it says I need a table name argument. What am I missing? Please help
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Type tagOPENFILENAME
  4.     lStructSize As Long
  5.     hwndOwner As Long
  6.     hInstance As Long
  7.     strFilter As String
  8.     strCustomFilter As String
  9.     nMaxCustFilter As Long
  10.     nFilterIndex As Long
  11.     strFile As String
  12.     nMaxFile As Long
  13.     strFileTitle As String
  14.     nMaxFileTitle As Long
  15.     strInitialDir As String
  16.     strTitle As String
  17.     Flags As Long
  18.     nFileOffset As Integer
  19.     nFileExtension As Integer
  20.     strDefExt As String
  21.     lCustData As Long
  22.     lpfnHook As Long
  23.     lpTemplateName As String
  24. End Type
  25.  
  26. Declare Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
  27.     Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
  28.  
  29. Declare Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
  30.     Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
  31. Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
  32.  
  33. Global Const ahtOFN_READONLY = &H1
  34. Global Const ahtOFN_OVERWRITEPROMPT = &H2
  35. Global Const ahtOFN_HIDEREADONLY = &H4
  36. Global Const ahtOFN_NOCHANGEDIR = &H8
  37. Global Const ahtOFN_SHOWHELP = &H10
  38. Global Const ahtOFN_NOVALIDATE = &H100
  39. Global Const ahtOFN_ALLOWMULTISELECT = &H200
  40. Global Const ahtOFN_EXTENSIONDIFFERENT = &H400
  41. Global Const ahtOFN_PATHMUSTEXIST = &H800
  42. Global Const ahtOFN_FILEMUSTEXIST = &H1000
  43. Global Const ahtOFN_CREATEPROMPT = &H2000
  44. Global Const ahtOFN_SHAREAWARE = &H4000
  45. Global Const ahtOFN_NOREADONLYRETURN = &H8000
  46. Global Const ahtOFN_NOTESTFILECREATE = &H10000
  47. Global Const ahtOFN_NONETWORKBUTTON = &H20000
  48. Global Const ahtOFN_NOLONGNAMES = &H40000
  49. ' New for Windows 95
  50. Global Const ahtOFN_EXPLORER = &H80000
  51. Global Const ahtOFN_NODEREFERENCELINKS = &H100000
  52. Global Const ahtOFN_LONGNAMES = &H200000
  53. Function Get_FileName()
  54.     Dim strFilter As String
  55.     Dim strInputFileName As String
  56.  
  57. strFilter = ahtAddFilterItem(strFilter, "Excel File  (*.xls)", "*.XLS")
  58. strInputFileName = ahtCommonFileOpenSave( _
  59. Filter:=strFilter, OpenFile:=True, _
  60. DialogTitle:="Please select an input file...", _
  61. Flags:=ahtOFN_HIDEREADONLY Or ahtOFN_ALLOWMULTISELECT)
  62.  
  63.     If Len(strInputFileName) > 0 Then
  64.         'insert what to do with file here
  65.         DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, tblClientMain, , no
  66.     Else
  67.         'No file chosen, or user canceled
  68.     End If
  69.  
  70. End Function
  71.  
  72. Function GetOpenFile(Optional varDirectory As Variant, _
  73.     Optional varTitleForDialog As Variant) As Variant
  74. ' Here's an example that gets an Access database name.
  75. Dim strFilter As String
  76. Dim lngFlags As Long
  77. Dim varFileName As Variant
  78. ' Specify that the chosen file must already exist,
  79. ' don't change directories when you're done
  80. ' Also, don't bother displaying
  81. ' the read-only box. It'll only confuse people.
  82.     lngFlags = ahtOFN_FILEMUSTEXIST Or _
  83.                 ahtOFN_HIDEREADONLY Or ahtOFN_NOCHANGEDIR
  84.     If IsMissing(varDirectory) Then
  85.         varDirectory = ""
  86.     End If
  87.     If IsMissing(varTitleForDialog) Then
  88.         varTitleForDialog = ""
  89.     End If
  90.  
  91.     ' Define the filter string and allocate space in the "c"
  92.     ' string Duplicate this line with changes as necessary for
  93.     ' more file templates.
  94.     strFilter = ahtAddFilterItem(strFilter, _
  95.                 "Access (*.mdb)", "*.MDB;*.MDA")
  96.     ' Now actually call to get the file name.
  97.     varFileName = ahtCommonFileOpenSave( _
  98.                     OpenFile:=True, _
  99.                     InitialDir:=varDirectory, _
  100.                     Filter:=strFilter, _
  101.                     Flags:=lngFlags, _
  102.                     DialogTitle:=varTitleForDialog)
  103.     If Not IsNull(varFileName) Then
  104.         varFileName = TrimNull(varFileName)
  105.     End If
  106.     GetOpenFile = varFileName
  107. End Function
  108.  
  109. Function ahtCommonFileOpenSave( _
  110.             Optional ByRef Flags As Variant, _
  111.             Optional ByVal InitialDir As Variant, _
  112.             Optional ByVal Filter As Variant, _
  113.             Optional ByVal FilterIndex As Variant, _
  114.             Optional ByVal DefaultExt As Variant, _
  115.             Optional ByVal Filename As Variant, _
  116.             Optional ByVal DialogTitle As Variant, _
  117.             Optional ByVal hwnd As Variant, _
  118.             Optional ByVal OpenFile As Variant) As Variant
  119. ' This is the entry point you'll use to call the common
  120. ' file open/save dialog. The parameters are listed
  121. ' below, and all are optional.
  122. '
  123. ' In:
  124. ' Flags: one or more of the ahtOFN_* constants, OR'd together.
  125. ' InitialDir: the directory in which to first look
  126. ' Filter: a set of file filters, set up by calling
  127. ' AddFilterItem. See examples.
  128. ' FilterIndex: 1-based integer indicating which filter
  129. ' set to use, by default (1 if unspecified)
  130. ' DefaultExt: Extension to use if the user doesn't enter one.
  131. ' Only useful on file saves.
  132. ' FileName: Default value for the file name text box.
  133. ' DialogTitle: Title for the dialog.
  134. ' hWnd: parent window handle
  135. ' OpenFile: Boolean(True=Open File/False=Save As)
  136. ' Out:
  137. ' Return Value: Either Null or the selected filename
  138. Dim OFN As tagOPENFILENAME
  139. Dim strFileName As String
  140. Dim strFileTitle As String
  141. Dim fResult As Boolean
  142.     ' Give the dialog a caption title.
  143.     If IsMissing(InitialDir) Then InitialDir = CurDir
  144.     If IsMissing(Filter) Then Filter = ""
  145.     If IsMissing(FilterIndex) Then FilterIndex = 1
  146.     If IsMissing(Flags) Then Flags = 0&
  147.     If IsMissing(DefaultExt) Then DefaultExt = ""
  148.     If IsMissing(Filename) Then Filename = ""
  149.     If IsMissing(DialogTitle) Then DialogTitle = ""
  150.     If IsMissing(hwnd) Then hwnd = Application.hWndAccessApp
  151.     If IsMissing(OpenFile) Then OpenFile = True
  152.     ' Allocate string space for the returned strings.
  153.     strFileName = Left(Filename & String(256, 0), 256)
  154.     strFileTitle = String(256, 0)
  155.     ' Set up the data structure before you call the function
  156.     With OFN
  157.         .lStructSize = Len(OFN)
  158.         .hwndOwner = hwnd
  159.         .strFilter = Filter
  160.         .nFilterIndex = FilterIndex
  161.         .strFile = strFileName
  162.         .nMaxFile = Len(strFileName)
  163.         .strFileTitle = strFileTitle
  164.         .nMaxFileTitle = Len(strFileTitle)
  165.         .strTitle = DialogTitle
  166.         .Flags = Flags
  167.         .strDefExt = DefaultExt
  168.         .strInitialDir = InitialDir
  169.         ' Didn't think most people would want to deal with
  170.         ' these options.
  171.         .hInstance = 0
  172.         '.strCustomFilter = ""
  173.         '.nMaxCustFilter = 0
  174.         .lpfnHook = 0
  175.         'New for NT 4.0
  176.         .strCustomFilter = String(255, 0)
  177.         .nMaxCustFilter = 255
  178.     End With
  179.     ' This will pass the desired data structure to the
  180.     ' Windows API, which will in turn it uses to display
  181.     ' the Open/Save As Dialog.
  182.     If OpenFile Then
  183.         fResult = aht_apiGetOpenFileName(OFN)
  184.     Else
  185.         fResult = aht_apiGetSaveFileName(OFN)
  186.     End If
  187.  
  188.     ' The function call filled in the strFileTitle member
  189.     ' of the structure. You'll have to write special code
  190.     ' to retrieve that if you're interested.
  191.     If fResult Then
  192.         ' You might care to check the Flags member of the
  193.         ' structure to get information about the chosen file.
  194.         ' In this example, if you bothered to pass in a
  195.         ' value for Flags, we'll fill it in with the outgoing
  196.         ' Flags value.
  197.         If Not IsMissing(Flags) Then Flags = OFN.Flags
  198.         ahtCommonFileOpenSave = TrimNull(OFN.strFile)
  199.     Else
  200.         ahtCommonFileOpenSave = vbNullString
  201.     End If
  202. End Function
  203.  
  204. Function ahtAddFilterItem(strFilter As String, _
  205.     strDescription As String, Optional varItem As Variant) As String
  206. ' Tack a new chunk onto the file filter.
  207. ' That is, take the old value, stick onto it the description,
  208. ' (like "Databases"), a null character, the skeleton
  209. ' (like "*.mdb;*.mda") and a final null character.
  210.  
  211.     If IsMissing(varItem) Then varItem = "*.*"
  212.     ahtAddFilterItem = strFilter & _
  213.                 strDescription & vbNullChar & _
  214.                 varItem & vbNullChar
  215. End Function
  216.  
  217. Private Function TrimNull(ByVal strItem As String) As String
  218. Dim intPos As Integer
  219.     intPos = InStr(strItem, vbNullChar)
  220.     If intPos > 0 Then
  221.         TrimNull = Left(strItem, intPos - 1)
  222.     Else
  223.         TrimNull = strItem
  224.     End If
  225. End Function
  226.  
Apr 6 '07 #4
MMcCarthy
14,534 Expert Mod 8TB
Have a look at this instead...
Expand|Select|Wrap|Line Numbers
  1. Function getData()
  2. Dim strFile As String 'Filename
  3. Dim strFileList() As String 'File Array
  4. Dim intFile As Integer 'File Number
  5. Dim filename As String
  6. Dim path As String
  7.  
  8.     DoCmd.SetWarnings False
  9.  
  10.     path = "Path to folder containing excel files"
  11.  
  12.     'Loop through the folder & build file list
  13.     strFile = Dir(path & "*.xls")
  14.     While strFile <> ""
  15.          'add files to the list
  16.         intFile = intFile + 1
  17.         ReDim Preserve strFileList(1 To intFile)
  18.         strFileList(intFile) = strFile
  19.         strFile = Dir()
  20.     Wend
  21.      'see if any files were found
  22.     If intFile = 0 Then
  23.         MsgBox "No files found"
  24.         Exit Sub
  25.     End If
  26.  
  27.     'cycle through the list of files
  28.     For intFile = 1 To UBound(strFileList)
  29.  
  30.         filename = path1 & strFileList(intFile)
  31.         DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel3, "TableName", filename, True
  32.  
  33.     Next intFile
  34.  
  35.    DoCmd.SetWarnings True
  36.  
  37. End Function
  38.  
Mary
Apr 6 '07 #5
MMcCarthy
14,534 Expert Mod 8TB
Try running this and see what happens ...

Expand|Select|Wrap|Line Numbers
  1. Function getData()
  2. Dim strFile As String 'Filename
  3. Dim strFileList() As String 'File Array
  4. Dim intFile As Integer 'File Number
  5. Dim filename As String
  6. Dim path As String
  7.  
  8.     DoCmd.SetWarnings False
  9.  
  10.     path = "\\Dc2850\Daily Billables\Finished\"
  11.  
  12.     'Loop through the folder & build file list
  13.     strFile = Dir(path & "*.xls")
  14.     While strFile <> ""
  15.          'add files to the list
  16.         intFile = intFile + 1
  17.         ReDim Preserve strFileList(1 To intFile)
  18.         strFileList(intFile) = strFile
  19.         strFile = Dir()
  20.     Wend
  21.      'see if any files were found
  22.     If intFile = 0 Then
  23.         MsgBox "No files found"
  24.         Exit Sub
  25.     End If
  26.  
  27.     'cycle through the list of files
  28.     For intFile = 1 To UBound(strFileList)
  29.  
  30.         filename = path & strFileList(intFile)
  31.         DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel3, "tblClientMail", filename, True
  32.  
  33.     Next intFile
  34.  
  35.    DoCmd.SetWarnings True
  36.  
  37. End Function
  38.  
Mary
Apr 6 '07 #6
rcollins
234 100+
Very nice...did you know you have an exit sub after MsgBox "No files found"? I had to take that out. worked good. Last issue on the import is that if the field Duration is null or 0 not to import that record. Is this possible?
Apr 6 '07 #7
MMcCarthy
14,534 Expert Mod 8TB
Very nice...did you know you have an exit sub after MsgBox "No files found"? I had to take that out. worked good. Last issue on the import is that if the field Duration is null or 0 not to import that record. Is this possible?
Sorry copying and pasting again. Replace the Exit Sub with Exit Function.

You can't check for null records using the TransferSpreadsheet method and it's not worth a more complicated method. Just run a delete query to delete empty records after the import.
Apr 6 '07 #8
rcollins
234 100+
I will add something to the button that will run a delete query. Thank you for your help on this one mary
Apr 12 '07 #9
MMcCarthy
14,534 Expert Mod 8TB
I will add something to the button that will run a delete query. Thank you for your help on this one mary
You're welcome.
Apr 12 '07 #10
Dear mmccarthy,

I also want to know if I need to import a specify worksheet, how to modify the module?

Software: access and excel 2003

Excel file: format is the same and need to import "data" worksheet, there is three worksheet in each excel file
Apr 19 '07 #11
rcollins
234 100+
I was wondering the same about multiple sheets in a workbook. I have 31 pages in each of my 15 workbooks, as this code only pulls from the fromt page. I will have to modify the front page to get the info off of all of the rest.
Apr 19 '07 #12
MMcCarthy
14,534 Expert Mod 8TB
To import Sheet named "Sheet1"

Expand|Select|Wrap|Line Numbers
  1.         DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel3, "tblClientMail", filename, True, "Sheet1$"
  2.  
Apr 19 '07 #13
rcollins
234 100+
What would be the proper way to loop through the 31 pages?
Apr 19 '07 #14
rcollins
234 100+
OK, got it doing all 31 pages. What about a spacific range, A1-P21
Apr 19 '07 #15
MMcCarthy
14,534 Expert Mod 8TB
OK, got it doing all 31 pages. What about a spacific range, A1-P21
"Sheet1!A1:P21"
Apr 19 '07 #16
rcollins
234 100+
why would I all the sudden be getting run time error 3349 numeric field overflow?
Apr 19 '07 #17
MMcCarthy
14,534 Expert Mod 8TB
why would I all the sudden be getting run time error 3349 numeric field overflow?
It sounds like one of the pieces of data you are trying to import is too large for the field you are trying to append it to.
Apr 19 '07 #18
rcollins
234 100+
Most of my fields are pretty small, definately small enough for text, I have one that is set as memo, because it is a comment field. What is funny, is as I make a few changes to the speadsheet, it may work once or twice, then not again. No changes are being made between the time it works and doesn't work. Help please
Apr 19 '07 #19
Thanks mmccarthy,

If you dont mind I would like to ask one more question.
As my data sheet named may be changed to data_xxxxx.x randomly and is it possible to import such data?
Thanks again
Apr 20 '07 #20
Denburt
1,356 Expert 1GB
Most of my fields are pretty small, definately small enough for text, I have one that is set as memo, because it is a comment field. What is funny, is as I make a few changes to the speadsheet, it may work once or twice, then not again. No changes are being made between the time it works and doesn't work. Help please
There are many methods of achieving your goals although I may not have a diret answer this may help I always like a direct method. Bouncing off this idea you can call ranges manipulate rows,columns etc. have fun...Hope it helps...

http://support.microsoft.com/kb/198571
Apr 21 '07 #21
rcollins
234 100+
I was using the range that mmcarthy gave me for inputting the different sheets with selected ranges. After numerous tries, getting numeric overflow error, not getting it to import, I limited the range of data. I had found that there was something wrong with column M, so when I modified the formulas that I was using, it started to work. So far all looks good.
Apr 23 '07 #22
rcollins
234 100+
So, if this isn't interesting enough...the numeric overflow error that I was getting has a reason. On the last page of the database, I have a named range client id that gets the id numbers and names from the client table. When you open the file, it says, "enable automatic refresh" and "Disable automatic refresh". The database does not understand this, and therefor crashes on import. Here is a site with the info if anyone else might need it to turn off the prompt.
http://support.microsoft.com/kb/248204
I hope this keeps someone from having the same issue in the future. Thanks for all of the help on the Import function of my database. Now, on to reports.
Apr 24 '07 #23

Post your reply

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

Similar topics

1 post views Thread by John Hewitt | last post: by
15 posts views Thread by daniroy | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.