Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / October 2006

Tip: Looking for answers? Try searching our database.

Expanation of OpenRecordSet Statement Please

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg - 17 Oct 2006 17:19 GMT
I have a spreadsheet  "C:\myWorkbook1.xls" with a named range
"mydatabase"

mydatabase range spans a1:C4 and contains the following data:

Name  Age Address
Bill      25   Ohio
Joe     30    New York
Mary   35    New Jersey

The Column headings "Name, Age and Address" are in row 1 (or A1:C1)

I found some code on the Word MVP FAQ website:

http://word.mvps.org/FAQs/InterDev/XLToWordWithDAO.htm

Sub Test()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = OpenDatabase("C:\myBook1.xls", False, False, "Excel 8.0")
Set rs = db.OpenRecordset("SELECT * FROM `mydatabase`")
While Not rs.EOF
 MsgBox rs.Fields(0).Value
 rs.MoveNext
Wend
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub

I would expect the While Not rs.EOF process to return:

Name
Bill
Joe
Mary

as it cycles through the rs first record to last.  It actually returns:

Bill
Joe
Mary

It does not return "Name" which is in the within the defined range
"mydatabase,"  the While Not
rs.EOF seems to skip the first row of the range.

I assume that skipping the first RS in the defined range must have
something to do with how this line is constructed:

Set rs = db.OpenRecordset("SELECT * FROM `mydatabase`")

There is no explanation of that line in the FAQ article and I can find
no explanation in the VBA help file either.  Especially related to the
"*."

Can anyone enlighten me as to why the first row in my defined range is
skipped when I run the code above?

Thanks.
PapaDos - 18 Oct 2006 08:12 GMT
By default, the first row is treated as the header and is used to define
fields names.
You can specify that your database has no headers by using

"Excel 8.0;HDR=NO"

instead of

"Excel 8.0"

in the 4rth parameter of the opendatabase function.
Signature

Regards,
Luc.

"Festina Lente"

> I have a spreadsheet  "C:\myWorkbook1.xls" with a named range
> "mydatabase"
[quoted text clipped - 57 lines]
>
> Thanks.
Greg Maxey - 18 Oct 2006 11:26 GMT
Thank you very much.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> By default, the first row is treated as the header and is used to
> define fields names.
[quoted text clipped - 70 lines]
>>
>> Thanks.
Greg - 18 Oct 2006 13:52 GMT
PapaDos,

I made the changes that you suggested and confirmed the results.
Thanks again.  That solution however presented another problem :-(

Using the same example database:

Name  Age Address
Bill      25   Ohio
Joe     30    New York
Mary   35    New Jersey

I want to run a macro to build a table in Word with the data:

Before having your tip, I used:

Sub BuildaTableinWordWithExcelData()
Dim myDB As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim j As Long
Dim dtable As Table, drow As Row
Set myDB = OpenDatabase("C:\myBook1.xls", False, False, "Excel 8.0")
Set myActiveRecord = myDB.OpenRecordset("mySSRange", dbOpenForwardOnly)
Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=1, numcolumns:=myActiveRecord.Fields.Count)
Set drow = dtable.Rows(1)
'Add the column header data.
For i = 1 To myActiveRecord.Fields.Count
   drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1).Name
Next i
Set drow = dtable.Rows.Add
'Populate the data
Do While Not myActiveRecord.EOF
   For i = 1 To myActiveRecord.Fields.Count
       drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
   Next i
   Set drow = dtable.Rows.Add
   myActiveRecord.MoveNext
Loop
drow.Delete
myActiveRecord.Close
myDB.Close
End Sub

Using your suggestion, I used:

Sub BuildaTableinWordWithExcelData()
Dim myDB As Database
Dim myActiveRecord As Recordset
Dim i As Long
Dim j As Long
Dim dtable As Table, drow As Row
Set myDB = OpenDatabase("C:\myBook1.xls", False, False, "Excel
8.0;HDR=NO")
Set myActiveRecord = myDB.OpenRecordset("mySSRange", dbOpenForwardOnly)
Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=1, numcolumns:=myActiveRecord.Fields.Count)
Set drow = dtable.Rows(1)
Do While Not myActiveRecord.EOF
   For i = 1 To myActiveRecord.Fields.Count
       drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)
   Next i
   Set drow = dtable.Rows.Add
   myActiveRecord.MoveNext
Loop
drow.Delete
myActiveRecord.Close
myDB.Close
End Sub

When I first ran this code, I got a Runtime error: 94 "Invalid use of
Null" when the code tried to write the label "Age" in the first row.  I
suspected that this had something to do with mixed numbers and text in
the column and so I formatted column 2 as "Text" vice the default
"General."

This seems to work, but I was wondering if there was something that I
could put in the code above that would work around this error
automatically to avoid having to specifically set a format?

Thanks.

> Thank you very much.
>
[quoted text clipped - 78 lines]
> >>
> >> Thanks.
PapaDos - 18 Oct 2006 23:31 GMT
Accessing Excel files through a database engine has some limitations.
This is one of them.

If you ONLY need to READ the data, adding ";IMEX=1" to the 4rth parameter
may help:

"Excel 8.0; HDR=NO; IMEX=1;"

This would convert mixed data types columns to text when possible.
But it only looks at a few lines (default is 8, I think) before deciding if
the column contains more than one data type, so this is not much better. And
do not ever try to update or append to the recordset data with this option...
Signature

Regards,
Luc.

"Festina Lente"

> PapaDos,
>
[quoted text clipped - 161 lines]
> > >>
> > >> Thanks.
Greg Maxey - 19 Oct 2006 00:12 GMT
PapaDos,

Thanks for the information.  You seem to have a treasure chest of bits to
add to the 4th parameter with which to enlighten the masses.  Where can a
list of these be found for reference?

Thanks again.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Accessing Excel files through a database engine has some limitations.
> This is one of them.
[quoted text clipped - 178 lines]
>>>>>
>>>>> Thanks.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.