Fully Automatic Data Entry userform in Excel VBA || Employee Database in Excel VBA

Click here to download this file

Fully Automatic Data Entry userform in Excel VBA || Employee Database in Excel VBA

Full vba code I shared here for your references. You can follow it 

  1. Option Explicit
  2. ------------------------------------------------------------------
  3. Private Sub UserForm_Initialize()
  4. Me.ComboBox1.AddItem "EMP-ID"
  5. Me.ComboBox1.AddItem "CONTACT NO"
  6. End Sub

  7. Private Sub TextBox1_Change()
  8. Me.TextBox1 = UCase(Me.TextBox1)
  9. End Sub
  10. Private Sub TextBox2_Change()
  11. Me.TextBox2 = UCase(Me.TextBox2)
  12. End Sub

  13. Private Sub TextBox3_Change()
  14. Me.TextBox3 = UCase(Me.TextBox3)

  15. End Sub

  16. Private Sub TextBox4_AfterUpdate()
  17. Me.TextBox4.Value = Format(Me.TextBox4, "dd-mmm-yy")

  18. End Sub


  19. Private Sub TextBox5_Change()
  20. Me.TextBox5 = UCase(Me.TextBox5)

  21. End Sub

  22. Private Sub TextBox6_AfterUpdate()
  23. Me.TextBox6.Text = Format(Me.TextBox6.Text, "000-000-0000")
  24. End Sub


  25. Private Sub TextBox8_Change()
  26. Me.TextBox8 = UCase(Me.TextBox8)

  27. End Sub

  28. Private Sub TextBox9_AfterUpdate()
  29. Me.TextBox9.Value = Format(Me.TextBox9, "dd-mmm-yy")

  30. End Sub
  31. ===============================================
  32. Private Sub CommandButton1_Click()
  33. ''**************CODES for Add Button****************

  34. If Me.Image1.Picture Is Nothing Then
  35. MsgBox "Please Insert Image First"
  36. Exit Sub
  37. End If
  38. '----------DEFINE WORKSHEET WHERE YOU ARE WORKING---
  39. Dim sh As Worksheet
  40. Set sh = Sheets("SHEET1")
  41. sh.Unprotect "121"
  42. '---------------TO FIND OUT LAST ROW---------------
  43. Dim i As Long
  44. i = sh.Range("A" & Rows.Count).End(xlUp).Row + 1

  45. '' ----------NOW PREVENT DUPLICATE ENTRY-----------
  46. Dim m As Long
  47. For m = 3 To i
  48. If Application.WorksheetFunction.CountIf(sh.Range("G3", "G" & m), Me.TextBox6.Value) > 0 Then
  49. MsgBox "DUPLICATE ITEM, PLEASE TRY ANOTHER"
  50. Exit Sub
  51. End If
  52. Next m
  53. '-----------------------------------------------
  54. '' mandatory field
  55. Dim n As Integer
  56. For n = 1 To 9

  57. If Me("TextBox" & n).Value = "" Then
  58. MsgBox "All field are mandatory"
  59. Exit Sub
  60. End If
  61. Next n

  62. '------------TRANSFERING DATA---------------------
  63. '' emp id
  64. sh.Cells(i, 1) = Me.TextBox1.Value & "_00" & i
  65. Dim x As Integer
  66. For x = 1 To 9
  67. sh.Cells(i, x + 1) = Me("TextBox" & x).Value
  68. Next x
  69. ''''----------Call image_transfer------------------
  70. Call image_transfer
  71. '-------------------------------------------------
  72. sh.Protect "121"

  73. MsgBox "DATA TRANSFERED SUCCESSFUL"
  74. '------------RESET THE FORM-----------------------
  75. Call CommandButton5_Click

  76. End Sub
  77. ===============================================
  78. Private Sub CommandButton2_Click()
  79. '***************CODES for Update*****************

  80. Dim sh As Worksheet
  81. Set sh = Sheets("Sheet1")
  82. '------------------------------------------------
  83. sh.Unprotect "121"
  84. '----TO FIND OUT THAT ROW WHICH IS BEING EDITED------
  85. Dim i As Long
  86. For i = 3 To sh.Range("A" & Rows.Count).End(xlUp).Row
  87. '----------------------------------------------------
  88. '-----DATA WILL SEARCH BY THE VALUE OF TEXTBOX10-----
  89. If Me.TextBox10.Value = sh.Cells(i, 1) Then
  90. '' Here if you want to search from HSN CODE i.e Column B then 2,
  91. Dim x As Integer
  92. '------NOW EDIT WILL MADE----------------------------
  93. For x = 1 To 9
  94. sh.Cells(i, x + 1) = Me("TextBox" & x).Value
  95. On Error Resume Next
  96. '--------PREVIOUS PIC WILL BE DELETED----------------
  97. ActiveSheet.Shapes.Range(Array("IMAGE_" & i)).Select
  98. Selection.Delete
  99. Next x
  100. End If
  101. Next i
  102. '------------------------------------------------------
  103. Call image_transfer 'NOW NEW PIC WILL BE PLACED
  104. '------------------------------------------------------
  105. sh.Protect "121"

  106. MsgBox "EDITED SUCCESSFUL"
  107. '-----------------------------------------------------
  108. Call CommandButton5_Click 'RESET THE FORM

  109. End Sub
  110. ===================================================
  111. Private Sub CommandButton3_Click()
  112. '' FOR DELETE ROW
  113. Dim sh As Worksheet
  114. Set sh = Sheets("Sheet1")
  115. sh.Unprotect "121"

  116. '----TO FIND OUT THAT ROW WHICH IS BEING EDITED------
  117. Dim i As Long
  118. For i = 3 To sh.Range("A" & Rows.Count).End(xlUp).Row
  119. '---------------DELETE SELECTED ROW------------------
  120. If Me.TextBox10.Value = sh.Cells(i, 1) Then
  121. sh.Cells(i, 1).EntireRow.Delete
  122. '------------- TO delete image----------------------
  123. On Error Resume Next
  124. ActiveSheet.Shapes.Range(Array("IMAGE_" & i)).Select
  125. Selection.Delete
  126. End If
  127. Next i
  128. '--------------------------------------------------
  129. sh.Protect "121"

  130. MsgBox "DELETED SUCCESSFUL"
  131. Call CommandButton5_Click 'RESET THE FORM
  132. Me.TextBox10.Enabled = True
  133. Me.TextBox10 = ""
  134. Me.CommandButton4.Enabled = True 'ENABLE CLICK TO EDIT BUTTON
  135. End Sub
  136. ==============================================
  137. Private Sub CommandButton4_Click()
  138. '************click to Edit Button**********************
  139. If Me.TextBox10 = "" Then Exit Sub
  140. '---------- DISABLED ADD BUTTON---------------------
  141. Me.CommandButton1.Enabled = False

  142. '----SELECT THE ROW OF LISTBOX------------------
  143. Dim r As Long
  144. For r = 0 To Me.ListBox1.ListCount - 1
  145. If Me.ListBox1.Selected(r) = True Then

  146. '-------------Data Transfer from ListBox To TextBoxes----
  147. Dim x As Integer
  148. For x = 1 To 9
  149. Me("TextBox" & x).Value = Me.ListBox1.Column(x)
  150. Next x
  151. End If
  152. Next r
  153. '------------------------------------------------------
  154. On Error Resume Next
  155. '-----EDIT, DELETE WILL MADE FOR TEXTBOX10'S VALUE------
  156. Me.TextBox10.Value = Me.ListBox1.Column(0)

  157. Me.TextBox10.Enabled = False
  158. '----------- LOAD searchED image---------------------
  159.  Dim ipath As String
  160. ipath = "C:\MY FOLDER\" & Me.TextBox10 & ".jpg"
  161. Me.Image1.Picture = LoadPicture(ipath)
  162. Me.Image1.PictureSizeMode = fmPictureSizeModeStretch
  163. '' Disable "Click To Edit" Button
  164. Me.CommandButton4.Enabled = False
  165. End Sub
  166. ==================================================
  167. Private Sub CommandButton5_Click()
  168. '************RESET BUTTON********************
  169. Dim x As Integer
  170. For x = 1 To 10
  171. Me("TextBox" & x).Value = ""
  172. Next x
  173. Me.CommandButton4.Enabled = True
  174. On Error Resume Next
  175. Me.Image1.Picture = LoadPicture(vbNullString)
  176. End Sub
  177. ==============================================
  178. Private Sub CommandButton6_Click()
  179. '*************ADD PHOTO BUTTON***************************
  180. On Error Resume Next
  181. Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
  182. Application.FileDialog(msoFileDialogOpen).Show '' to show browse box
  183. '-----------TO SELECT IMAGE SOURCE PATH---------------------
  184. Dim filepath As String
  185. filepath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
  186. '------------LOAD FROM SOURCE PATH-------------------
  187. Me.Image1.Picture = LoadPicture(filepath)
  188. Me.Image1.PictureSizeMode = fmPictureSizeModeStretch
  189. '------------------SUBSCRIBE MY CHANNEL-------------------
  190. End Sub

  191. Private Sub CommandButton7_Click()
  192. Unload Me
  193. End Sub
  194. =======================================================
  195. Private Sub TextBox10_Change()
  196. '********* LOAD LISTBOX WHEN WE TYPING ON THIS TEXTBOX************
  197. '-------FORMAT TEXTBOX AS UPPER/LOWER CASE---------------------
  198. Me.TextBox10.Value = UCase(Me.TextBox10)
  199. '--------CLEAR LISTBOX EVERYTIME WHEN CLICK-------------------
  200. Me.ListBox1.Clear
  201. '------------------------------------------------------
  202. Dim SHT As Worksheet
  203. Set SHT = Sheets("SHEET1")
  204. Dim b, a As Integer
  205. '-----TO SELECT COLUMN NUMBER--FROM COMBOBOX--------
  206. If Me.ComboBox1.Value = "EMP-ID" Then b = 1
  207. If Me.ComboBox1.Value = "CONTACT NO" Then b = 7
  208. '--------TO FIND OUT THE ROW NUMBER----------------
  209. Dim i, x As Long
  210. For i = 3 To SHT.Range("A" & Rows.Count).End(xlUp).Row
  211. '----SHOW MSGBOX IF COMBOBOX BLANK--------------
  212. If Me.ComboBox1.Value = "" Then
  213. MsgBox "SELECT CRITERIA"
  214. Exit Sub
  215. End If
  216. '------------------------------------------------
  217. '-------------TO COUNT CHARECTERS OF COLUMN'S CELL-------
  218. For x = 1 To Len(SHT.Cells(i, b))
  219. '------TO COUNT TEX------------------------------
  220. a = Me.TextBox10.TextLength
  221. '------NOW APLLY CONDITION-------------------------
  222. If UCase(Mid(SHT.Cells(i, b), x, a)) = Me.TextBox10 And Me.TextBox10 <> "" Then
  223. 'NOW LOAD LISBOX BASED ON ABOVE COND----------------------------
  224. With Me.ListBox1
  225. .AddItem SHT.Cells(i, 1)
  226. .List(.ListCount - 1, 1) = SHT.Cells(i, 2)
  227. .List(.ListCount - 1, 2) = SHT.Cells(i, 3)
  228. .List(.ListCount - 1, 3) = SHT.Cells(i, 4)
  229. .List(.ListCount - 1, 4) = SHT.Cells(i, 5)
  230. .List(.ListCount - 1, 5) = SHT.Cells(i, 6)
  231. .List(.ListCount - 1, 6) = SHT.Cells(i, 7)
  232. .List(.ListCount - 1, 7) = SHT.Cells(i, 8)
  233. .List(.ListCount - 1, 8) = SHT.Cells(i, 9)
  234. .List(.ListCount - 1, 9) = SHT.Cells(i, 10)
  235. End With
  236. End If
  237. Next x
  238. Next i
  239. '------------NSUTRADHAR--------------------------------
  240. End Sub
  241. ===========================================
  242. Sub RectangleRoundedCorners1_Click()
  243. Emp_Database_frm.Show
  244. End Sub
  245. ===========================================
  246. Sub image_transfer()
  247. If Emp_Database_frm.TextBox1 = "" Then Exit Sub
  248. Dim sh As Worksheet
  249. Set sh = Sheets("Sheet1")
  250. Dim i As Long
  251. i = sh.Range("A" & Rows.Count).End(xlUp).Row
  252. '' to copy image from userform & paste it with resize
  253. Dim filepath As String
  254. filepath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
  255. sh.Cells(i, "K").Select
  256. sh.Range("K" & i).RowHeight = 40
  257. sh.Range("K" & i).ColumnWidth = 13
  258. sh.Cells(i, "K") = sh.Pictures.Insert(filepath).Select
  259. Selection.ShapeRange.Name = "IMAGE_" & i
  260. Selection.ShapeRange.ScaleWidth 0.3, msoFalse, msoScaleFromTopLeft
  261. Selection.ShapeRange.ScaleHeight 0.3, msoFalse, msoScaleFromTopLeft
  262. '' to copy the image and save it on a folder
  263.  Dim ipath As String
  264. ipath = "C:\MY FOLDER\" & Emp_Database_frm.TextBox1 & "_00" & i & ".jpg"
  265.  FileCopy filepath, ipath
  266. '' now paste the hyperlink of the image
  267.  sh.Cells(i, "L").Select
  268.  ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:=ipath, TextToDisplay:=Emp_Database_frm.TextBox1.Value
  269. Emp_Database_frm.Image1.Picture = LoadPicture(vbNullString)
  270. End Sub

For Support and FeedBack
You can share your feedback through this blogs or can contact through email if you have any queries regarding any excel related file. Thanks for visiting this blogs.

15 comments:

  1. Great job sir keep it up really enjoyed your work and found the ucase as additional for my template in which you already worked and make it easy for me hope the teaching and learning process will be continue onward and once again bundleeee Of thanks

    ReplyDelete
  2. Please send the file

    ReplyDelete
  3. Please send the file

    ReplyDelete
  4. Great job
    Please kindly send the file
    thank you

    ReplyDelete
  5. Pls can you send the link through mail?

    ReplyDelete
  6. Many people ask you about:
    PLEASE SHARE PASSWORD TO UNPROTECT EXCEL FILE

    Can you? Then why we can download without do something...

    ReplyDelete