Sorting IP's with Access SQL via ADO...

I have this SQL, works great in MSAcess.

SELECT tblIPs.ID, tblIPs.IPAddress
ORDER BY CLng(Left([IPAddress],InStr([IPAddress],".")-1)), CLng(Mid([IPAddress],InStr([IPAddress],".")+1,InStr(InStr([IPAddress],".")+1,[IPAddress],".")-InStr([IPAddress],".")-1)), CLng(Mid([IPAddress],InStr(InStr([IPAddress],".")+1,[IPAddress],".")+1,InStr(InStr(InStr([IPAddress],".")+1,[IPAddress],".")+1,[IPAddress],".")-InStr(InStr([IPAddress],".")+1,[IPAddress],".")-1)), CLng(Mid([IPAddress],InStr(InStr(InStr([IPAddress],".")+1,[IPAddress],".")+1,[IPAddress],".")+1));

But when I try to use it in a TADOQuery, I get this error:

"[Microsoft][ODBC Microsoft Access Driver] '' is not a valid name. Make sure that it does not include
invalid characters or punctuation and that it is not too long."

What's up with that?

How could I write the above query to sort IPs using SQL. I DO NOT WANT TO BREAK UP THE IP INTO FOUR INTEGER COLUMNS.

LVL 27
Eddie ShipmanAll-around developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

If you are using Acces2000 + you could try this

SELECT Table1.ipaddress
FROM Table1
ORDER BY Val([ipaddress]), Val(StrReverse(Val(StrReverse([ipaddress]+"1"))))
Eddie ShipmanAll-around developerAuthor Commented:
Very good but not quite right. Try sorting these:

Pay close attention to the 214 and 217 blocks. is listed before
and is listed before

which are both incorrect.
Eddie ShipmanAll-around developerAuthor Commented:
I found a way to do it based upon the formula on this page:

I can sort descending by changing the asc in the first orderby to desc.

SELECT tblIPs.ID, tblIPs.IPAddress
  Val(Left([IPAddress],InStr([IPAddress],".")-1)) * 16777216 asc,
  Val(Mid([IPAddress],InStr([IPAddress],".")+1,InStr(InStr([IPAddress],".")+1,[IPAddress],".")-InStr([IPAddress],".")-1)) * 65536,
  Val(Mid([IPAddress],InStr(InStr([IPAddress],".")+1,[IPAddress],".")+1,InStr(InStr(InStr([IPAddress],".")+1,[IPAddress],".")+1,[IPAddress],".")-InStr(InStr([IPAddress],".")+1,[IPAddress],".")-1)) * 256,

This works exactly like I want.

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

When you save the IP address into the DB you could also save a sort column, ie

IP_Address          sort_column

this will speed up the sorting, how many addresses are you saving?
Eddie ShipmanAll-around developerAuthor Commented:
I am importing a Black Ice attack list and there could be upto 50,000 lines. I do not want to have
extra fields just for sorting. If I wsa going to do that, I might as well store the IP's as four integers.
There are 2 IP's per line that need to be stored, btw.

On another note. We have decided to dump Access due to performance and have gone to mySQL.
Access imported 48,000 lines in 7 min, mySQL imported the same file in 1 min. using the exact same

But sorting and filtering is very slow using the ZEOS DB components.
With 50,000 records it would be faster to add the extra column, than evaluate your sort expression. Disk space is cheap try it and see? I would expect a major difference in sorting speed.
Eddie ShipmanAll-around developerAuthor Commented:
Since moving to mySQL, the sorting as well as the importing is extremely fast. No need for it.
User resolved; points (125) refunded and question closed.

EE Admin

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

From novice to tech pro — start learning today.