PC Review


Reply
Thread Tools Rate Thread

CopyFromRecordset only returns first record

 
 
aardvick
Guest
Posts: n/a
 
      2nd Oct 2008
Below is my code. The message boxes confirm that the query looks good and
returns 4 records. But only the first record is copied onto the worksheet.
What am I doing wrong?
tia,
Aardvick

Sub test()
Dim db As DAO.Database, rs As DAO.Recordset, ws As Worksheet
Dim lName As String, hits As Integer

lName = "Anderson"
strQry = "SELECT * FROM [MyTable] WHERE [Last Name] = """ & lName & """"
MsgBox "Query looks like: " & strQry

Set db = OpenDatabase("C:\myDB.mdb")
Set rs = db.OpenRecordset(strQry, dbOpenDynaset)

rs.MoveLast
hits = rs.RecordCount
MsgBox "Number of records returned =" & hits

If hits > 0 Then
Set ws = Worksheets.Add
Count = rs.Fields.Count
For I = 0 To Count - 1
ws.Cells(1, I + 1).Value = rs.Fields(I).Name
Next
ws.Range("A2").CopyFromRecordset rs
End If
End Sub
 
Reply With Quote
 
 
 
 
aardvick
Guest
Posts: n/a
 
      2nd Oct 2008
Correction! It only returns the LAST record.

"aardvick" wrote:

> Below is my code. The message boxes confirm that the query looks good and
> returns 4 records. But only the first record is copied onto the worksheet.
> What am I doing wrong?
> tia,
> Aardvick
>
> Sub test()
> Dim db As DAO.Database, rs As DAO.Recordset, ws As Worksheet
> Dim lName As String, hits As Integer
>
> lName = "Anderson"
> strQry = "SELECT * FROM [MyTable] WHERE [Last Name] = """ & lName & """"
> MsgBox "Query looks like: " & strQry
>
> Set db = OpenDatabase("C:\myDB.mdb")
> Set rs = db.OpenRecordset(strQry, dbOpenDynaset)
>
> rs.MoveLast
> hits = rs.RecordCount
> MsgBox "Number of records returned =" & hits
>
> If hits > 0 Then
> Set ws = Worksheets.Add
> Count = rs.Fields.Count
> For I = 0 To Count - 1
> ws.Cells(1, I + 1).Value = rs.Fields(I).Name
> Next
> ws.Range("A2").CopyFromRecordset rs
> End If
> End Sub

 
Reply With Quote
 
Tim Williams
Guest
Posts: n/a
 
      3rd Oct 2008
Try

rs.MoveFirst

before doing copyfromrecordset

Tim

"aardvick" <(E-Mail Removed)> wrote in message
newsDEA6CB4-2E80-4B0F-971B-(E-Mail Removed)...
> Below is my code. The message boxes confirm that the query looks good and
> returns 4 records. But only the first record is copied onto the
> worksheet.
> What am I doing wrong?
> tia,
> Aardvick
>
> Sub test()
> Dim db As DAO.Database, rs As DAO.Recordset, ws As Worksheet
> Dim lName As String, hits As Integer
>
> lName = "Anderson"
> strQry = "SELECT * FROM [MyTable] WHERE [Last Name] = """ & lName & """"
> MsgBox "Query looks like: " & strQry
>
> Set db = OpenDatabase("C:\myDB.mdb")
> Set rs = db.OpenRecordset(strQry, dbOpenDynaset)
>
> rs.MoveLast
> hits = rs.RecordCount
> MsgBox "Number of records returned =" & hits
>
> If hits > 0 Then
> Set ws = Worksheets.Add
> Count = rs.Fields.Count
> For I = 0 To Count - 1
> ws.Cells(1, I + 1).Value = rs.Fields(I).Name
> Next
> ws.Range("A2").CopyFromRecordset rs
> End If
> End Sub



 
Reply With Quote
 
MikeR
Guest
Posts: n/a
 
      3rd Oct 2008
Tim Williams wrote:
> Try
>
> rs.MoveFirst
>
> before doing copyfromrecordset
>
> Tim
>
> "aardvick" <(E-Mail Removed)> wrote in message
> newsDEA6CB4-2E80-4B0F-971B-(E-Mail Removed)...
>> Below is my code. The message boxes confirm that the query looks good and
>> returns 4 records. But only the first record is copied onto the
>> worksheet.
>> What am I doing wrong?
>> tia,
>> Aardvick
>>
>> Sub test()
>> Dim db As DAO.Database, rs As DAO.Recordset, ws As Worksheet
>> Dim lName As String, hits As Integer
>>
>> lName = "Anderson"
>> strQry = "SELECT * FROM [MyTable] WHERE [Last Name] = """ & lName & """"
>> MsgBox "Query looks like: " & strQry
>>
>> Set db = OpenDatabase("C:\myDB.mdb")
>> Set rs = db.OpenRecordset(strQry, dbOpenDynaset)
>>
>> rs.MoveLast
>> hits = rs.RecordCount
>> MsgBox "Number of records returned =" & hits
>>
>> If hits > 0 Then
>> Set ws = Worksheets.Add
>> Count = rs.Fields.Count
>> For I = 0 To Count - 1
>> ws.Cells(1, I + 1).Value = rs.Fields(I).Name

rs.Movenext
>> Next
>> ws.Range("A2").CopyFromRecordset rs
>> End If
>> End Sub

>
>

What Tim said plus rs.MoveNext

Mike
 
Reply With Quote
 
aardvick
Guest
Posts: n/a
 
      3rd Oct 2008
Thanks a million! I knew it must be something obvious : )

"Tim Williams" wrote:

> Try
>
> rs.MoveFirst
>
> before doing copyfromrecordset
>
> Tim
>
> "aardvick" <(E-Mail Removed)> wrote in message
> newsDEA6CB4-2E80-4B0F-971B-(E-Mail Removed)...
> > Below is my code. The message boxes confirm that the query looks good and
> > returns 4 records. But only the first record is copied onto the
> > worksheet.
> > What am I doing wrong?
> > tia,
> > Aardvick
> >
> > Sub test()
> > Dim db As DAO.Database, rs As DAO.Recordset, ws As Worksheet
> > Dim lName As String, hits As Integer
> >
> > lName = "Anderson"
> > strQry = "SELECT * FROM [MyTable] WHERE [Last Name] = """ & lName & """"
> > MsgBox "Query looks like: " & strQry
> >
> > Set db = OpenDatabase("C:\myDB.mdb")
> > Set rs = db.OpenRecordset(strQry, dbOpenDynaset)
> >
> > rs.MoveLast
> > hits = rs.RecordCount
> > MsgBox "Number of records returned =" & hits
> >
> > If hits > 0 Then
> > Set ws = Worksheets.Add
> > Count = rs.Fields.Count
> > For I = 0 To Count - 1
> > ws.Cells(1, I + 1).Value = rs.Fields(I).Name
> > Next
> > ws.Range("A2").CopyFromRecordset rs
> > End If
> > End Sub

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL that returns every record twice Aino Microsoft Access Queries 3 6th Oct 2007 01:34 PM
Cursor returns to first record in subform after updating second record JGlubb Microsoft Access Forms 2 13th Jul 2006 03:31 PM
ADO Returns Only Last Record =?Utf-8?B?S2VuIEh1ZHNvbg==?= Microsoft Excel Programming 3 13th May 2005 06:38 PM
Update record - returns to the first record in the record set Kim Microsoft Access Forms 0 22nd Apr 2005 04:20 PM
Recordset only returns first record, when Query SQL returns 51 records Alex Microsoft Access VBA Modules 1 16th Jul 2003 01:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:09 PM.