Results 1 to 2 of 2
  1. #1
    focosi is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    97

    writing control names on a form to a table

    I need to create a table with names of controls on my forms.
    I'll use that table to apply user rules.


    Any solution to write the table automatically using VBA code ?
    I know how to populate a combobox...

    DoCmd.OpenForm YourFormName, acNormal, , , , acHidden
    Dim c As Control
    For Each c In YourFormName.Controls
    [Control-Name].RowSource = [Control-Name].RowSource & c.Name & ";"
    Next c

    ..., but not how to write a table.
    Thanks!

  2. #2
    apr pillai's Avatar
    apr pillai is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2010
    Location
    Alappuzha, India
    Posts
    209
    Most probably this may be a one time operation and can be done manually without going for VBA.

    Since, you asked for it here is a sample routine:

    Code:
    Public Function SaveFldNames(ByVal frmName As String)
    Dim db As Database, ctrl As Control, sctn As Section
    Dim rst As Recordset, frmActive As Form
    Dim frmctrlName
    
    Set db = CurrentDb
    Set frmActive = Forms(frmName)
    Set sctn = frmActive.Section(acDetail)
    Set rst = db.OpenRecordset("Table1Fields") 'with 2 fields: ID, FieldNames
    
    For Each ctrl In sctn.Controls
       frmctrlName = ctrl.Name
       ctrlType = ctrl.Properties(3).Name
       If ctrlType = "ControlSource" Then
           rst.AddNew
           rst![FieldNames] = frmctrlName
           rst.Update
       End If
    Next
    rst.Close
    
    
    Set rst = Nothing
    Set db = Nothing
    Set frmActive = Nothing
    Set sctn = Nothing
    
    End Function
    Keep the Form open and call the above Function with the Form Name as Parameter, from a Command Button click.

    Before that create a table with the fields: ID, FIELDNAMES for writing out the field names from the Form.

    The controls on the Details Section of the Form only checked. You may have different type of controls on the Details Section of the Form like Labels. These are ignored and only controls with ControlSource property are written into the target table.

    I have used a Table with the name Table1Fields for writing out field names and ID field is added as an Autonumber field.

    If all the fields from the Record Source Table/Query are present on the Form then it is easier to open the TableDef of the SourceTable/Query, read and transfer the Field Names into the target table rather than scanning and finding required controls from the jungle of other controls on the form.

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

Similar Threads

  1. RowSource Value not writing to the Table
    By tbassngal in forum Queries
    Replies: 5
    Last Post: 09-03-2011, 12:16 PM
  2. Replies: 5
    Last Post: 04-24-2011, 03:14 AM
  3. Replies: 2
    Last Post: 06-20-2010, 06:54 PM
  4. Writing data from textbox to table
    By mll in forum Forms
    Replies: 4
    Last Post: 03-10-2010, 05:10 PM
  5. Automatically Writing SQL From A Table
    By smitstev in forum Programming
    Replies: 1
    Last Post: 06-05-2009, 09:38 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