troubleshooting Question

Update Table from Unbound Form - Problem with For Loop sequence

Avatar of Karen Schaefer
Karen SchaeferFlag for United States of America asked on
Microsoft Access
8 Comments1 Solution505 ViewsLast Modified:
I am using a function to update a table based on an unbound form's fields.  Problem is that it is also passing the label controls. Determine control types and bypass all but the actual fields that contain data

Also I am not sure my loop is working correctly,  This is my attempt to prevent the labels as be passed as a control.  Not work right though.  See '>>>>>>>>>>

Thanks,

K
Function UpdateTables(strMain As String, strTable As String)
    On Error GoTo UpdateTables_Error
    Dim rs As DAO.Recordset
    Dim F As Form
    Dim c As Control
    Dim i As Long
    Dim strSQL As String
    Dim strFormField As String
    'Identify the form/subform you're working with
    Set F = Forms(strMain).Form
    ' Open the table to add new records
    strSQL = strTable
    Set rs = Currentdb.OpenRecordset(strSQL)
        rs.AddNew
        For i = 0 To rs.Fields.Count - 1
            For Each c In F
                strFormField = c.Name
                Debug.Print strFormField
   '>>>>>     If rs.Fields(i).Type <> dbText Or rs.Fields(i).Type <> dbMemo Or rs.Fields(i).Type <> dbInteger Or rs.Fields(i).Type <> dbDate Then
                    GoTo ResumeNext
                ElseIf rs.Fields(i).Name = strFormField Then
                    rs.Fields(i).Value = Nz(F(strFormField).Value)
                End If
ResumeNext:
            Next c
        Next i
        rs.Update
    rs.Close
    Set rs = Nothing
    'Now clean up the form so the user can add a new record if he wants
    ClearFormData strMain
    
 
UpdateTables_Error:
    Select Case Err
        Case 0
            Exit Function
        Case Else
            MsgBox "Subroutine Functions" & ".UpdateTables." & vbCrLf & vbCrLf & "You have error number " & Err & ".  " & Err.Description
    End Select
End Function
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros