Results 1 to 5 of 5
  1. #1
    rkalapura is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    New York
    Posts
    69

    Concatenate Rows into one row

    I am using the following code (function "EmailGps()") on the record source property of an unbound field to get the email addresses in access 2010. Email address are listing by the query "QrySupvTLPDue". This query fetch me all people I need to send the email automatically. This code makes all email addresses in one line separated by semicolon. This part is working fine and has no problem for the first report.

    Public Function EmailGps()
    Dim Action As DAO.Database
    Dim Rs As DAO.Recordset
    Dim eAdr As String
    Set Rs = CurrentDb.OpenRecordset("QrySupvTLPDue", dbOpenForwardOnly)
    With Rs
    If Not Rs.EOF And Not Rs.BOF Then
    Do Until Rs.EOF
    If Rs![EmailAd] <> "" Then
    eAdr = eAdr & Rs!EmailAd & ";"
    End If
    Rs.MoveNext


    Loop
    End If
    End With
    Rs.Close
    Set Rs = Nothing
    eAdr = left(eAdr, Len(eAdr) - 1)
    EmailGps = eAdr
    End Function

    Now I have a second report which I need to email the same way. So I have a second query which fetch the list of email address of people involved. I made a second function called Egroup() with same code and only changed the name of the query from "QrySupvTLPDue" to "QryEGroup". When I assign this function to an unbound field, it gives error. Both queries have only one filed for email address and sorted ascending. I tried with many other queries with one field, none of them working other than the first.

    The error I am getting is "Runtime error 3061". Too few parameters. Expected 1.

    I am not an expert in changing code but can understand. Can anybody tell what I am missing here for using the same code more than one time in a database?

    Appreciate your help.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    parameters expect means you either have a parameter inside 1 of the queries that is not answered, or a field is misspelled and the query THINKS its a param.

    Instead of cycling thru collecting ALL emails to send once, I scan each email once and send X number of emails. But either way works I guess until you hit the text limit of the SENDTO line.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Also, you should wrap any sample code in "code tags".
    Highlight the vba code involved then click on the # sign in the header of the response window.

    I suggest you look for the free utility called SmartIndenter which will help you format your code for improved readability.

    After SmartIndenter

    Code:
    Public Function EmailGps()
        Dim Action As DAO.Database
        Dim Rs As DAO.Recordset
        Dim eAdr As String
        Set Rs = CurrentDb.OpenRecordset("QrySupvTLPDue", dbOpenForwardOnly)
        With Rs
            If Not Rs.EOF And Not Rs.BOF Then
                Do Until Rs.EOF
                    If Rs![EmailAd] <> "" Then
                        eAdr = eAdr & Rs!EmailAd & ";"
                    End If
                    Rs.MoveNext
                Loop
            End If
        End With
        Rs.Close
        Set Rs = Nothing
        eAdr = Left(eAdr, Len(eAdr) - 1)
        EmailGps = eAdr
    End Function

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    "QryEGroup" opens without error?

    All of the fields in queries "QrySupvTLPDue" and "QryEGroup" are the same?

    In the function Egroup(), did you change the 2nd from the last line to "Egroup = eAdr"?

    You don't need the lines
    Code:
    Dim Action As DAO.Database  (not used)
    
    With Rs    (you are explicitly using "Rs" for the recordset variables)
    End With

    -----------------------------
    If the queries have the same fields, you could use one function by turning it into a parameter function:
    Code:
    Public Function EmailGps(pQryName As String) As String
    
        Dim Rs As DAO.Recordset
        Dim eAdr As String
        
        Set Rs = CurrentDb.OpenRecordset(pQryName, dbOpenForwardOnly)
    
        If Not Rs.EOF And Not Rs.BOF Then
            Do Until Rs.EOF
                If Rs![EmailAd] <> "" Then
                    eAdr = eAdr & Rs!EmailAd & ";"
                End If
                Rs.MoveNext
            Loop
        End If
    
        Rs.Close
        Set Rs = Nothing
        eAdr = Left(eAdr, Len(eAdr) - 1)
        EmailGps = eAdr
    End Function
    Usage would be:

    Code:
    SomeVariable = EmailGps("QrySupvTLPDue")
    
    AnotherVariable = EmailGps("QryEGroup")

  5. #5
    rkalapura is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    New York
    Posts
    69

    Concatenate Rows to Row

    Thank you for everybody who responded and assisted for solving my problem. I have sorted this out using some Macro sequence actions. Now I found out a way to use this function any number of time in the database. Following is the Macro sequence I used:

    1. Instead of using query in the code, I used a temporary table of one filed (for email address). I changed the previous queries to "append query" which will append rows to the temporary table used in code.
    2. Each time after running the code and using the code result, I delete all records from the table with a delete query. So next time when the code run, it append records to the empty table. The code result is stored in unbound filed in a form. So every time I open the form, I get a fresh email address list which I need.

    I have almost 12 different reports in my database which emailed to different recipient. When I run a report in different time, the number and address of recipient are different. It is very difficult to enter the email address manually each time looking in to the report. Now all of them are working fine with my Macro function.

    Thank you very much everybody who sent valuable thoughts for solving my issue. God Bless all.



    Quote Originally Posted by ssanfu View Post
    "QryEGroup" opens without error?

    All of the fields in queries "QrySupvTLPDue" and "QryEGroup" are the same?

    In the function Egroup(), did you change the 2nd from the last line to "Egroup = eAdr"?

    You don't need the lines
    Code:
    Dim Action As DAO.Database  (not used)
    
    With Rs    (you are explicitly using "Rs" for the recordset variables)
    End With

    -----------------------------
    If the queries have the same fields, you could use one function by turning it into a parameter function:
    Code:
    Public Function EmailGps(pQryName As String) As String
    
        Dim Rs As DAO.Recordset
        Dim eAdr As String
        
        Set Rs = CurrentDb.OpenRecordset(pQryName, dbOpenForwardOnly)
    
        If Not Rs.EOF And Not Rs.BOF Then
            Do Until Rs.EOF
                If Rs![EmailAd] <> "" Then
                    eAdr = eAdr & Rs!EmailAd & ";"
                End If
                Rs.MoveNext
            Loop
        End If
    
        Rs.Close
        Set Rs = Nothing
        eAdr = Left(eAdr, Len(eAdr) - 1)
        EmailGps = eAdr
    End Function
    Usage would be:

    Code:
    SomeVariable = EmailGps("QrySupvTLPDue")
    
    AnotherVariable = EmailGps("QryEGroup")

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Concatenate 3 rows into single header row
    By kestefon in forum Access
    Replies: 13
    Last Post: 01-31-2014, 06:10 PM
  2. merge or concatenate two rows in one
    By vojinb in forum Queries
    Replies: 7
    Last Post: 08-03-2011, 09:15 AM
  3. Help needed - Trying to concatenate rows
    By clarkian11 in forum Queries
    Replies: 15
    Last Post: 06-09-2011, 10:11 AM
  4. Replies: 6
    Last Post: 04-07-2011, 12:33 PM
  5. Concatenate Multiple Rows into One
    By Knarly555 in forum Queries
    Replies: 5
    Last Post: 11-12-2010, 06:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums