home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / My_DLL(upd20886610272007.psc / EazyDtAcc+NewMsgBox / DLL / AksesDatabase.cls next >
Text File  |  2007-10-03  |  10KB  |  452 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 1  'vbDataSource
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "AksesData"
  10. Attribute VB_GlobalNameSpace = True
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = True
  14. '%#########################################%'
  15. '%Author   : Tendri S (20)                 %'
  16. '%Date     : October 03, 2007 (Update)     %'
  17. '%Location : Bekasi, Indonesia             %'
  18. '%Email    : mizz_daeng@plasa.com          %'
  19. '%Please Do Not Removes Any Copyrights     %'
  20. '%#########################################%'
  21.  
  22. Private Cn As New ADODB.Connection
  23. Private rs As New ADODB.Recordset
  24. Private Rs2 As New ADODB.Recordset
  25. Private TransOpen As Boolean
  26. Private RsOpen As Boolean
  27. Private RsOpen2 As Boolean
  28. Private ConnectString As String
  29.  
  30. Private mvarConnectStr As String
  31. Private mvarConnectionType As ConnectType
  32.  
  33. Public Enum ConnectType
  34.  OnDemand = 0
  35.  Persist = 1
  36. End Enum
  37. Public Property Let TipeKoneksi(ByVal vData As ConnectType)
  38. 'mengubah nilai
  39. mvarConnectionType = vData
  40. End Property
  41. Public Property Get TipeKoneksi() As ConnectType
  42. 'membaca nilai
  43. TipeKoneksi = mvarConnectionType
  44. End Property
  45. Public Property Let KonekStr(ByVal vData As String)
  46. mvarConnectStr = vData
  47. ConnectString = mvarConnectStr
  48. End Property
  49. Public Property Get KonekStr() As String
  50. KonekStr = mvarConnectStr
  51. End Property
  52. Public Function GetAllData(Tabel As String, OrderBy As String) As Recordset
  53. On Error GoTo ErrorCheck
  54.  
  55. If Not Cn.State = adStateOpen Then
  56.  ConnectDb
  57. End If
  58.  
  59. yy = "select * from " & Tabel & " order by " & OrderBy
  60. Set rs = New ADODB.Recordset
  61. rs.Open yy, Cn, adOpenDynamic, adLockOptimistic
  62. RsOpen = True
  63. Set GetAllData = rs
  64.  
  65. Exit Function
  66.  
  67. If TipeKoneksi = OnDemand Then
  68.  Set Cn = Nothing
  69. End If
  70.  
  71. rs.Close
  72. Set rs = Nothing
  73.  
  74. ErrorCheck:
  75.  MsgBox Err.Description, , "Data Access"
  76.  If TipeKoneksi = OnDemand Then
  77.   DisconnectDB
  78.  End If
  79. End Function
  80. Public Function DeleteData(Tabel As String, Field As String, Data As String) As Boolean
  81. 'UNTUK NGEHAPUS RECORD-RECORD DARI DATABASE
  82. On Error GoTo ErrorCheck
  83.  
  84. If Not Cn.State = adStateOpen Then
  85.  ConnectDb
  86. End If
  87.  
  88. yy = "select * from " & Tabel & " where " & Field & "='" & Data & "'"
  89. Set rs = New ADODB.Recordset
  90. rs.Open yy, Cn, adOpenKeyset, adLockOptimistic
  91.  
  92. If Not rs.EOF Then
  93.  xx = "delete from " & Tabel & " where " & Field & "='" & Data & "'"
  94.  Cn.Execute xx
  95.  DeleteData = True
  96.  'MsgBox "Data is completely removed..", vbInformation, "Data Access"
  97.  rs.Close
  98.  Set rs = Nothing
  99. Else
  100.  'MsgBox "Record not found, Delete Failed..", vbCritical, "Pesan"
  101.  mb.TendriMsg "Record not found, Delete Failed..", vbCritical, "Tendri Data Access"
  102.  DeleteData = False
  103.  rs.Close
  104.  Set rs = Nothing
  105.  Exit Function
  106. End If
  107.   
  108. If TipeKoneksi = OnDemand Then
  109.  DisconnectDB
  110. End If
  111.  
  112. Exit Function
  113.  
  114. ErrorCheck:
  115.  MsgBox Err.Description, , "Data Access"
  116.  If TipeKoneksi = OnDemand Then
  117.   DisconnectDB
  118.  End If
  119.  DeleteData = False
  120. End Function
  121.  
  122. Public Function GetData(Tabel As String, NmField As String, PKey As String, Data As String)
  123. On Error GoTo ErrorCheck
  124.  
  125. If Not Cn.State = adStateOpen Then
  126.  ConnectDb
  127. End If
  128.  
  129. yy = "select " & NmField & " from " & Tabel & " where " & PKey & "='" & Data & "'"
  130. Set rs = New ADODB.Recordset
  131. rs.Open yy, Cn, adOpenDynamic, adLockOptimistic
  132.  
  133. If Not rs.EOF Then
  134.  GetData = rs(0)
  135. Else
  136.  GetData = ""
  137. End If
  138.  
  139. rs.Close
  140. Set rs = Nothing
  141.  
  142. Exit Function
  143.  
  144. If TipeKoneksi = OnDemand Then
  145.  Set Cn = Nothing
  146. End If
  147.  
  148. ErrorCheck:
  149.  MsgBox Err.Description, , "Data Access"
  150.  If TipeKoneksi = OnDemand Then
  151.   DisconnectDB
  152.  End If
  153. End Function
  154. Public Function CekData(Tabel As String, NmField As String, Data As Variant) As Boolean
  155. On Error GoTo ErrorCheck
  156.  
  157. Dim xx As String
  158.     
  159. If Not Cn.State = adStateOpen Then
  160.  ConnectDb
  161. End If
  162.  
  163. xx = "select * from " & Tabel & " where " & NmField & "='" & Data & "'"
  164. rs.Open xx, Cn, adOpenDynamic, adLockOptimistic
  165.  
  166. If Not rs.EOF Then
  167.  CekData = True
  168. Else
  169.  CekData = False
  170. End If
  171.  
  172. rs.Close
  173. Set rs = Nothing
  174.     
  175. If TipeKoneksi = OnDemand Then
  176.  DisconnectDB
  177. End If
  178.     
  179. Exit Function
  180.  
  181. ErrorCheck:
  182.  MsgBox Err.Description, , "Data Access"
  183.  CekData = False
  184.  If TipeKoneksi = OnDemand Then
  185.   DisconnectDB
  186.  End If
  187. End Function
  188.  
  189. Public Function FillListCombo(Field As String, Tabel As String, Combo As Object)
  190. On Error Resume Next
  191.  
  192. Dim x As String
  193.     
  194. Combo.Clear
  195.  
  196. If Not Cn.State = adStateOpen Then
  197.  ConnectDb
  198. End If
  199.  
  200. Set rs = New ADODB.Recordset
  201. x = "select " & Field & " from " & Tabel & " order by " & Field & ""
  202. rs.Open x, Cn, adOpenDynamic, adLockOptimistic
  203.  
  204. While Not rs.EOF
  205.  Combo.AddItem rs(0)
  206.  rs.MoveNext
  207. Wend
  208.  
  209. rs.Close
  210. Set rs = Nothing
  211.  
  212. If TipeKoneksi = OnDemand Then
  213.  DisconnectDB
  214. End If
  215. End Function
  216. Public Function InsertData(ByVal SqlStr As String, RecordArray() As Variant) As Boolean
  217. 'UNTUK TAMBAH DATA BARU
  218. Dim y As Integer
  219. Dim Size As Integer
  220. Dim RsCount As Integer
  221.  
  222. On Error GoTo ErrorCheck
  223.     
  224. 'jml array
  225. Size = UBound(RecordArray)
  226.     
  227. 'konek ke database
  228. If Not Cn.State = adStateOpen Then
  229.  ConnectDb
  230. End If
  231.  
  232. Cn.BeginTrans
  233.  
  234. TransOpen = True
  235. Rs2.Open SqlStr, Cn, adOpenKeyset, adLockOptimistic
  236. RsOpen = True
  237. Rs2.AddNew
  238.  
  239. For y = 0 To Size
  240.  If Not RecordArray(y) = Empty Then
  241.   Rs2.Fields(y).Value = RecordArray(y)
  242.  End If
  243. Next
  244.  
  245. Rs2.Update
  246. InsertData = True
  247. 'MsgBox "Data is completely saved..", vbInformation, "Data Access"
  248.  
  249. Rs2.Close
  250. RsOpen = False
  251.  
  252. Cn.CommitTrans
  253.  
  254. TransOpen = False
  255. Set Rs2 = Nothing
  256.  
  257. If TipeKoneksi = OnDemand Then
  258.  DisconnectDB
  259. End If
  260.  
  261. Exit Function
  262.  
  263. ErrorCheck:
  264.  MsgBox Err.Description, , "Data Access"
  265.  If TransOpen = True Then
  266.   Cn.RollbackTrans
  267.  End If
  268.  InsertData = False
  269.  If TipeKoneksi = OnDemand Then
  270.   DisconnectDB
  271.  End If
  272. End Function
  273. Public Function UpdateData(ByVal SqlStr As String, RecordArray() As Variant, Optional dtField As String) As Boolean
  274. 'UNTUK NGEDIT RECORD
  275. Dim y As Integer
  276. Dim Size As Integer
  277. Dim RsCount As Integer
  278.     
  279. On Error GoTo ErrorCheck
  280.     
  281. 'jml array
  282. Size = UBound(RecordArray)
  283.     
  284. If Not Cn.State = adStateOpen Then
  285.  ConnectDb
  286. End If
  287.  
  288. Cn.BeginTrans
  289.  
  290. TransOpen = True
  291. Rs2.Open SqlStr, Cn, adOpenKeyset, adLockOptimistic
  292. RsOpen = True
  293.  
  294. If Not Rs2.EOF Then
  295.  'Loop semua record
  296.  y = 0
  297.  RsCount = Rs2.RecordCount
  298.  
  299.  If RsCount > 1 Then
  300.   'MsgBox "More than one record, Update Failed..", vbCritical, "Data Access"
  301.   mb.TendriMsg "More than one record, Update Failed..", vbCritical, "Tendri Data Access"
  302.   UpdateData = False
  303.   Exit Function
  304.  Else
  305.   For y = 0 To Size
  306.    If Not RecordArray(y) = Empty Then
  307.     Rs2.Fields(y).Value = RecordArray(y)
  308.    End If
  309.   Next
  310.             
  311.   Rs2.Update
  312.   UpdateData = True
  313.   If dtField = Empty Then mb.TendriMsg "Data has been updated..", vbInformation, "Tendri Data Access"
  314.   'MsgBox "Data is completely updated..", vbInformation, "Data Access"
  315.   If dtField <> Empty Then
  316.    For I = 0 To RsCount
  317.     mb.TendriMsg "Data " & dtField & " " & Rs2.Fields(I) & " has been updated..", vbInformation, "Tendri Data Access"
  318.     Exit For
  319.    Next
  320.   End If
  321.  End If
  322.  
  323.  Rs2.Close
  324.  RsOpen = False
  325.  
  326.  Cn.CommitTrans
  327.  
  328.  TransOpen = False
  329.  Set Rs2 = Nothing
  330. Else
  331.  Cn.RollbackTrans
  332.  
  333.  Rs2.Close
  334.  RsOpen = False
  335.  Set Rs2 = Nothing
  336.  'MsgBox "No record found, Update Failed..", vbCritical, "Pesan"
  337.  mb.TendriMsg "No record found, Update Failed..", vbCritical, "Tendri Data Access"
  338.  UpdateData = False
  339.  Exit Function
  340. End If
  341.  
  342. If TipeKoneksi = OnDemand Then
  343.  DisconnectDB
  344. End If
  345.  
  346. Exit Function
  347.  
  348. ErrorCheck:
  349.  MsgBox Err.Description, , "Data Access"
  350.  If TransOpen = True Then
  351.   Cn.RollbackTrans
  352.  End If
  353.  UpdateData = False
  354.  If TipeKoneksi = OnDemand Then
  355.   DisconnectDB
  356.  End If
  357. End Function
  358.  
  359. Private Sub ConnectDb()
  360. 'UNTUK MEMBUAT KONEKSI ADO KE DB
  361. On Error GoTo ErrorCheck
  362.  
  363. Cn.ConnectionTimeout = 0
  364. Cn.CommandTimeout = 0
  365. Cn.CursorLocation = adUseClient
  366. Cn.Open ConnectString
  367.  
  368. Exit Sub
  369.  
  370. ErrorCheck:
  371.  MsgBox Err.Description, , "Data Access"
  372. End Sub
  373. Public Function SearchData(ByVal SqlStr As String) As Recordset
  374. On Error Resume Next
  375.  
  376. If Not Cn.State = adStateOpen Then
  377.  ConnectDb
  378. End If
  379.  
  380. Rs2.Open SqlStr, Cn, adOpenDynamic, adLockOptimistic
  381. If Not Rs2.EOF Then
  382.  RsOpen = True
  383.  Set SearchData = Rs2
  384. Else
  385.  Rs2.Close
  386.  Set Rs2 = Nothing
  387.  RsOpen = False
  388. End If
  389.  
  390. Exit Function
  391.  
  392. If ConnectionType = OnDemand Then
  393.  Set Cn = Nothing
  394. End If
  395. End Function
  396.  
  397. Private Sub DisconnectDB()
  398. On Error Resume Next
  399. 'UNTUK MENUTUP RECORDSET DAN KONEKSI KE DATABASE
  400.  
  401. If RsOpen = True Then
  402.  Set rs = Nothing
  403.  RsOpen = False
  404. End If
  405.  
  406. If RsOpen2 = True Then
  407.  Set Rs2 = Nothing
  408.  RsOpen2 = False
  409. End If
  410.  
  411. Cn.Close
  412. Set Cn = Nothing
  413.  
  414. Exit Sub
  415. End Sub
  416.  
  417.  
  418. 'Private Sub DisplayError()
  419. 'ConcatString = ""
  420. 'Dim ec As Integer
  421. '
  422. 'If Cn.Errors.Count > 0 Then
  423. ' For ec = 0 To Cn.Errors.Count - 1
  424. '  ConcatString = ConcatString & "DB Error Number: " & Cn.Errors(ec).Number & vbNewLine
  425. '  ConcatString = ConcatString & "     Source: " & Cn.Errors(ec).Source & vbNewLine
  426. '  ConcatString = ConcatString & "             " & Cn.Errors(ec).Description & vbNewLine
  427. '  ConcatString = ConcatString & vbNewLine
  428. ' Next
  429. '
  430. ' If TransOpen = True Then
  431. '  ConcatString = ConcatString & "ADO.RollBack" & vbNewLine
  432. '  ConcatString = ConcatString & "          Proses penyimpanan data diroll back telah terjadi error. Data tidak tersimpan."
  433. '  ConcatString = ConcatString & vbNewLine
  434. '  RollBackOccured = False
  435. ' End If
  436. 'Else
  437. ' ConcatString = ConcatString & "In Code Error Number: " & Err.Number & vbNewLine
  438. ' ConcatString = ConcatString & "          " & Err.Description & vbNewLine
  439. ' ConcatString = ConcatString & vbNewLine
  440. 'End If
  441. 'End Sub
  442. Private Sub Class_Terminate()
  443.  DisconnectDB
  444. End Sub
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.