Passing a string to an expression in Access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • birdDBheadache
    New Member
    • Mar 2014
    • 16

    Passing a string to an expression in Access

    I am trying to take the results of a subroutine (which is the string strFields in the code below) and pass them to an expression in an Access query. Is this even possible or am I thinking about this the wrong way? Can I only use functions in an expression?

    Code:
    Sub Fieldnames() 
        Dim Rst As Recordset
        Dim strFields As String
        Dim db As Database
        Dim f As Field
        Dim qdfParmQry As QueryDef
            Set db = CurrentDb()
            Set qdfParmQry = db.QueryDefs("qry_1test")
            qdfParmQry("Forms!Occu_formatting!Species") = [Forms]![Occu_formatting]![Species]
            Set Rst = qdfParmQry.OpenRecordset()
       
        For Each f In Rst.Fields
          strFields = strFields & f.Name & ", "
        Next
        Rst.Close
    
    strFields = Left(strFields, Len(strFields) - 2)
    
    End Sub
  • GKJR
    New Member
    • Jan 2014
    • 108

    #2
    I've read that one of the distinctions between functions and subroutines is that only functions can be called from a query. Couldn't you just change this sub to a function?

    Comment

    • birdDBheadache
      New Member
      • Mar 2014
      • 16

      #3
      Ok that worked...now on to the next problem!

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32656

        #4
        Good answer. Only function procedures (code that returns values) can be called from SQL. Sub procedures may not even be invoked from SQL so even if you want code to execute and don't care about the returned value, you still need a function if called from SQL.

        NB. Queries are written in SQL.

        Comment

        Working...