Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8675207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:53:43+00:00 2026-06-12T19:53:43+00:00

OK so I have this function which queries a database using a query string

  • 0

OK so I have this function which queries a database using a query string that I pass to it.
At the moment it outputs the query result in the worksheet.
How do I just get the function to give me the result as a range that I can use in VBA to perform calculations etc? How would I then reference this range? E.g. to get the “Name” column in the result.

Function Access_Data(query As String)
 'Requires reference to Microsoft ActiveX Data Objects xx Library

Dim Cn As ADODB.Connection, Rs As ADODB.Recordset
Dim MyConn, sSQL As String

Dim Rw As Long, Col As Long, c As Long
Dim MyField, Location As Range

 'Set destination
Set Location = Sheets(1).Range("a1")
 'Set source
MyConn = "S:\Docs\Harry\Engine Client\Engine3.accdb"
 'Create query
sSQL = query

 'Create RecordSet
Set Cn = New ADODB.Connection
With Cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .Open MyConn
    Set Rs = .Execute(sSQL)
End With

 'Write RecordSet to results area
Rw = Location.Row
Col = Location.Column
c = Col
Do Until Rs.EOF
    For Each MyField In Rs.Fields
        Cells(Rw, c) = MyField
        c = c + 1
    Next MyField
    Rs.MoveNext
    Rw = Rw + 1
    c = Col
Loop
Set Location = Nothing
Set Cn = Nothing

End Function

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:53:44+00:00Added an answer on June 12, 2026 at 7:53 pm
    Function Access_Data(query As String)
     'Requires reference to Microsoft ActiveX Data Objects xx Library
    
    Dim Cn As ADODB.Connection, Rs As ADODB.Recordset
    Dim MyConn, sSQL As String
    
    Dim Rw As Long, c As Long
    Dim MyField, Result
    
     'Set source
    MyConn = "S:\Docs\Harry\Engine Client\Engine3.accdb"
     'Create query
    sSQL = query
    
     'Create RecordSet
    Set Cn = New ADODB.Connection
    With Cn
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .CursorLocation = adUseClient
        .Open MyConn
        Set Rs = .Execute(sSQL)
    End With
    
     'Write RecordSet to results
    Redim Result(1 To Rs.RecordCount, 1 To Rs.Fields.Count)
    Rw = 1
    Do Until Rs.EOF
        c = 1
        For Each MyField In Rs.Fields
            Result(Rw, c) = MyField
            c = c + 1
        Next MyField
        Rs.MoveNext
        Rw = Rw + 1
    Loop
    Set Cn = Nothing
    
    Access_Data = Result
    End Function
    

    This will return a multi-dimension array. A range must refer to some portion of a worksheet: you cannot create an “invisible” range. (Though you can make a portion of a worksheet invisible, if that’s what you’re after.)

    To access the results:

    Dim v, i As Long
    v = Access_Data("select ID, Name from somewhere")
    For i = 1 To UBound(v, 1)
        MsgBox v(i, 1) & " / " & v(i, 2)
    Next
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function which returns a datatype InetAddress[] public InetAddress [] lookupAllHostAddr(String host)
Hi have made this function which is made to replicate an error that I
I have a function which calculates distance from database records. This function is called
I'm using PDO inside a database abstraction library function query that I've made. I'm
I have a search function that queries the database and has ~15 optional parameters.
I have this function which is supposed to set a certain time format to
I have this function which pings my server for a specific change in data:
I have this function which gives me ( for a current date ) -
We have this anonymous function in our code, which is part of the jQuery's
I have a function (function1) which requires sta mode. I'd like tocall this function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.