Solved

Return Select query result to string variable in Access 2007

Posted on 2011-02-15
5
1,711 Views
Last Modified: 2012-05-11
I need to be able to run a SELECT query and return all values it finds to a string variable with the results seperated by commas using VBA. Can anyone help?
0
Comment
Question by:JP_TechGroup
5 Comments
 
LVL 51

Accepted Solution

by:
HainKurt earned 250 total points
ID: 34901471
get the result as is from db and concatenate in the app

rs = cmd.open(sql)
whil not rs.eof
  if s="" then s = s & ", "& rs(0)
  if s<>"" then s = s & ", "& rs(0)
wend
0
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 250 total points
ID: 34904751
I would use GetRows.
From the on-line help:
Sub RækkerMatrix()
  Dim dbs As Database, rst As Recordset, strSQL As String
  Dim varRecords As Variant, intI As Integer, intJ As Integer

  Set dbs = CurrentDb
  strSQL = "SELECT * FROM tblYourTable"
  Set rst = dbs.OpenRecordset(strSQL)
  rst.MoveLast
  rst.MoveFirst
  varRecords = rst.GetRows(rst.RecordCount)
  For intI = 0 To UBound(varRecords, 2)
    Debug.Print
    For intJ = 0 To UBound(varRecords, 1)
      Debug.Print varRecords(intJ, intI)
    Next intJ
  Next intI
  rst.Close
  Set dbs = Nothing
End Sub

Open in new window


Adjust the line
      Debug.Print varRecords(intJ, intI)
to fit your need.

/gustav
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 34906061
0
 

Author Closing Comment

by:JP_TechGroup
ID: 34908329
Used data gathering from one and string handling for the other. Thanks guys.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 34908404
You are welcome!

/gustav
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question