Solved

Return Select query result to string variable in Access 2007

Posted on 2011-02-15
5
1,689 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now