Using VB code or Macro to read records from one form and transfer to another

Posted on 2009-02-14
Last Modified: 2013-11-27

From the subform (see image 1 circled), I would like to write VB code on command button that reads and stores all job numbers.  Once stored (e.g. one dimensional array), I would like then to append them all in comma delimited format and write them to the text box on another form (see Image 2 circled). I have manage quite a bit and know how to read first job number, but then struggle to code to move on the another record in the subform and read 2nd, 3rd etc job number.  Please help
Question by:LillyC
    LVL 84
    It's difficult to work with individual rows in a subform; you instead would work with the subform's Recordset. If you're doing this from the Parent form, you'd do something like this:

    Dim rst As ADODB.Recordset
    Dim sID as String

    Set rst = New ADODB.REcordset

    rst.Open "SELECT * FROM YourSubformTable WHERE YourForeignKeyField=" & Me.YourIDField, CurrentProject.Connection

    Do Until rst.EOF
      sID = rst("YourJobNumField") & ", " & sID

    If Len(sID) > 0 Then
      '/trim off the trailing ,
      Me.YourOtherFormField = Right(sID, Len(sID)-1)
    End If

    Obviously you'd have to change the TAble and Field names to match your own. Note that the YourForeignKeyField and YourIdField would be those defined in the Child and Master link fields of your subform (respectively). YourSubformTable would be the Recordsource of the form you're using as a subform.

    Author Comment


    Many thanks for that.  The solution seems a lot closer now.  It just that (whether is a fact that I use Microsoft Access 2007 or not), it would not let me specify Dim rst As ADODB.Recordset.

    It does let me specify Dim rst As DAO.Recordset or just Dim rst as Recordset.

    Also, a bit confused with some of the syntax, so to help I have following (please try to send me the code using below):

    Master Form Name is Small Works Form
    Subform name is Small Works Datasheet
    Subform Table name is Small Works Table
    and the
    Field Name is Job Number, same both in subform and the table.

    Also, I am assuming below then needs to change in terms of coding, maybe using OpenRecordset, rather than open. I have tried following, but did not work.  PLEASE HELP!

    rst.Open "SELECT * FROM Small Works Table WHERE Job Number=" & Forms![Small Works Form]![Small Works Datasheet].Form.[Job Number], CurrentProject.Connection

    Do Until rst.EOF
      sID = rst("Job Number") & ", " & sID

    P.S.  By the way, below works, giving me headings of the actual subform table (but that is obviously what I do not need).  It prints them correct, however, in immediate window.  Just in a case that helps you solve the above.

    Dim rst As DAO.Recordset
        Dim fld As Field

        Set rst = Forms![Small Works Form]![Small Works Datasheet].Form.Recordset
        For Each fld In rst.Fields
            'Print field names.
         Debug.Print fld.Name

    LVL 84

    Accepted Solution

    In the Subform Control properties, what are the MasterLinkField and ChildLinkField?


    Author Closing Comment

    Hi Expert,

    I have made it work, WICKED!  But it would not have happened without your help!  See the code below that works.

    P.S.  The only thing that it does not work is truncating the last comma in sID string, when I use len-1, it for some reason does not take comma at the end away, but the first number from the left.  Anyhow, that is not important, but if you do know why please let me know.  MANY THANKS!  Please reply and we will automatically accept your ssolution and give maximum points, that are WELL DESERVED!

    'set recordset to small works subform
    Set rst = Forms![Small Works Form]![Small Works Datasheet].Form.Recordset
    'read job numbers until end of fields and write them to sID string appending with coma separation
    Do Until rst.EOF
      sID = rst("Job Number") & ", " & sID

    'print in immediate window for check
    'Debug.Print sID

    If Len(sID) > 0 Then
      '/trim off the trailing if there is any ,
    Forms![Job Number Allocation Form].[Additional Small Works Job No's].Value = Right(sID, Len(sID))
    End If


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
    In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 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

    7 Experts available now in Live!

    Get 1:1 Help Now