Link to home
Start Free TrialLog in
Avatar of Aiysha
AiyshaFlag for United States of America

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.
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

>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.

a certain filename or any .mdb file?

also, where should it save the name of the mdb file?
Avatar of Aiysha

ASKER

all mdbs in path "c:\program files\CompanyDatabase"..the results to be saved in xls file at "c:\"

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?
Avatar of Aiysha

ASKER

no the resultin sheet should have

Colum1         colum2
Name of the database
Avatar of Aiysha

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.Application")
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.FilesystemObject")
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
Avatar of Aiysha

ASKER

I should have mentioned this in my question, will the above code search the sub folders under CompanyDatabase?
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aiysha

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 ?

I get the following error on the line ucase(right(file.name,3)) = "MDB" then
Avatar of Aiysha

ASKER

angelIII  convert the function into sub and use the call method..and also declare excel variables as public..it will work..