Creating form that sends textbox data to database with a timestamp for only each textbox that contains data. The code I have writes the date to all rows regardless if there is data or not.
Have tried a few things, but cannot seem to make it work right. I'm sure there is a simple solution, but I am not seeing it.
Basic Code:
Private Sub EnterPrint_cmd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("SampleLog")
Dim ts As Date
ts = Now
'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
'check for Person
If Trim(Me.txtPerson.Value) = "" Then
Me.txtPerson.SetFocus
MsgBox "Please enter Person"
Exit Sub
End If
'check for a Sample Id
If Trim(Me.txtSample1.Value) = "" Then
Me.txtSample1.SetFocus
MsgBox "Please enter a Sample Id"
Exit Sub
End If
'copy the sample id to database
ws.Cells(iRow, 1).Value = Me.txtSample1.Value
ws.Cells(iRow + 1, 1).Value = Me.txtSample2.Value
ws.Cells(iRow + 2, 1).Value = Me.txtSample3.Value
ws.Cells(iRow + 3, 1).Value = Me.txtSample4.Value
ws.Cells(iRow + 4, 1).Value = Me.txtSample5.Value
ws.Cells(iRow + 5, 1).Value = Me.txtSample6.Value
ws.Cells(iRow + 6, 1).Value = Me.txtSample7.Value
ws.Cells(iRow + 7, 1).Value = Me.txtSample8.Value
ws.Cells(iRow + 8, 1).Value = Me.txtSample9.Value
ws.Cells(iRow + 9, 1).Value = Me.txtSample10.Value
ws.Cells(iRow + 10, 1).Value = Me.txtSample11.Value
ws.Cells(iRow + 11, 1).Value = Me.txtSample12.Value
ws.Cells(iRow + 12, 1).Value = Me.txtSample13.Value
ws.Cells(iRow + 13, 1).Value = Me.txtSample14.Value
'add date to database
ws.Cells(iRow, 2).Value = ts
ws.Cells(iRow + 1, 2).Value = ts
ws.Cells(iRow + 2, 2).Value = ts
ws.Cells(iRow + 3, 2).Value = ts
ws.Cells(iRow + 4, 2).Value = ts
ws.Cells(iRow + 5, 2).Value = ts
ws.Cells(iRow + 6, 2).Value = ts
ws.Cells(iRow + 7, 2).Value = ts
ws.Cells(iRow + 8, 2).Value = ts
ws.Cells(iRow + 9, 2).Value = ts
ws.Cells(iRow + 10, 2).Value = ts
ws.Cells(iRow + 11, 2).Value = ts
ws.Cells(iRow + 12, 2).Value = ts
ws.Cells(iRow + 13, 2).Value = ts
'clear the data
Me.txtSample1.Value = ""
Me.txtSample2.Value = ""
Me.txtSample3.Value = ""
Me.txtSample4.Value = ""
Me.txtSample5.Value = ""
Me.txtSample6.Value = ""
Me.txtSample7.Value = ""
Me.txtSample8.Value = ""
Me.txtSample9.Value = ""
Me.txtSample10.Value = ""
Me.txtSample11.Value = ""
Me.txtSample12.Value = ""
Me.txtSample13.Value = ""
Me.txtSample14.Value = ""
Me.txtPerson.Value = ""
Me.txtPerson.SetFocus
End Sub
Have tried a few things, but cannot seem to make it work right. I'm sure there is a simple solution, but I am not seeing it.
Basic Code:
Private Sub EnterPrint_cmd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("SampleLog")
Dim ts As Date
ts = Now
'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
'check for Person
If Trim(Me.txtPerson.Value) = "" Then
Me.txtPerson.SetFocus
MsgBox "Please enter Person"
Exit Sub
End If
'check for a Sample Id
If Trim(Me.txtSample1.Value) = "" Then
Me.txtSample1.SetFocus
MsgBox "Please enter a Sample Id"
Exit Sub
End If
'copy the sample id to database
ws.Cells(iRow, 1).Value = Me.txtSample1.Value
ws.Cells(iRow + 1, 1).Value = Me.txtSample2.Value
ws.Cells(iRow + 2, 1).Value = Me.txtSample3.Value
ws.Cells(iRow + 3, 1).Value = Me.txtSample4.Value
ws.Cells(iRow + 4, 1).Value = Me.txtSample5.Value
ws.Cells(iRow + 5, 1).Value = Me.txtSample6.Value
ws.Cells(iRow + 6, 1).Value = Me.txtSample7.Value
ws.Cells(iRow + 7, 1).Value = Me.txtSample8.Value
ws.Cells(iRow + 8, 1).Value = Me.txtSample9.Value
ws.Cells(iRow + 9, 1).Value = Me.txtSample10.Value
ws.Cells(iRow + 10, 1).Value = Me.txtSample11.Value
ws.Cells(iRow + 11, 1).Value = Me.txtSample12.Value
ws.Cells(iRow + 12, 1).Value = Me.txtSample13.Value
ws.Cells(iRow + 13, 1).Value = Me.txtSample14.Value
'add date to database
ws.Cells(iRow, 2).Value = ts
ws.Cells(iRow + 1, 2).Value = ts
ws.Cells(iRow + 2, 2).Value = ts
ws.Cells(iRow + 3, 2).Value = ts
ws.Cells(iRow + 4, 2).Value = ts
ws.Cells(iRow + 5, 2).Value = ts
ws.Cells(iRow + 6, 2).Value = ts
ws.Cells(iRow + 7, 2).Value = ts
ws.Cells(iRow + 8, 2).Value = ts
ws.Cells(iRow + 9, 2).Value = ts
ws.Cells(iRow + 10, 2).Value = ts
ws.Cells(iRow + 11, 2).Value = ts
ws.Cells(iRow + 12, 2).Value = ts
ws.Cells(iRow + 13, 2).Value = ts
'clear the data
Me.txtSample1.Value = ""
Me.txtSample2.Value = ""
Me.txtSample3.Value = ""
Me.txtSample4.Value = ""
Me.txtSample5.Value = ""
Me.txtSample6.Value = ""
Me.txtSample7.Value = ""
Me.txtSample8.Value = ""
Me.txtSample9.Value = ""
Me.txtSample10.Value = ""
Me.txtSample11.Value = ""
Me.txtSample12.Value = ""
Me.txtSample13.Value = ""
Me.txtSample14.Value = ""
Me.txtPerson.Value = ""
Me.txtPerson.SetFocus
End Sub