Results 1 to 6 of 6
  1. #1
    JonoGee is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2022
    Posts
    27

    Concetenate a field value with the same field in different rows

    Howdy folks! Good evening/Goodmorning...


    So i am trying to concatenate a field that appears in X number of rows...
    I came across this sample dB attached Database1.mdb

    Basically when you run the query it concatenates all the "Markets" Entries into one field...

    When I have tried to adopt this method in my dB I am getting allsorts of weird and wonderful runtime errors galore and I can't see an obvious mistake on my part and I was hoping for an eagle eye to call me a muppet and tell me to change this to that.....

    Click image for larger version. 

Name:	ConcatImage1.jpg 
Views:	13 
Size:	105.7 KB 
ID:	49499

    [tblCustomFieldsSKU] = Table where all the fields are stored that require concatenating... The field is called [CustomField] This is on a subform called [tblEMGSKUMasterTable]

    [EMGSKUID] links both tables and is the driving force that determines which rows are concatenated. So all [EMGSKUID]=4722 records, concatenate the field called [CustomField]

    The Module code is as follows.... (and this is way over my head)

    Code:
    Option Compare DatabaseOption Explicit
    
    
    Function Concatenate(Gettbl As String, GetKey As String, KeyValue As String, FieldConct As String) As String
    Dim Rst As DAO.Recordset, MySql As String
    Concatenate = ""
    MySql = "SELECT * FROM " & Gettbl & " WHERE (((" & Gettbl & "." & KeyValue & ")='" & GetKey & "'));"
    Set Rst = CurrentDb.OpenRecordset(MySql, dbOpenSnapshot)
    If Rst.RecordCount > 0 Then
    Rst.MoveLast
    Rst.MoveFirst
    Do While Not Rst.EOF
        If Nz(Concatenate, "") = "" Then
        Concatenate = Rst.Fields(FieldConct)
        Else
        Concatenate = Concatenate & ", " & Rst.Fields(FieldConct)
        End If
    Rst.MoveNext
    Loop
    End If
    Rst.Close
    Set Rst = Nothing
    End Function
    I hope this makes sense. If anyone has any ideas I would be glad to hear them, and/or explain anything i missed a bit clearer

    I attached the sample database again, so you can see how its supposed to work, but doesnt when applied to my database. I am getting a 3464 error....

    Click image for larger version. 

Name:	Error 3464.jpg 
Views:	12 
Size:	85.2 KB 
ID:	49500

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,906
    No idea as to what a 3464 error is.
    Try putting your sql string into a string variable.
    Then you can debug.print it until you get it correct, then use in the openrecordset command.
    If you still cannot see the error, you can post the output back here.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    tblCustomField referred to in the query is not included in the sample, nor is the table in your picture. The other query using the function runs fine.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    Provide the exact error message, not just the number.

    Prefixing field with table name is not necessary in this case. Parens are not needed. If table or field names include space or special characters or are same as reserved word, use [], use them anyway just to be safe. If key is autonumber/number, don't use apostrophe delimiters. Semi-colon is optional.

    MySql = "SELECT * FROM [" & Gettbl & "] WHERE [" & KeyValue & "]=" & GetKey

    Using the same field as input to two arguments does not make sense to me. Notice in the example db it uses two fields with same name but they are in different tables. This is why I advise not to use exact same name in multiple tables.

    Why not provide your db where this code fails?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,111
    Have a look at the modified sample attached; the original one assumed the GetKey was a string so in the function it was enclosed in single quotes. I copied the function and modified it to accept a numeric GetKey (new function Concatenate_Numeric). Try to use this one with the same arguments you had in the original.

    I have a similar function on my site (JoinFromArray @ http://forestbyte.com/vba-code-samples/) but lately I've been using theDbGuy's SimpleCSV():
    http://www.accessmvp.com/thedbguy/co...itle=simplecsv

    Just add the function to a standard module and in your case usage would be:
    Code:
    CustomFields:SimpleCSV("SELECT [CustomField] FROM 
    tblCustomFieldsSKU WHERE EMGSKUID=" & [EMGSKUID], ",")
    Cheers,
    Attached Files Attached Files
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Replies: 8
    Last Post: 08-16-2017, 10:53 AM
  2. Replies: 2
    Last Post: 04-12-2017, 01:40 PM
  3. Sum up fields in different rows to a new field in a query
    By Jan e Porsböll in forum Queries
    Replies: 1
    Last Post: 08-15-2014, 07:05 AM
  4. create rows from field
    By cm-net in forum Queries
    Replies: 1
    Last Post: 01-24-2011, 02:05 PM
  5. Inserting multiple rows using a value in a field
    By z1efuller1 in forum Queries
    Replies: 1
    Last Post: 01-06-2010, 11:20 AM

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