View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips Bob Phillips is offline
external usenet poster
 
Posts: 10,593
Default SQL Select string

If you only want the field names, the recordset has a fields collection, and
you can get at it like so

For i = 0 To oRS.Fields.Count - 1

MsgBox oRS.Fields(i).Name
Next i

where oRS is the recordsset returned from the query. You can do a full query
as originally

szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "] ;"

to build the recordset

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Geoff" wrote in message
...
I appreciate the explanation however I am only looking for headers, not
data
under the headers. If I understand correctly, the clause WHERE LastName =
myname would look in the column LastName for whatever value has been
assigned
to "myname". That is not necessary here, I only want to get specific
headers.

Whilst I can get the whole header row using ADO for speed, it would be
even
better if I can just pull the specific header names (if they exist). If
the
last appears strange then you only have to consider how many different
ways
there are of categorising the name "Bob", it can be First Name, Christian
Name, ForeName, ForeName(s) for example.

A shortened example of the headers extracted from individual wbooks with
ADO
are shown

Subscription ID Delegate ID Order Type etc
List code Honorific Forename etc
URN HONORIFIC INITIALS
Subscription ID Delegate ID Order Type
keycode Source Title
Order Date Name
Title Christian Surname JobTitle
URN HONORIFIC INITIALS FORENAME

The purpose of doing a first row extraction using ADO is a fast prelim
search to find wbooks with similar patterns of headers. Next stage is to
sort the wbooks into groups and then present them to an existing form for
data filtering. Doing it in groups, rather than singly, minimises user
input
to the form and potentially saves a great deal of time.

Despite a lot of experiments I am unable to work out the syntax of the
Query. I have modified the SELECT * part ot the query and also tried
WHERE
rs.fields(x).Name LIKE whatever
But nothing works.

I would really appreciate if it can be solved and apologise if I have
misunderstood your comments.

Geoff

"Bob Phillips" wrote:

An xls list can be treated as just another table, and queried the same
way.
For this, we need to have headers in row, and this is the column names
for
the SQL query. You can query that table, using WHERE clauses against
those
column names. You must specify each column to qualify (WHERE) with the
value
to be retrieved. To go back to my original

szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "] " & _
"WHERE LastName = '" & myName & "' AND KeyCode = '" &
myCode

We have column names (headings) of LastName and KeyCode, this must be
exact
match. myName and myCode are the variables that you wish to restrict the
query to.


--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my
addy)



"Geoff" wrote in message
...
So the WHERE clause should read something like:

WHERE fieldname = Last Name, Keycode, First Name and the other specific
headers I am looking for.

Is this possible working with lists in .xls wbooks?

Geoff

"Geoff" wrote:

That would give me the contents of columns LastName and KeyCode with
filters
myName and myCode.

I am just reading the first row of multiple Excel wbooks. This is to
establish data patterns. Column names are random and specific names
(if
they
are there) can be in any column order.

Wbook1 keycode Source Title
Wbook2 List code Honorific Forename Surname keycode etc etc

Geoff

"Bob Phillips" wrote:

szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "] "
&
_
"WHERE LastName = '" & myName & "' AND KeyCode = '"
&
myCode
& "';"


--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in
my
addy)



"Geoff" wrote in message
...
Hi
I use ADO to get the first row of data from multiple unopened
wbooks.
This string gets the contents of every cell in row 1 given that
SourceSheet
= "Sheet1" and SourceRange = "A1:IV1"

szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ &
"];"

Instead of all the row how can I write a string to result in
specific
names
eg Last Name, Keycode, Company etc?

T.I.A.

Geoff