×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Populating a Range from a Recordset - MS Access VBA

Populating a Range from a Recordset - MS Access VBA

Populating a Range from a Recordset - MS Access VBA

(OP)
This is the fastest way I've found to populate a range from a DAO.Recordset without having to move thru the recordset via a For Next Loop.
Public Sub Trial()
Dim rng as Range
Dim wrksht as Worksheet
Dim rst as Recordset (DAO.Recordset)
Dim strSQL1 as String

strSQL1 = "SELECT [TABLE].* FROM [TABLE];" '<-MS Access SQL Syntax

Set rst = db.OpenRecordset(strSQL1, dbOpenDynaset)
rst.MoveLast
MsgBox "Number of records: " & rst.RecordCount, , Title:="Number of Records Returned" 'msgbox denoting how many records returned
rst.MoveFirst

Set rng = wrksht.cells(2,1)
rng.CopyFromRecordset rst, rst.RecordCount
End Sub

RE: Populating a Range from a Recordset - MS Access VBA

Hi Shiznaw and welcome to Tek-Tips.

Is this a question or simply an observation? I don't see any code that sets the wrksht object.

Also, it's best to include TGML to format your code like this:

CODE --> vba

Public Sub Trial()
  Dim rng as Range
  Dim wrksht as Worksheet
  Dim rst as Recordset (DAO.Recordset)
  Dim strSQL1 as String

  strSQL1 = "SELECT [TABLE].* FROM [TABLE];" '<-MS Access SQL Syntax

  Set rst = db.OpenRecordset(strSQL1, dbOpenDynaset)
  rst.MoveLast
  MsgBox "Number of records: " & rst.RecordCount, , Title:="Number of Records Returned" 'msgbox denoting how many records returned
  rst.MoveFirst

  Set rng = wrksht.cells(2,1)
  rng.CopyFromRecordset rst, rst.RecordCount
End Sub 

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close