Results 1 to 1 of 1
  1. #1
    Join Date
    May 2003
    Location
    Dallas
    Posts
    817
    Provided Answers: 5

    Unanswered: Code to fill an unbound form and code to write the data back This is good stuff

    A while ago I asked if there was a way to fill the controls on a an unbound form from a recordset if the control names matched the field names - IZZY got me started with the filling in the controls and I finished with the return code to write the data back.

    The beauty of these two procedures is that if you are using unbound forms - all you have to do to fill in the data and write the data back is name the controls the same as the fields.

    This will eliminate alot of lines of code of
    me!control = rst!fieldname

    so here you guys go - this has been a blessing for me.

    Public Sub FillUnboundForm()
    On error resume next
    Dim db As Database
    Dim rst As Recordset
    Dim rfld As DAO.Field
    On Error Resume Next
    Set db = CurrentDb
    Set rst = db.OpenRecordset("select * from mytable", dbOpenDynaset, dbSeeChanges)

    With rst
    If .EOF Then
    Else
    For Each rfld In rst.Fields
    Me(rfld.Name) = rfld
    Next
    End If
    End With

    End Sub

    Public Sub saveUnboundForm()
    On error resume next
    Dim db As Database
    Dim rst As Recordset
    Dim rfld As DAO.Field, myfield
    On Error Resume Next
    Set db = CurrentDb
    Set rst = db.OpenRecordset("select * from mytable",dbOpenDynaset, dbSeeChanges)

    With rst
    If .EOF Then
    Else
    For Each rfld In rst.Fields
    rst.Edit
    rfld = Me(rfld.Name)
    rst(Me(rfld.Name)) = Me(rfld.Name).Value
    rst.update
    Next
    End If
    End With

    End Sub
    Last edited by axsprog; 08-15-08 at 14:34.
    Dale Houston, TX

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •