inkserious
New Member
Taking from the current VBA classes, I have created a very cool little UserForm to input data into a structured table. However, I've run into a problem in everyday use: the form has no code to check for a duplicate entry. There are three different fields than need to be verified to insure there is not a duplicate: date, shift and time. One solution I came up with was to add another column and concatenate the three fields together. Then I could use a countif statement in the code to see if a record already exits. The problem with that is I want to allow the user the option to overwrite the existing record. So, if a duplicate record is found, MsgBox("Duplicate Entry Found." & Chr(10) & "Do you want to overwrite?", vbQuestion + vbYesNo, "Duplicate Found") Here is my existing code:
[pre]
[/pre]
[pre]
Code:
Private Sub cmdSubmit_Click()
Dim lrowCount As Long
Dim ctl As Control
Dim ws As Worksheet
Set ws = Worksheets("testRundown")
' Write data to worksheet
lrowCount = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ws.Cells(lrowCount, 1).Value = Me.cboDate.Value
ws.Cells(lrowCount, 2).Value = Me.cboShift.Value
ws.Cells(lrowCount, 3).Value = Me.cboTime.Value
ws.Cells(lrowCount, 4).Value = Me.txtDrop.Value
ws.Cells(lrowCount, 5).Value = Me.txtWin.Value
ws.Cells(lrowCount, 6).Value = Format(Now(), "mm/dd/yy hh:mm")
' Clear the form
For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then
ctl.Value = ""
ElseIf TypeName(ctl) = "CheckBox" Then
ctl.Value = False
End If
Next ctl
Call cmdClose_Click
End Sub