Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

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

Hi,

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
form1jpeg.JPG
form2jpeg.JPG
0
LillyC
Asked:
LillyC
  • 2
  • 2
1 Solution
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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
  rst.MoveNext  
Loop

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.
0
 
LillyCAuthor Commented:
Hi,

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
  rst.MoveNext  
Loop

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
    Next



0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
In the Subform Control properties, what are the MasterLinkField and ChildLinkField?

0
 
LillyCAuthor Commented:
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
  rst.MoveNext
Loop

'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

0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now