Advertisement
Guest User

User Data Form

a guest
Aug 19th, 2024
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. Sub Submit()
  2.  
  3. Dim sh As Worksheet
  4. Dim tbl As ListObject
  5. Dim newRow As ListRow
  6.  
  7. On Error GoTo ErrorHandler ' Set up error handling
  8.  
  9. ' Check if the worksheet exists
  10. On Error Resume Next
  11. Set sh = ThisWorkbook.Sheets("05618")
  12. On Error GoTo ErrorHandler
  13. If sh Is Nothing Then
  14. MsgBox "Worksheet '05618' not found!", vbCritical
  15. Exit Sub
  16. End If
  17.  
  18.  
  19. ' Check if the table exists on the worksheet
  20. On Error Resume Next
  21. Set tbl = sh.ListObjects("TableOhFiveSixOneEight") ' Ensure this matches your table name
  22. On Error GoTo ErrorHandler
  23. If tbl Is Nothing Then
  24. MsgBox "Table 'TableOhFiveSixOneEight' not found on the worksheet '05618'!", vbCritical
  25. Exit Sub
  26. End If
  27.  
  28. ' Try to add a new row to the table
  29. On Error Resume Next
  30. Set newRow = tbl.ListRows.Add(AlwaysInsert:=True)
  31. If Err.Number <> 0 Then
  32. MsgBox "Failed to add a new row: " & Err.Description, vbCritical
  33. Exit Sub
  34. End If
  35. On Error GoTo ErrorHandler
  36.  
  37. ' Populate the new row with form data
  38. With newRow.Range
  39. .Cells(2, 1).Value = frmForm.txtMRN.Text
  40. .Cells(2, 2).Value = frmForm.txtName.Text
  41. .Cells(2, 3).Value = frmForm.txtID.Text
  42. .Cells(2, 4).Value = frmForm.cmbPhysician.Value
  43. .Cells(2, 5).Value = frmForm.cmbNurse.Value
  44. .Cells(2, 6).Value = frmForm.cmbStatus.Value
  45. .Cells(2, 7).Value = frmForm.cmbCycle.Value
  46. .Cells(2, 8).Value = frmForm.txtDate.Text
  47. .Cells(2, 9).Value = frmForm.cmbCalendar.Value
  48. .Cells(2, 10).Value = frmForm.cmbLabs.Value
  49. .Cells(2, 11).Value = frmForm.cmbRecist.Value
  50. .Cells(2, 12).Value = Application.UserName
  51. .Cells(2, 13).Value = Format(Now(), "MM/DD/YYYY")
  52. End With
  53.  
  54.  
  55. Exit Sub
  56.  
  57. ErrorHandler:
  58. MsgBox "An error occurred: " & Err.Description, vbCritical
  59. End Sub
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement