Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Try
rs.MoveFirst before doing copyfromrecordset Tim "aardvick" wrote in message ... 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 |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Tim Williams wrote:
Try rs.MoveFirst before doing copyfromrecordset Tim "aardvick" wrote in message ... 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 |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thanks a million! I knew it must be something obvious : )
"Tim Williams" wrote: Try rs.MoveFirst before doing copyfromrecordset Tim "aardvick" wrote in message ... 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 |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Index with mulitple value returns and muliple column returns | Excel Worksheet Functions | |||
Open new record with selected fields from previous record | Excel Discussion (Misc queries) | |||
Date returns always returns: 00 January 1900 | Excel Worksheet Functions | |||
ADO Returns Only Last Record | Excel Programming | |||
Record Macro - Record custom user actions | Excel Programming |