I get a daily excel file for a entire month which I want to transfer into Access at the end of the month. So, there are around 20-25 excel files I get by the end of the month, and I would like to import them into Access using a button. The data contained in the excel files is similar, so there should no formatting issues while importing.
I searched through the forums and found the code by mmccarthy for importing excel files. I tried using that code, but it keeps giving me a error message "No files found" -
Private Sub Command3_Click()
-
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 = "C:\Documents and Settings\KK\Desktop\Test\"
-
-
'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"
-
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 Sub
-
Can someone tell me what I might be doing wrong ??
Thanks.
28 19192
So all these *.xls files are sitting in a folder called test on your desktop?
Try placing the wildcard symbol in your last subdirectory in your file path:
path = "C:\Documents and Settings\KK\Desktop\Test\*.*"
mmccarthy - Yes, all these .xls files on my desktop in a folder named Test. I had deleted the line Exit Sub below the MsgBox. Was that the correct thing to do ?
puppydog - I tried that, but still tells me No files found. When I then click on Ok, it tells me Run-time error '9': Subscript out of range. When I click on debug, it takes me to this line in the code - For intFile = 1 To UBound(strFileList)
Thanks.
mmccarthy - Yes, all these .xls files on my desktop in a folder named Test. I had deleted the line Exit Sub below the MsgBox. Was that the correct thing to do ?
puppydog - I tried that, but still tells me No files found. When I then click on Ok, it tells me Run-time error '9': Subscript out of range. When I click on debug, it takes me to this line in the code - For intFile = 1 To UBound(strFileList)
Thanks.
Set a breakpoint on line14 in the example. When the code breaks mouse over the strFile variable and see what value it contains.
I am not sure if this is what u wanted. But after creating the breakpoint, it tells me strFile = ""
How do i check the strFile variable value ?
I am not sure if this is what u wanted. But after creating the breakpoint, it tells me strFile = ""
Now with the same breakpoint mouse over path and see if it is set to where you think?
Next go to your desktop and open the test folder. Right click on any of the *.xls files and go to properties. Check the Location. Make sure it letter for letter corresponds to the path in your file.
Lastly are you sure these files have an *.xls extension.
Now with the same breakpoint mouse over path and see if it is set to where you think?
Next go to your desktop and open the test folder. Right click on any of the *.xls files and go to properties. Check the Location. Make sure it letter for letter corresponds to the path in your file.
Lastly are you sure these files have an *.xls extension.
When taking the mouse the path, it is exactly where i want to select the excel files from.
The properties of the folder also shows the same location.
All the files do have a .xls extension.
When my code breaks down, and i click on debug, it points to For intFile = 1 To UBound(strFileList)
When taking the mouse the path, it is exactly where i want to select the excel files from.
The properties of the folder also shows the same location.
All the files do have a .xls extension.
When my code breaks down, and i click on debug, it points to For intFile = 1 To UBound(strFileList)
Thats because you removed the End Sub. The code is continuing without any files available.
There doesn't seem to be any problem with the code I don't understand why the files can't be found.
Thats because you removed the End Sub. The code is continuing without any files available.
There doesn't seem to be any problem with the code I don't understand why the files can't be found.
So do i need to put the EndSub back ?
Also, i am using Access 07, though it is saved as Access 03. Could that be a issue ?
So do i need to put the EndSub back ?
yes
Also, i am using Access 07, though it is saved as Access 03. Could that be a issue ?
This can always be an issue. My advice is to run a compile on the database (can be found in the debug menu) and see if any errors appear.
Is there any reason this file cannot be opened in 03 or converted to 07
yes
This can always be an issue. My advice is to run a compile on the database (can be found in the debug menu) and see if any errors appear.
Is there any reason this file cannot be opened in 03 or converted to 07
Where do i enter the End Sub statement ? before or after line 25 ?
Where do i enter the End Sub statement ? before or after line 25 ?
before it under the msgbox
before it under the msgbox
It gives a error message "Block If without End If"
It gives a error message "Block If without End If"
Does it indicate where this error is occuring. The only If statement in this code is the following: -
'see if any files were found
-
If intFile = 0 Then
-
MsgBox "No files found"
-
End Sub
-
End If
-
Does it indicate where this error is occuring. The only If statement in this code is the following: -
'see if any files were found
-
If intFile = 0 Then
-
MsgBox "No files found"
-
End Sub
-
End If
-
When i inserted End Sub after the Msg, and ran it, it highlights the End Sub statement and displays that error.
When i inserted End Sub after the Msg, and ran it, it highlights the End Sub statement and displays that error.
Sorry my fault.
Should be
not
Sorry my fault.
Should be
not
I still get the msg Files not found. I wonder why.
I still get the msg Files not found. I wonder why.
Did it compile OK?
Did you try running it in 03 or save a copy and convert it to 07 to see if you still have a problem.
One quick point, dont forget to reset your message warning before you exit or you will find yourself saving changes deleting things completely by accident and never know it: - MsgBox "No files found"
-
DoCmd.SetWarnings True
-
Exit Sub
-
End If
-
After you answer Mary's last post try checking the very first command and see where it stands, that will help.
Your code appears to be fine yet I have seen some strange and abnormal behavior with the DIR function before.
Did it comile OK?
Did you try running it in 03 or save a copy and convert it to 07 to see if you still have a problem.
Still does not work. I tried running in 03, but gives me same message.
When I created a break point, and ran the mouse over the path it shows " " instead of the actual path.
After you answer Mary's last post try checking the very first command and see where it stands, that will help.
Your code appears to be fine yet I have seen some strange and abnormal behavior with the DIR function before.
What do you recommend I do with the 1st line ?
Thanks.
Obviously there is an issue with the path or file names somewhere. Try using the following function to select the files you want and I am sue that you will be able to move on to the next level. -
'use this wherever appropriate
-
If GetFileD = False Then Exit Sub
-
- Public Function GetFileD() As Boolean
-
Dim myDialog As FileDialog
-
Dim vrtSelectedItem As Variant
-
Dim strSearchPath As String
-
Dim Cnt As Integer
-
Set myDialog = Application.FileDialog(msoFileDialogOpen)
-
Dim strDbName As String
-
strSearchPath = strDbName
-
With myDialog
-
.AllowMultiSelect = True
-
.Filters.Add "Excel Files", "*.xls", 1
-
.Title = "Please Locate the Files to Import!"
-
.InitialFileName = strSearchPath
-
If .Show = -1 Then
-
-
For Each vrtSelectedItem In .SelectedItems
-
'The importit is a function you can use to import your files.
-
ImportIt (vrtSelectedItem)
-
GetFileD = True
-
Next vrtSelectedItem
-
MsgBox "All data imported into the NewData Table." & vbCrLf & vbCrLf & "We will now post any new data into the data table!"
-
'The user pressed Cancel.
-
Else
-
End If
-
Set myDialog = Nothing
-
End With
-
End Function
-
Obviously there is an issue with the path or file names somewhere. Try using the following function to select the files you want and I am sue that you will be able to move on to the next level. -
'use this wherever appropriate
-
If GetFileD = False Then Exit Sub
-
- Public Function GetFileD() As Boolean
-
Dim myDialog As FileDialog
-
Dim vrtSelectedItem As Variant
-
Dim strSearchPath As String
-
Dim Cnt As Integer
-
Set myDialog = Application.FileDialog(msoFileDialogOpen)
-
Dim strDbName As String
-
strSearchPath = strDbName
-
With myDialog
-
.AllowMultiSelect = True
-
.Filters.Add "Excel Files", "*.xls", 1
-
.Title = "Please Locate the Files to Import!"
-
.InitialFileName = strSearchPath
-
If .Show = -1 Then
-
-
For Each vrtSelectedItem In .SelectedItems
-
'The importit is a function you can use to import your files.
-
ImportIt (vrtSelectedItem)
-
GetFileD = True
-
Next vrtSelectedItem
-
MsgBox "All data imported into the NewData Table." & vbCrLf & vbCrLf & "We will now post any new data into the data table!"
-
'The user pressed Cancel.
-
Else
-
End If
-
Set myDialog = Nothing
-
End With
-
End Function
-
Thanks.
But it tells me Expected End Sub...I inserted the End Sub statement at the end, but it still gives me that error.
ok it would appear that we need to back way up here. at the top of this form module it should look like:
Option Compare Database
Option Explicit
Then press the menu button Debug then the button Compile, if your a keyboard nut press alt. D, L and it should compile hopefully with no errors. If you do get an error then it should give you an error response with a button that says debug. Clicking on debug should highlight a particular line of code. What we need is this highlighted line and a line or two above and below so we can help diagnose the problem.
I tried the code. It works fine
Adam.
i think you might need to use:
not just dir
i think you might need to use:
not just dir
This is a Simple code to import all the Excel files into Access.
Function Impo_allExcel()
Dim myfile
Dim mypath
mypath = "n:\importxls\aramiska\"
Do
myfile = Dir(mypath & "*.xls")
'this will import ALL the excel files (one at a time, but automatically) in this folder. Make sure that's what you want.
DoCmd.TransferSpreadsheet acImport, 8, "aramiskaimport2", mypath & myfile
myfile = Dir
Loop Until myfile = ""
End Function
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Grim Reaper |
last post by:
I am importing a .csv file into Access that has 37 fields. My problem is
that sometimes the last field only has data at the end of the column (it
looks like when you import a file into Access, for...
|
by: Geoff Jones |
last post by:
Hi
I have a question which I hope somebody can answer. I have written a VB
application with which I want to import an Excel file, analyze the data
within it and do some calculations. There are...
|
by: Snozz |
last post by:
The short of it:
If you needed to import a CSV file of a certain structure on a regular
basis(say 32 csv files, each to one a table in 32 databases), what
would be your first instinct on how to...
|
by: hharriel |
last post by:
Hi,
I am hoping someone can help me with an issue I am having with excel
and ms access. I have collected data (which are in individual excel
files) from 49 different school districts. All...
|
by: mukeshsrivastav |
last post by:
dear sir
i want to move form excel to access.
i have 5 excel file having same formats and fields.now i want to import all data in one access table.
importing one file is easy .but importing and...
|
by: thadson |
last post by:
Hi,
I'm trying to import specific cells from MS Excel 2000 spreadsheets to MS Access 2000 tables then move the spreadsheets to a different directory.
I'm very new to this and I'm having trouble...
|
by: ciaran.hudson |
last post by:
Hi
I have multiple excel files of the same format in a directory.
They are called book1.xls, book2.xls, book3.xls and so on.
What is the easiest way to import the tab named sheet1 from each of...
|
by: Harshe |
last post by:
hello all,
I am trying to code, but i am just stuck after importing one sheet. so here is the gist of what i need help with.
In a workbook at the start of the year (january) i will have 4...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |