Aiysha
asked on
Find a database and run a query
I want to create a macro that search certain folder for mdb files. If it finds the file, the macro saves the path and name of the mdb and runs a query.
ASKER
all mdbs in path "c:\program files\CompanyDatabase"..th e results to be saved in xls file at "c:\"
Thank you .
Thank you .
can you be please a bit clearer of what query you speak of, and how you want the results to be saved into the .xls?
1 sheet per mdf found for example?
1 sheet per mdf found for example?
ASKER
no the resultin sheet should have
Colum1 colum2
Name of the database
Colum1 colum2
Name of the database
ASKER
sorry, for the post it was not complete the second column should have the path of the database.
Thanks:
dim appExcel as object
set appExcel = CreateObject("Excel.Applic ation")
dim objBook as object
set objBook = appExcel.Workbooks.Add
dim objSheet as object
set objSheet = objBook.Sheets(1)
dim fso as object
set fso = createobject("Scripting.Fi lesystemOb ject")
dim folder as object
set folder = fso.GetFolder("c:\program files\CompanyDatabase")
dim file as object
dim lngRow as long
for each file in folder.files
if upper(right(file.name,3)) = "MDB" then
lngRow = lngRow + 1
objSheet.Cells(lngRow,1) = file.name
objSheet.Cells(lngRow,2) = file.path
end if
next
objBook.Save "C:\mdb.xls"
objBook.close
set objBook = nothing
appExcel.Quit
set appExcel = nothing
set file = nothing
set folder = nothing
set fso = nothing
dim appExcel as object
set appExcel = CreateObject("Excel.Applic
dim objBook as object
set objBook = appExcel.Workbooks.Add
dim objSheet as object
set objSheet = objBook.Sheets(1)
dim fso as object
set fso = createobject("Scripting.Fi
dim folder as object
set folder = fso.GetFolder("c:\program files\CompanyDatabase")
dim file as object
dim lngRow as long
for each file in folder.files
if upper(right(file.name,3)) = "MDB" then
lngRow = lngRow + 1
objSheet.Cells(lngRow,1) = file.name
objSheet.Cells(lngRow,2) = file.path
end if
next
objBook.Save "C:\mdb.xls"
objBook.close
set objBook = nothing
appExcel.Quit
set appExcel = nothing
set file = nothing
set folder = nothing
set fso = nothing
small correction:
instead of objBook.Save it should be objBook.SaveAs
instead of objBook.Save it should be objBook.SaveAs
ASKER
I should have mentioned this in my question, will the above code search the sub folders under CompanyDatabase?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I get the following error on the line upper(right(file.name,3)) = "MDB" then
"sub or function not defined" is there a library I need to be referring to ?
"sub or function not defined" is there a library I need to be referring to ?
I get the following error on the line ucase(right(file.name,3)) = "MDB" then
ASKER
angelIII convert the function into sub and use the call method..and also declare excel variables as public..it will work..
a certain filename or any .mdb file?
also, where should it save the name of the mdb file?