Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    lbaotf is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2013
    Posts
    15

    Post Joining Rows in a Form from a query

    I am trying to join records in a single form or query based on a value from a field in a record. ie.



    I have records that have a unique value field (Rating) 1-1000 the last number can vary depending on how many records are entered and will be a different amount of records everytime I try generating the form or query.

    I need to be able to take the first record and the last record and combine selected fields, then the second record and the next to last record, then the third record and the third record from the last record.........and so on until the records meet in the middle.

    Anyone have any Ideas?

    Thank You

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    And what happens if there is an odd number of records?

    Seems like I've seen this before. I don't know if any combination of queries alone could accomplish this. I think VBA and a temp table will be needed.
    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.

  3. #3
    lbaotf is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2013
    Posts
    15
    There will never be an odd number and if there was would just need an error message to let user know they needed to add another record...

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Do you know anything about VBA and opening recordsets and writing records to table? Because that's what I think will be needed.
    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.

  5. #5
    lbaotf is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2013
    Posts
    15
    NO Not Sure but I will study that just a little bit It seems to be a simple solution but maybe it is more diff. than I think.

    I am Basicly Sort Asending 1 Column and then if I have 10 records, selecting first then Last together, then second and nest to last and so on together.

    If you could give me a little example it might help I would appreciate it.

    Thank you so much

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Here is a simple procedure.
    Code:
    Option Compare Database
    Option Explicit
    
    Sub MatchRecs()
    Dim rs1 As DAO.Recordset
    Dim rs2 As DAO.Recordset
    Dim i As Integer
    Set rs1 = CurrentDb.OpenRecordset("SELECT * FROM Table1 ORDER BY RecNum ASC;", dbOpenSnapshot)
    Set rs2 = CurrentDb.OpenRecordset("SELECT * FROM Table1 ORDER BY RecNum DESC;", dbOpenSnapshot)
    CurrentDb.Execute "DELETE FROM TempTable"
    If rs1.RecordCount > 0 Then
        If rs1.RecordCount Mod 2 <> 0 Then
            MsgBox "Odd number of records. Procedure canceled."
        Else
            For i = 1 To rs1.RecordCount / 2
                CurrentDb.Execute "INSERT INTO TempTable(RecNum1, RecNum2, Data1, Data2) VALUES(" & _
                rs1!RecNum & ", " & rs2!RecNum & ", '" & rs1!Data & "', '" & rs2!Data & "')"
                rs1.MoveNext
                rs2.MoveNext
            Next
        End If
    End If
    End Sub
    I created a table called Table1 with two fields - RecNum and Data. I created another table called TempTable to hold the manipulated records with 4 fields - RecNum1, RecNum2, Data1, Data2. I put the code in a general module and manually ran it with the Run command button on the VBA editor menu. You could put everything between Sub MatchRecs() and End Sub in a button Click event behind a form.

    Change names in the code as appropriate.
    Last edited by June7; 11-20-2013 at 06:34 PM.
    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.

  7. #7
    lbaotf is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2013
    Posts
    15
    OK I understand a little of that....I am going to give it a try now...
    Thank you I will keep you posted.... You are great

  8. #8
    lbaotf is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2013
    Posts
    15
    Can the table contain an autonumber?


    Quote Originally Posted by June7 View Post
    I created a table called Table1 with two fields - RecNum and Data. I created another table called TempTable to hold the manipulated records with 4 fields - RecNum1, RecNum2, Data1, Data2. I put the code in a general module and manually ran it with the Run command button on the VBA editor menu. You could put everything between Sub MatchRecs() and End Sub in a button Click event behind a form.

    Change names in the code as appropriate.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Which table? Table1? Don't see why not.
    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.

  10. #10
    lbaotf is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2013
    Posts
    15
    I Have put the code in the general module but I cant get it to run with a command. maybe im not commanding it properly.

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Click cursor anywhere within the procedure then click the Run button (green triangle) from VBA editor or F5 or click Run menu item.

    Review link at bottom of my post for debugging guidelines.
    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.

  12. #12
    lbaotf is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2013
    Posts
    15
    HI June7
    Thank you so much for your assistance. I have the test working I am moving forward to implement it into my current Data...

    I want to thank you for your time.

    Can this only be done at the table level? or can it be accomplished with queries or forms?

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    That was the driving force behind the VBA and temp table solution. Don't think queries can do this. Really don't know what you mean by accomplishing with form.
    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.

  14. #14
    lbaotf is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2013
    Posts
    15
    Well what I have is a Database built with a table called Riders, with fields listed RidersID, FirstName, LastName, Rank, Rating, HorseName, Class,

    I have generated forms from that table to enter Data in each field, I have Queries, and Macros pulling data by Class, Rating, Horsename, Combining FirstName and LastName together. Then I am using a random expr to randomly Sort the records into queries that provides the exact data needed by the RidersID based on the Class and Rating.

    The Reason for the combine of records in the order with Last Record and First Record and so on, is that after these records have been randomly sorted is when I need to combine the records from last and first and so on. Ultimatley two of the Riders need to be puuled together into one record after all the sorting and then printed on a report.

    Hope I explained it well enough...

    Again thank You so much for the progress I am making...

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You can have a 'menu' form with button: "Match Riders and Print Report"
    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.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Joining 2-Tables in a query
    By djclntn in forum Queries
    Replies: 7
    Last Post: 03-15-2012, 08:37 AM
  2. Replies: 0
    Last Post: 02-09-2012, 05:43 PM
  3. Joining 2-Tables in a Query
    By djclntn in forum Queries
    Replies: 25
    Last Post: 01-13-2012, 12:54 PM
  4. Joining 2 fields in query to make one
    By robsworld78 in forum Queries
    Replies: 5
    Last Post: 07-11-2011, 12:06 AM
  5. Joining a Query
    By access_gbr in forum Queries
    Replies: 1
    Last Post: 05-04-2010, 10:46 AM

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