VB6Streamline Adding New Records
Option Explicit

Private NewEmployee_ID As String

Public Function AddNew() As String
   Dim Ctl As Control

   NewEmployee_ID = "
   
   ' create a new record and set field defaults
   deMain.rsEmployee.AddNew
   deMain.rsEmployee("Pub_ID").Value = "9901"
   deMain.rsEmployee("Hire_Date").Value = Now()
   If deMain.rsJobs.State <> adStateOpen Then _
      deMain.rsJobs.Open
   deMain.rsEmployee("Job_ID").Value = _
      deMain.rsJobs("Job_ID").Value
      
   ' set max textbox chars using db schema
   For Each Ctl In Me.Controls
      If TypeOf Ctl Is TextBox Then
         If Ctl.DataField <> " Then
            If deMain.rsEmployee _
               (Ctl.DataField).Type = adVarChar Or _
               deMain.rsEmployee _
               (Ctl.DataField).Type = adChar Then
               Ctl.MaxLength = deMain.rsEmployee _
               (Ctl.DataField).DefinedSize
            End If
         End If
      End If
   Next
   
   ' display new authors form modally
   Me.Show vbModal

   AddNew = NewEmployee_ID
End Function

Private Sub cmdCancel_Click()
   Unload Me
End Sub

Private Sub cmdOK_Click()
   On Error GoTo LocalError
      
   ' save changes (.update bug workaround)
   deMain.rsEmployee("Emp_ID").Value = _
      deMain.GetEmp_ID
   NewEmployee_ID = deMain.rsEmployee("Emp_ID").Value
   deMain.rsEmployee.Move (0)
   
   Unload Me

LocalExit:
   Exit Sub
LocalError:
   NewEmployee_ID = "
   MsgBox Err.Description, vbExclamation + vbOKOnly
   Resume LocalExit
End Sub

Private Sub Form_Unload(Cancel As Integer)
   If NewEmployee_ID = " Then _
      deMain.rsEmployee.CancelUpdate
End Sub



Private Sub txtfname_KeyUp(KeyCode As Integer, Shift _
   As Integer)
   Call ValidateRecord
End Sub

Private Sub txtjob_lvl_KeyPress(KeyAscii As Integer)

   ' Allow only delete or numerical characters
   If InStr("0123456789" & Chr$(8), Chr(KeyAscii)) _
      = 0 Then
      KeyAscii = 0
   End If

End Sub

Private Sub txtjob_lvl_Validate(Cancel As Boolean)

   ' allow only numbers <= 255
   If Val(txtjob_lvl.Text) > 255 Then
      txtjob_lvl.Text = 255
      Cancel = False
   End If
  
End Sub

Private Sub ValidateRecord()
   
   ' enable ok if all req fields present
   If Len(Trim(txtfname.Text)) > 0 And _
      Len(Trim(txtlname.Text)) > 0 Then
      cmdOK.Enabled = True
   Else
      cmdOK.Enabled = False
   End If


End Sub

Private Sub txtlname_KeyUp(KeyCode As Integer, Shift _
   As Integer)
   Call ValidateRecord
End Sub

Listing A A public "AddNew" method and a range of data validation techniques helps streamline the addition of new records.