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
22 6764
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
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.
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 -
Option Compare Database
-
-
Type tagOPENFILENAME
-
lStructSize As Long
-
hwndOwner As Long
-
hInstance As Long
-
strFilter As String
-
strCustomFilter As String
-
nMaxCustFilter As Long
-
nFilterIndex As Long
-
strFile As String
-
nMaxFile As Long
-
strFileTitle As String
-
nMaxFileTitle As Long
-
strInitialDir As String
-
strTitle As String
-
Flags As Long
-
nFileOffset As Integer
-
nFileExtension As Integer
-
strDefExt As String
-
lCustData As Long
-
lpfnHook As Long
-
lpTemplateName As String
-
End Type
-
-
Declare Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
-
Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
-
-
Declare Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
-
Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
-
Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
-
-
Global Const ahtOFN_READONLY = &H1
-
Global Const ahtOFN_OVERWRITEPROMPT = &H2
-
Global Const ahtOFN_HIDEREADONLY = &H4
-
Global Const ahtOFN_NOCHANGEDIR = &H8
-
Global Const ahtOFN_SHOWHELP = &H10
-
Global Const ahtOFN_NOVALIDATE = &H100
-
Global Const ahtOFN_ALLOWMULTISELECT = &H200
-
Global Const ahtOFN_EXTENSIONDIFFERENT = &H400
-
Global Const ahtOFN_PATHMUSTEXIST = &H800
-
Global Const ahtOFN_FILEMUSTEXIST = &H1000
-
Global Const ahtOFN_CREATEPROMPT = &H2000
-
Global Const ahtOFN_SHAREAWARE = &H4000
-
Global Const ahtOFN_NOREADONLYRETURN = &H8000
-
Global Const ahtOFN_NOTESTFILECREATE = &H10000
-
Global Const ahtOFN_NONETWORKBUTTON = &H20000
-
Global Const ahtOFN_NOLONGNAMES = &H40000
-
' New for Windows 95
-
Global Const ahtOFN_EXPLORER = &H80000
-
Global Const ahtOFN_NODEREFERENCELINKS = &H100000
-
Global Const ahtOFN_LONGNAMES = &H200000
-
Function Get_FileName()
-
Dim strFilter As String
-
Dim strInputFileName As String
-
-
strFilter = ahtAddFilterItem(strFilter, "Excel File (*.xls)", "*.XLS")
-
strInputFileName = ahtCommonFileOpenSave( _
-
Filter:=strFilter, OpenFile:=True, _
-
DialogTitle:="Please select an input file...", _
-
Flags:=ahtOFN_HIDEREADONLY Or ahtOFN_ALLOWMULTISELECT)
-
-
If Len(strInputFileName) > 0 Then
-
'insert what to do with file here
-
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, tblClientMain, , no
-
Else
-
'No file chosen, or user canceled
-
End If
-
-
End Function
-
-
Function GetOpenFile(Optional varDirectory As Variant, _
-
Optional varTitleForDialog As Variant) As Variant
-
' Here's an example that gets an Access database name.
-
Dim strFilter As String
-
Dim lngFlags As Long
-
Dim varFileName As Variant
-
' Specify that the chosen file must already exist,
-
' don't change directories when you're done
-
' Also, don't bother displaying
-
' the read-only box. It'll only confuse people.
-
lngFlags = ahtOFN_FILEMUSTEXIST Or _
-
ahtOFN_HIDEREADONLY Or ahtOFN_NOCHANGEDIR
-
If IsMissing(varDirectory) Then
-
varDirectory = ""
-
End If
-
If IsMissing(varTitleForDialog) Then
-
varTitleForDialog = ""
-
End If
-
-
' Define the filter string and allocate space in the "c"
-
' string Duplicate this line with changes as necessary for
-
' more file templates.
-
strFilter = ahtAddFilterItem(strFilter, _
-
"Access (*.mdb)", "*.MDB;*.MDA")
-
' Now actually call to get the file name.
-
varFileName = ahtCommonFileOpenSave( _
-
OpenFile:=True, _
-
InitialDir:=varDirectory, _
-
Filter:=strFilter, _
-
Flags:=lngFlags, _
-
DialogTitle:=varTitleForDialog)
-
If Not IsNull(varFileName) Then
-
varFileName = TrimNull(varFileName)
-
End If
-
GetOpenFile = varFileName
-
End Function
-
-
Function ahtCommonFileOpenSave( _
-
Optional ByRef Flags As Variant, _
-
Optional ByVal InitialDir As Variant, _
-
Optional ByVal Filter As Variant, _
-
Optional ByVal FilterIndex As Variant, _
-
Optional ByVal DefaultExt As Variant, _
-
Optional ByVal Filename As Variant, _
-
Optional ByVal DialogTitle As Variant, _
-
Optional ByVal hwnd As Variant, _
-
Optional ByVal OpenFile As Variant) As Variant
-
' This is the entry point you'll use to call the common
-
' file open/save dialog. The parameters are listed
-
' below, and all are optional.
-
'
-
' In:
-
' Flags: one or more of the ahtOFN_* constants, OR'd together.
-
' InitialDir: the directory in which to first look
-
' Filter: a set of file filters, set up by calling
-
' AddFilterItem. See examples.
-
' FilterIndex: 1-based integer indicating which filter
-
' set to use, by default (1 if unspecified)
-
' DefaultExt: Extension to use if the user doesn't enter one.
-
' Only useful on file saves.
-
' FileName: Default value for the file name text box.
-
' DialogTitle: Title for the dialog.
-
' hWnd: parent window handle
-
' OpenFile: Boolean(True=Open File/False=Save As)
-
' Out:
-
' Return Value: Either Null or the selected filename
-
Dim OFN As tagOPENFILENAME
-
Dim strFileName As String
-
Dim strFileTitle As String
-
Dim fResult As Boolean
-
' Give the dialog a caption title.
-
If IsMissing(InitialDir) Then InitialDir = CurDir
-
If IsMissing(Filter) Then Filter = ""
-
If IsMissing(FilterIndex) Then FilterIndex = 1
-
If IsMissing(Flags) Then Flags = 0&
-
If IsMissing(DefaultExt) Then DefaultExt = ""
-
If IsMissing(Filename) Then Filename = ""
-
If IsMissing(DialogTitle) Then DialogTitle = ""
-
If IsMissing(hwnd) Then hwnd = Application.hWndAccessApp
-
If IsMissing(OpenFile) Then OpenFile = True
-
' Allocate string space for the returned strings.
-
strFileName = Left(Filename & String(256, 0), 256)
-
strFileTitle = String(256, 0)
-
' Set up the data structure before you call the function
-
With OFN
-
.lStructSize = Len(OFN)
-
.hwndOwner = hwnd
-
.strFilter = Filter
-
.nFilterIndex = FilterIndex
-
.strFile = strFileName
-
.nMaxFile = Len(strFileName)
-
.strFileTitle = strFileTitle
-
.nMaxFileTitle = Len(strFileTitle)
-
.strTitle = DialogTitle
-
.Flags = Flags
-
.strDefExt = DefaultExt
-
.strInitialDir = InitialDir
-
' Didn't think most people would want to deal with
-
' these options.
-
.hInstance = 0
-
'.strCustomFilter = ""
-
'.nMaxCustFilter = 0
-
.lpfnHook = 0
-
'New for NT 4.0
-
.strCustomFilter = String(255, 0)
-
.nMaxCustFilter = 255
-
End With
-
' This will pass the desired data structure to the
-
' Windows API, which will in turn it uses to display
-
' the Open/Save As Dialog.
-
If OpenFile Then
-
fResult = aht_apiGetOpenFileName(OFN)
-
Else
-
fResult = aht_apiGetSaveFileName(OFN)
-
End If
-
-
' The function call filled in the strFileTitle member
-
' of the structure. You'll have to write special code
-
' to retrieve that if you're interested.
-
If fResult Then
-
' You might care to check the Flags member of the
-
' structure to get information about the chosen file.
-
' In this example, if you bothered to pass in a
-
' value for Flags, we'll fill it in with the outgoing
-
' Flags value.
-
If Not IsMissing(Flags) Then Flags = OFN.Flags
-
ahtCommonFileOpenSave = TrimNull(OFN.strFile)
-
Else
-
ahtCommonFileOpenSave = vbNullString
-
End If
-
End Function
-
-
Function ahtAddFilterItem(strFilter As String, _
-
strDescription As String, Optional varItem As Variant) As String
-
' Tack a new chunk onto the file filter.
-
' That is, take the old value, stick onto it the description,
-
' (like "Databases"), a null character, the skeleton
-
' (like "*.mdb;*.mda") and a final null character.
-
-
If IsMissing(varItem) Then varItem = "*.*"
-
ahtAddFilterItem = strFilter & _
-
strDescription & vbNullChar & _
-
varItem & vbNullChar
-
End Function
-
-
Private Function TrimNull(ByVal strItem As String) As String
-
Dim intPos As Integer
-
intPos = InStr(strItem, vbNullChar)
-
If intPos > 0 Then
-
TrimNull = Left(strItem, intPos - 1)
-
Else
-
TrimNull = strItem
-
End If
-
End Function
-
Have a look at this instead... -
Function getData()
-
Dim strFile As String 'Filename
-
Dim strFileList() As String 'File Array
-
Dim intFile As Integer 'File Number
-
Dim filename As String
-
Dim path As String
-
-
DoCmd.SetWarnings False
-
-
path = "Path to folder containing excel files"
-
-
'Loop through the folder & build file list
-
strFile = Dir(path & "*.xls")
-
While strFile <> ""
-
'add files to the list
-
intFile = intFile + 1
-
ReDim Preserve strFileList(1 To intFile)
-
strFileList(intFile) = strFile
-
strFile = Dir()
-
Wend
-
'see if any files were found
-
If intFile = 0 Then
-
MsgBox "No files found"
-
Exit Sub
-
End If
-
-
'cycle through the list of files
-
For intFile = 1 To UBound(strFileList)
-
-
filename = path1 & strFileList(intFile)
-
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel3, "TableName", filename, True
-
-
Next intFile
-
-
DoCmd.SetWarnings True
-
-
End Function
-
Mary
Try running this and see what happens ... -
Function getData()
-
Dim strFile As String 'Filename
-
Dim strFileList() As String 'File Array
-
Dim intFile As Integer 'File Number
-
Dim filename As String
-
Dim path As String
-
-
DoCmd.SetWarnings False
-
-
path = "\\Dc2850\Daily Billables\Finished\"
-
-
'Loop through the folder & build file list
-
strFile = Dir(path & "*.xls")
-
While strFile <> ""
-
'add files to the list
-
intFile = intFile + 1
-
ReDim Preserve strFileList(1 To intFile)
-
strFileList(intFile) = strFile
-
strFile = Dir()
-
Wend
-
'see if any files were found
-
If intFile = 0 Then
-
MsgBox "No files found"
-
Exit Sub
-
End If
-
-
'cycle through the list of files
-
For intFile = 1 To UBound(strFileList)
-
-
filename = path & strFileList(intFile)
-
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel3, "tblClientMail", filename, True
-
-
Next intFile
-
-
DoCmd.SetWarnings True
-
-
End Function
-
Mary
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?
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.
I will add something to the button that will run a delete query. Thank you for your help on this one mary
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.
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
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.
To import Sheet named "Sheet1" -
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel3, "tblClientMail", filename, True, "Sheet1$"
-
What would be the proper way to loop through the 31 pages?
OK, got it doing all 31 pages. What about a spacific range, A1-P21
OK, got it doing all 31 pages. What about a spacific range, A1-P21
"Sheet1!A1:P21"
why would I all the sudden be getting run time error 3349 numeric field overflow?
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.
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
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
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
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.
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.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
1 post
views
Thread by Fred |
last post: by
|
2 posts
views
Thread by Fred |
last post: by
|
1 post
views
Thread by John Hewitt |
last post: by
|
3 posts
views
Thread by deko |
last post: by
|
15 posts
views
Thread by daniroy |
last post: by
|
8 posts
views
Thread by Nick M |
last post: by
|
2 posts
views
Thread by john |
last post: by
|
11 posts
views
Thread by MD |
last post: by
| | | | | | | | | | | | |