VBA: Checking Rage Exists or Not

Hi Experts

I am using following asp code

strCnxnString="Provider=MSDASQL.1;Persist Security Info=False;Extended Properties='Excel 9.0';DBQ="& Server.MapPath(strFileName) & ";Driver={Microsoft Excel Driver (*.xls)};DriverId=790;FIL=excel 9.0;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;ReadOnly=1;SafeTransactions=0;Threads=8;UID=admin;UserCommitSync=Yes;"";User Id=admin;"

Set cnnXL= server.CreateObject("ADODB.Connection")
Set objXL=server.CreateObject("ADODB.RecordSet")
sqlXL="SELECT * FROM Range9"     '>> This range is to be checked whether it exists
cnnXL.Open strCnxnString,3,3
objXL.Open SqlXL,cnnXL,3,3

My question is
>>sqlXL="SELECT * FROM Range9"
if there is no such range  "Range9" it will give error
is there any direct way to check whether specified range Exists or not ?

Thanks
Shiju


LVL 14
Shiju SasidharanAssoc Project ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bruintjeCommented:
Hello shijusn,

since you can't do anything when it errors [no data will be returned] you could add an errorhandler catching the specific error, only when you have a direct handle to an excel sheet you could check through the object if a range existed don't know of a way through ADO

----------
on error goto Errhandle

Set cnnXL= server.CreateObject("ADODB.Connection")
Set objXL=server.CreateObject("ADODB.RecordSet")
sqlXL="SELECT * FROM Range9"     '>> This range is to be checked whether it exists
cnnXL.Open strCnxnString,3,3
objXL.Open SqlXL,cnnXL,3,3

'do all other work

exit sub
Errhandle:
  if Err.Number = ?? then
    'do something here
  else
    'another error
  end if
  'cleanup objects
  exit sub
end sub
----------

hope this helps a bit
bruintje
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bruintjeCommented:
assuming you do this directly from asp
0
EDDYKTCommented:
OR

on error resume next
Set cnnXL= server.CreateObject("ADODB.Connection")
Set objXL=server.CreateObject("ADODB.RecordSet")
sqlXL="SELECT * FROM Range9"     '>> This range is to be checked whether it exists
if err.number = 0 then
cnnXL.Open strCnxnString,3,3
objXL.Open SqlXL,cnnXL,3,3
end if
0
bruintjeCommented:
delete no refund
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.