Results 1 to 6 of 6
  1. #1
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111

    Matching form data entry boxes to record set names via loop

    I have a form with several data entry boxes (text, combo, check boxes, and option groups). The name of the data entry boxes match the field names of the corresponding table that the data will be appended to/ drawn from.



    When the user selects a value from a combo box, a sql is executed that gathers the data meeting the criteria and auto populates a bunch of fields in the form. My real form has many fields in it and I will be running similar functions throughout (e.g. writing data to table), I would like to create a loop to perform the field matching instead of listing each form data entry box equaling its corresponding record source.

    Using this test example, how can I alter my script to a loop instead:
    Code:
    Private Sub txt_ID_AfterUpdate()
    
        Dim db As Database
        Dim rs As DAO.Recordset
        Dim strSQL As String
    
            strSQL = "SELECT Table1.ID, Table1.Color, Table1.Make, Table1.Model, Table2.FName, Table2.LName FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.ID WHERE"
            strSQL = strSQL & "[Table1].ID = """ & Me.txt_ID & """"
            
            Set rs = Currentdb.OpenRecordset(strSQL, DB_OPEN_DYNASET)
            
            Me!ID = rs!ID
            Me!Color = rs!Color
            Me!Make = rs!Make
            Me!Model = rs!Model
            Me!FName = rs!FName
            Me!LName = rs!LName
    
            rs.Close
            Set rs = Nothing
    End Sub
    I want it to do something like this:

    Code:
    Private Sub txt_ID_AfterUpdate()
    
        Dim db As Database
        Dim rs As DAO.Recordset
        Dim strSQL As String
    
            strSQL = "SELECT Table1.ID, Table1.Color, Table1.Make, Table1.Model, Table2.FName, Table2.LName FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.ID WHERE"
            strSQL = strSQL & "[Table1].ID = """ & Me.txt_ID & """"
            
            Set rs = Currentdb.OpenRecordset(strSQL, DB_OPEN_DYNASET)
    
            For each FieldName in FormA
                Me!<FieldName> = rs!<FieldName>
            Next FieldName
    
            rs.Close
            Set rs = Nothing
    End Sub
    Many thanks in advance.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Is ID an autonumber type field?

    Why change the code if it already works?

    Those 6 fields are they only ones in the form RecordSource?

    Why duplicating data?
    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
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111
    This is an example only. My real database is much more complex with many more fields to loop through than is shown in example. ID is not an autonumber. Duplication of data is required because data entry person does not have direct access to master tables. I would appreciate an answer if for no other reason than curiosity at how to make the coding more concise.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Your SQL needs a space after the WHERE keyword. The ID field is a text data type?

    The SQL is joining the two tables and both tables have the same field names. Might be an issue with this concept because of the table name qualifier. That becomes part of the fieldname and there is no equivalence to the form field names, unless the form RecordSource also joins the tables.

    Maybe easiest to loop through the recordset fields instead of looping through the form fields.

    If Not rs.EOF Then
    For i = 0 To rs.Fields.Count - 1
    Me.Recordset.Fields(rs.Fields(i).Name) = rs.Fields(i)
    Next
    End If

    Or if bound textboxes have same name as the field:

    Me.Controls(rs.Fields(i).Name) = rs.Fields(i)
    Last edited by June7; 05-13-2014 at 10:49 AM.
    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
    newbieX is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Jul 2013
    Posts
    111

    Final Code

    This code that you provided worked but needed to add period to rsFields(i)
    Code:
    If Not rs.EOF Then
        For i = 0 To rs.Fields.Count - 1
           Me.Controls(rs.Fields(i).Name) = rs.Fields(i)
        Next
    End If
    rs.Close
    Set rs = Nothing
    I finally settled on this code. Thanks for pointing me in the right direction.

    Code:
    rs.MoveFirst
    For Each fld In rs.Fields
        Me.Controls(fld.Name) = fld
    Next fld

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Sorry for typo. I edited the post.

    Glad you figured it out.
    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.

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

Similar Threads

  1. Replies: 7
    Last Post: 11-02-2012, 12:05 PM
  2. Replies: 5
    Last Post: 08-15-2012, 06:01 PM
  3. Replies: 1
    Last Post: 09-28-2011, 01:17 PM
  4. Replies: 2
    Last Post: 08-02-2011, 07:25 AM
  5. Replies: 5
    Last Post: 04-24-2011, 03:14 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