Advertisement
tegarkurniawan

contoh2

Feb 1st, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 KB | None | 0 0
  1. 1. Module
  2.  
  3. Public Conn As New ADODB.Connection 'untuk koneksi
  4. Public Rs As New ADODB.Recordset 'membedakan tabel
  5. Public StrConnect As String 'membaca text
  6. Public StrSQL As String
  7. Public Sub konek()
  8. StrConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\db_sekolah.mdb" 'menentukan provider dan tempat databasenya
  9. If Conn.State = adStateOpen Then 'jika database dalam kondisi terbuka
  10. Conn.Close 'maka ditutup
  11. Else 'jika tidak
  12. Conn = New ADODB.Connection
  13. Conn.Open StrConnect
  14. End If
  15. End Sub
  16.  
  17. 2. Script Login
  18.  
  19. Private Sub cmdBatal_Click()
  20. txtUsername.Text = ""
  21. txtPassword.Text = ""
  22. cmbhakAkses.Text = ""
  23. End Sub
  24.  
  25. Private Sub cmdMasuk_Click()
  26. If txtUsername.Text = "" And txtPassword.Text = "" Then
  27. MsgBox "Anda belum mengisikan Username & Password!", vbExclamation + vbOKOnly, "Informasi"
  28. txtUsername.SetFocus
  29. ElseIf txtUsername.Text = "" Then
  30. MsgBox "Anda belum mengisikan Username!", vbExclamation + vbOKOnly, "Informasi"
  31. txtUsername.SetFocus
  32. ElseIf txtPassword.Text = "" Then
  33. MsgBox "Anda belum mengisikan Password!", vbExclamation + vbOKOnly, "Informasi"
  34. txtPassword.SetFocus
  35. Else
  36. StrSQL = "SELECT * FROM login WHERE nama ='" & txtUsername.Text & "'"
  37. Set Rs = Conn.Execute(StrSQL)
  38. If Rs.EOF Then
  39. MsgBox "Username yang Anda masukkan salah!", vbCritical, "Informasi"
  40. txtUsername.SetFocus
  41. ElseIf txtPassword.Text <> Rs("sandi") Then
  42. MsgBox "Password yang Anda masukkan salah!", vbCritical, "Informasi"
  43. txtPassword.SetFocus
  44. Else
  45. MsgBox "Login Berhasil!", vbInformation + vbOKOnly, "Informasi"
  46. Menu.Show
  47. End If
  48. End If
  49. Unload Me
  50. End Sub
  51.  
  52. Private Sub Form_Load()
  53. Call konek
  54. End Sub
  55.  
  56. Private Sub txtPassword_KeyPress(KeyAscii As Integer)
  57. If KeyAscii = 13 Then
  58. cmdMasuk.SetFocus
  59. End If
  60. End Sub
  61.  
  62. Private Sub txtUsername_KeyPress(KeyAscii As Integer)
  63. If KeyAscii = 13 Then
  64. txtPassword.SetFocus
  65. End If
  66. End Sub
  67.  
  68. 3. Script pada form siswa
  69.  
  70. Private Sub cmdBatal_Click()
  71. Call BERSIH
  72. End Sub
  73.  
  74. Private Sub cmdCari_Click()
  75. Call KEADAANTEKS(False)
  76. If (txtCari.Text = "") Then
  77. MsgBox "NIS yang dicari belum diisi!", vbInformation + vbOKOnly, "Infomasi"
  78. Else
  79. StrSQL = "SELECT * FROM siswa WHERE NIS ='" & txtCari.Text & "'"
  80. Set Rs = Conn.Execute(StrSQL)
  81. If Rs.EOF Then
  82. MsgBox "Siswa dengan NIS tersebut tidak ada!", vbInformation + vbOKOnly, "Informasi"
  83. Call BERSIH
  84. txtCari.Text = ""
  85. Else
  86. txtNIS.Text = "" + Rs("NIS")
  87. txtNama.Text = "" + Rs("Nama")
  88. txtTanggal.Text = "" + Rs("TTL")
  89. txtAlamat.Text = "" + Rs("Alamat")
  90. End If
  91. End If
  92. End Sub
  93.  
  94. Private Sub cmdEdit_Click()
  95. If (txtCari.Text = "") Then
  96. MsgBox "Silahkan cari data yg akan di edit!", vbInformation + vbOKOnly, "Informasi"
  97. Else
  98. Call KEADAANTEKS(True)
  99. cmdUpdate.Enabled = True
  100. cmdSimpan.Enabled = False
  101. cmdBatal.Enabled = False
  102. End If
  103. End Sub
  104.  
  105. Private Sub cmdHapus_Click()
  106. Dim pesan As Integer
  107. If (txtCari.Text = "") Then
  108. MsgBox "Silahkan cari data yg akan di hapus!", vbInformation + vbOKOnly, "Informasi"
  109. Else
  110. pesan = MsgBox("Apakah anda yakin akan mengahpus data ini ?", vbQuestion + vbYesNo, "Konfirmasi")
  111. If pesan = 6 Then
  112. StrSQL = "DELETE FROM siswa Where NIS='" & txtCari.Text & "'"
  113. Conn.Execute (StrSQL)
  114.  
  115. Call REFRESHDATA
  116.  
  117. txtCari.Text = ""
  118. Call BERSIH
  119. MsgBox "Data berhasil di hapus!", vbInformation + vbOKOnly, "Informasi"
  120. End If
  121. End If
  122.  
  123. 'Dim cari As String
  124. 'cari = InputBox("Masukan NIS yang akan di hapus!", "Hapus Data")
  125. 'Adodc1.Recordset.Find "NIS='" & cari & "'"
  126. 'If Not Adodc1.Recordset.EOF Then
  127. 'Adodc1.Recordset.Delete
  128. 'MsgBox "Data berhasil di hapus!", vbInformation + vbOKOnly, "Informasi"
  129. 'End If
  130. End Sub
  131.  
  132. Private Sub cmdSimpan_Click()
  133. If (txtNIS.Text = "") Then
  134. MsgBox "Data belum diisi!, Silahkan diisi!", vbExclamation + vbOKOnly, "Informasi"
  135. Else
  136. StrSQL = "SELECT NIS FROM siswa WHERE NIS='" & txtNIS.Text & "'"
  137. Set Rs = Conn.Execute(StrSQL)
  138. If Not Rs.EOF Then
  139. MsgBox "NIS sudah ada, silahkan isi baru!", vbInformation + vbOKOnly, "Informasi"
  140. txtNIS.SetFocus
  141. Call BERSIH
  142. Else
  143. StrSQL = "INSERT INTO siswa(NIS, Nama, TTL, Alamat) values ('" & txtNIS.Text & "','" & txtNama.Text & "','" & txtTanggal.Text & "','" & txtAlamat.Text & "')"
  144. Conn.Execute (StrSQL)
  145. MsgBox "Apa yakin data akan di simpan?", vbQuestion + vbOKCancel, "Konfirmasi"
  146. Call REFRESHDATA
  147. Call BERSIH
  148. Call KEADAANTEKS(False)
  149. cmdSimpan.Enabled = False
  150. cmdBatal.Enabled = False
  151. End If
  152. End If
  153. End Sub
  154.  
  155. Private Sub cmdTambah_Click()
  156. Call KEADAANTEKS(True)
  157. cmdSimpan.Enabled = True
  158. cmdBatal.Enabled = True
  159. txtNIS.SetFocus
  160. Call BERSIH
  161. End Sub
  162.  
  163. Private Sub cmdUpdate_Click()
  164. StrSQL = "SELECT NIS FROM siswa WHERE NIS='" & txtNIS.Text & "'"
  165. Set Rs = Conn.Execute(StrSQL)
  166. If (txtNIS.Text <> txtCari.Text) And (Not Rs.EOF) Then
  167. MsgBox "NIS tersebut sudah ada!", vbInformation + vbOKOnly, "Information"
  168. txtNIS.SetFocus
  169. Else
  170. StrSQL = "UPDATE siswa SET NIS='" & txtNIS.Text & "', Nama='" & txtNama.Text & "', TTL='" & txtTanggal.Text & "', Alamat='" & txtAlamat.Text & "' WHERE NIS='" & txtCari.Text & "'"
  171. Conn.Execute (StrSQL)
  172. MsgBox "Apa yakin data akan di edit?", vbQuestion + vbOKCancel, "Konfirmasi"
  173. Call REFRESHDATA
  174. Call BERSIH
  175. End If
  176. End Sub
  177.  
  178. Private Sub Form_Load()
  179. Call REFRESHDATA
  180. Call KEADAANTEKS(False)
  181.  
  182. cmdSimpan.Enabled = False
  183. cmdBatal.Enabled = False
  184. cmdUpdate.Enabled = False
  185. End Sub
  186.  
  187. Sub BERSIH()
  188. txtNIS.Text = ""
  189. txtNama.Text = ""
  190. txtTanggal.Text = ""
  191. txtAlamat.Text = ""
  192. End Sub
  193.  
  194. Sub REFRESHDATA()
  195. Call konek
  196. Adodc1.ConnectionString = StrConnect
  197. Adodc1.RecordSource = "siswa"
  198. Adodc1.Refresh
  199. Set DataGrid1.DataSource = Adodc1
  200. DataGrid1.Refresh
  201. Set Conn = New ADODB.Connection
  202. Conn.Open StrConnect
  203. End Sub
  204.  
  205. Sub KEADAANTEKS(stat As Boolean)
  206. txtNIS.Enabled = stat
  207. txtNama.Enabled = stat
  208. txtTanggal.Enabled = stat
  209. txtAlamat.Enabled = stat
  210. End Sub
  211.  
  212. Private Sub txtAlamat_KeyPress(KeyAscii As Integer)
  213. If KeyAscii = 13 Then
  214. cmdSimpan.SetFocus
  215. End If
  216. End Sub
  217.  
  218. Private Sub txtCari_KeyPress(KeyAscii As Integer)
  219. If KeyAscii = 13 Then
  220. cmdCari.SetFocus
  221. End If
  222. End Sub
  223.  
  224. Private Sub txtNama_KeyPress(KeyAscii As Integer)
  225. If KeyAscii = 13 Then
  226. txtTanggal.SetFocus
  227. End If
  228. End Sub
  229.  
  230. Private Sub txtNIS_KeyPress(KeyAscii As Integer)
  231. If KeyAscii = 13 Then
  232. txtNama.SetFocus
  233. End If
  234. End Sub
  235.  
  236. Private Sub txtTanggal_KeyPress(KeyAscii As Integer)
  237. If KeyAscii = 13 Then
  238. txtAlamat.SetFocus
  239. End If
  240. End Sub
  241.  
  242. 4. Script Report
  243.  
  244. Private Sub cmdCetakSiswa_Click()
  245. Set DataReport1.DataSource = Adodc1
  246. DataReport1.Refresh
  247. DataReport1.WindowState = 2
  248. DataReport1.Show
  249. Adodc1.Refresh
  250. End Sub
  251.  
  252. Private Sub Form_Load()
  253. Call konek
  254. Adodc1.ConnectionString = StrConnect
  255. Adodc1.RecordSource = "siswa"
  256. Adodc1.Refresh
  257. Set DataGrid1.DataSource = Adodc1
  258. DataGrid1.Refresh
  259. Set Conn = New ADODB.Connection
  260. Conn.Open StrConnect
  261. End Sub
  262.  
  263. 5. Script Memanggil data dalam table lain
  264.  
  265. Private Sub cmbnorek_Click()
  266. StrSQL = "SELECT * FROM Pelanggan WHERE No_Rek='" & cmbnorek.Text & "'"
  267. Set Rs = Conn.Execute(StrSQL)
  268. lblnama.Caption = "" & Rs.Fields("Nama")
  269. txtabodemen.Text = "" & Rs.Fields("Abodemen")
  270. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement