Link to home
Start Free TrialLog in
Avatar of Karen Schaefer
Karen SchaeferFlag for United States of America

asked on

Checking for required fields, and updating unbound form

I have an unbound form that opens up in add mode with certain required fields.  I need to check if the fields are null and if so have the system return the user back to the null field(s).  

Once all required fields contain data I need to update the data into the table.  This form opens in add mode.  I have tried using the bound recordsource but that wants to create a record that would later need to be deleted if the user does not complete the processing of the form.  So I am trying to use an unbound form without using a temp table..

I need to create a form that validates that all required fields contain data, then update the table(recordsource) with the data from the unbound form.  Am I on the right track?

Thanks,

Karen
Private Sub cmdApproval_Click()
 
   On Error GoTo cmdApproval_Click_Error
    For Each ctl In Me.Controls
        If ctl.Tag = "C" Then
            If IsNull(Me.ActiveControl) Then
                Call MsgBox("There is Data Missing.", vbExclamation Or vbDefaultButton1, "Warning")
                Me.ActiveControl.SetFocus
                Me.ActiveControl.BackColor = 128
                Me.ActiveControl.ForeColor = 16777215
                Exit Sub
            End If
        End If
    Next ctl
 
    Set rs = Currentdb.OpenRecordset("Select * from [Emergent Work]")
    If Not rs.EOF Then
       With rs
           .AddNew
           !Tech_Grp_Person = Me.Tech_Grp_Person
           !Task_Description = Me.Task_Description
           !Requested_by = Me.Requested_by
           !Work_Estimate = Me.Work_Estimate
           !Planned_Start = Me.Planned_Start
           !Planned_End = Me.Planned_End
           !ECD = Me.ECD
           !In_Support_of = Me.In_Support_of
           !Work_Authority = Me.Work_Authority
           !Status = 1
           .Update
       End With
    End If
        Set rs = Currentdb.OpenRecordset("Select Max(Seq_NO) as ID, Tech_Grp_Person from [Emergent Work]")
        Do Until rs.EOF
            gWR = rs.Fields("ID").Value
            Debug.Print rs.Fields("Tech_Grp_Person")
            gTP = rs.Fields("Tech_Grp_Person").Value
        Loop
    EmailMgmtApprReq
   
   On Error GoTo 0
   Exit Sub
 
cmdApproval_Click_Error:
 
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdApproval_Click of VBA Document Form_frmEmergentWork"
End Sub

Open in new window

Avatar of Emil_Gray
Emil_Gray
Flag of United States of America image

In each of the fields in which you require data in the On Lost Focus event put code returning focus to that field until the user complies and adds proper data to the field.

Me.nameoffield.SetFocus
I of course am not yet awake. The correct syntax for the On Lost Focus event is:

Example:

Private Sub Insurance_ID_LostFocus()
If IsNull(Insurance_ID) Then Insurance_ID.SetFocus
End Sub


Sorry 'bout that, I'll get a cup of coffee.
Avatar of Karen Schaefer

ASKER

Thanks, for the input, however, the validation is only 1 part I am looking for the best approach to updating a record in a table - whether to use bound or unbound form in this case?

Thanks,

K
ASKER CERTIFIED SOLUTION
Avatar of Karen Schaefer
Karen Schaefer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No objection from me. I'm glad Karen found her answer.

Have a great day Karen.