Advertisement
Jgug

VBA Services Form 01

Aug 29th, 2014
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Compare Database
  2.  
  3. Dim rsServicesID As Recordset
  4. Dim SoftAddMode As Boolean
  5. Dim HardAddMode As Boolean
  6.  
  7.  
  8. ' On Form Load ==============================================================================================================
  9. Private Sub Form_Load()
  10.     Dim db As DAO.Database
  11.     Dim strSQL As String
  12.  
  13.     Me.btnAddSoft.Enabled = False
  14.     Me.btnAddHard.Enabled = False
  15.     Me.btnDelSoft.Enabled = True
  16.     Me.btnDelHard.Enabled = True
  17.  
  18.     Set db = CurrentDb
  19.  
  20.     ' Query for all Services IDs
  21.    strSQL = "SELECT Services.[Service_ID] " & _
  22.              "FROM Services"
  23.              
  24.     Set rsServicesID = db.OpenRecordset(strSQL)
  25.     rsServicesID.MoveFirst
  26.    
  27.     ShowServiceDetails (rsServicesID.Fields("Service_ID").Value)
  28. End Sub
  29.  
  30.  
  31. ' Buttons for Software List  ================================================================================================
  32. ' Show full Software list
  33. Private Sub btnShowSoft_Click()
  34.     ShowFullList ("Software")
  35.     Me.btnAddSoft.Enabled = True
  36.     Me.btnDelSoft.Enabled = False
  37.     SoftAddMode = True
  38. End Sub
  39. ' Add selected Softwares to Service
  40. Private Sub btnAddSoft_Click()
  41.     AddSoftwareToService (rsServicesID.Fields("Service_ID").Value)
  42.     ShowServiceDetails (rsServicesID.Fields("Service_ID").Value)
  43.     Me.btnAddSoft.Enabled = False
  44. End Sub
  45. ' Delete selected Softwares to Service
  46. Private Sub btnDelSoft_Click()
  47.     DeleteSoftwareFromService (rsServicesID.Fields("Service_ID").Value)
  48.     ShowServiceDetails (rsServicesID.Fields("Service_ID").Value)
  49.     Me.btnAddSoft.Enabled = False
  50. End Sub
  51.  
  52.  
  53. ' Buttons for Hardware List  ================================================================================================
  54. ' Show full Hardware list
  55. Private Sub btnShowHard_Click()
  56.     ShowFullList ("Hardware")
  57.     Me.btnAddHard.Enabled = True
  58.     Me.btnDelHard.Enabled = False
  59.     HardAddMode = True
  60. End Sub
  61. ' Add selected Hardware to Service
  62. Private Sub btnAddHard_Click()
  63.     AddHardwareToService (rsServicesID.Fields("Service_ID").Value)
  64.     ShowServiceDetails (rsServicesID.Fields("Service_ID").Value)
  65.     Me.btnAddHard.Enabled = False
  66. End Sub
  67. ' Delete selected Hardware to Service
  68. Private Sub btnDelHard_Click()
  69.     DeleteHardwareFromService (rsServicesID.Fields("Service_ID").Value)
  70.     ShowServiceDetails (rsServicesID.Fields("Service_ID").Value)
  71.     Me.btnAddHard.Enabled = False
  72. End Sub
  73.  
  74.  
  75. ' Buttons for moving through Services =======================================================================================
  76. ' Next Service
  77. Private Sub btnNext_Click()
  78.     Me.btnAddSoft.Enabled = False
  79.     Me.btnAddHard.Enabled = False
  80.     Me.btnDelSoft.Enabled = True
  81.     Me.btnDelHard.Enabled = True
  82.  
  83.     If rsServicesID.RecordCount <> 0 Then
  84.         On Error Resume Next
  85.             rsServicesID.MoveNext
  86.             If Err.Number = 3021 Then
  87.                 MsgBox "Äîñòèãíóòà ïîñëåäíÿÿ çàïèñü"
  88.                 Err.Clear
  89.             Else
  90.                 ShowServiceDetails (rsServicesID.Fields("Service_ID").Value)
  91.             End If
  92.         On Error GoTo 0
  93.     End If
  94. End Sub
  95. ' Previous Service
  96. Private Sub btnPrevious_Click()
  97.     Me.btnAddSoft.Enabled = False
  98.     Me.btnAddHard.Enabled = False
  99.     Me.btnDelSoft.Enabled = True
  100.     Me.btnDelHard.Enabled = True
  101.    
  102.     If rsServicesID.RecordCount <> 0 Then
  103.         On Error Resume Next
  104.             rsServicesID.MovePrevious
  105.             If Err.Number = 3021 Then
  106.                 MsgBox "Äîñòèãíóòà ïåðâàÿ çàïèñü"
  107.                 Err.Clear
  108.             Else
  109.                 ShowServiceDetails (rsServicesID.Fields("Service_ID").Value)
  110.             End If
  111.         On Error GoTo 0
  112.     End If
  113. End Sub
  114.  
  115.  
  116. ' Buttons to add, delete and edit Service ===================================================================================
  117. ' Delete Service
  118. Private Sub btnDelete_Click()
  119.     DeleteService (rsServicesID.Fields("Service_ID").Value)
  120. End Sub
  121. ' Edit Service
  122. Private Sub btnChange_Click()
  123.     EditService (rsServicesID.Fields("Service_ID").Value)
  124.     ShowServiceDetails (rsServicesID.Fields("Service_ID").Value)
  125. End Sub
  126. ' Add Service
  127. Private Sub btnAdd_Click()
  128.     AddService
  129.     ShowServiceDetails (rsServicesID.Fields("Service_ID").Value)
  130. End Sub
  131.  
  132. ' User-defined Subs =========================================================================================================
  133. ' Show Service details
  134. Private Sub ShowServiceDetails(ID As Integer)
  135.     Dim db As DAO.Database
  136.     Dim rsSevice As DAO.Recordset
  137.     Dim rsServiceSoftware As DAO.Recordset
  138.     Dim rsServiceHardware As DAO.Recordset
  139.    
  140.     Set db = CurrentDb
  141.    
  142.     ' Query for Services
  143.    strSQL = "SELECT Services.[Service_ID], Services.[Service_name], Services.[Price] " & _
  144.              "FROM Services " & _
  145.              "WHERE Services.[Service_ID] = " & ID
  146.     Debug.Print strSQL
  147.    
  148.     Set rsSevice = db.OpenRecordset(strSQL)
  149.  
  150.     Me.textID = rsSevice.Fields("Service_ID").Value    ' Display ID
  151.    Me.sIDlist = rsSevice.Fields("Service_name").Value    ' Display Name
  152.    Me.textPrice = rsSevice.Fields("Price").Value    ' Display Price
  153.    
  154.     ' Query for Services/Software
  155.    strSQL = "SELECT [Services/Software].Service_ID, [Services/Software].Software_ID, Software.Software_name " & _
  156.              "FROM Software INNER JOIN [Services/Software] ON Software.Software_ID = [Services/Software].Software_ID " & _
  157.              "WHERE [Services/Software].Service_ID = " & ID
  158.     Debug.Print strSQL
  159.    
  160.     Set rsServiceSoftware = db.OpenRecordset(strSQL)
  161.     Set Me.listSoft.Recordset = rsServiceSoftware
  162.    
  163.     ' Query for Services/Hardware
  164.    strSQL = "SELECT [Services/Hardware].Service_ID, [Services/Hardware].Hardware_ID, Hardware.Hardware_name " & _
  165.              "FROM Hardware INNER JOIN [Services/Hardware] ON Hardware.Hardware_ID = [Services/Hardware].Hardware_ID " & _
  166.              "WHERE [Services/Hardware].Service_ID = " & ID
  167.     Debug.Print strSQL
  168.    
  169.     Set rsServiceHardware = db.OpenRecordset(strSQL)
  170.     Set Me.listHard.Recordset = rsServiceHardware
  171. End Sub
  172. ' Delete Service
  173. Private Sub DeleteService(ID As Integer)
  174.     Dim db As DAO.Database
  175.     Dim strSQL As String
  176.    
  177.     Set db = CurrentDb
  178.    
  179.     ' Delete from Services
  180.    strSQL = "DELETE FROM Services  " & _
  181.              "WHERE Services.Service_ID = " & ID
  182.     Debug.Print strSQL
  183.     DoCmd.RunSQL (strSQL)
  184.    
  185.     ' Delete from Services/Software
  186.    strSQL = "DELETE FROM [Services/Software] " & _
  187.              "WHERE [Services/Software].Service_ID = " & ID
  188.     Debug.Print strSQL
  189.     DoCmd.RunSQL (strSQL)
  190.    
  191.     ' Delete from Services/Hardware
  192.    strSQL = "DELETE FROM [Services/Hardware] " & _
  193.              "WHERE [Services/Hardware].Service_ID = " & ID
  194.     Debug.Print strSQL
  195.     DoCmd.RunSQL (strSQL)
  196. End Sub
  197. ' Edit Service
  198. Private Sub EditService(ID As Integer)
  199.     Dim db As DAO.Database
  200.     Dim strSQL As String
  201.    
  202.     Set db = CurrentDb
  203.  
  204.     ' Update Service_name & Price
  205.    strSQL = "UPDATE Services " & _
  206.              "SET Service_name = '" & Me.sIDlist.Value & "',  " & _
  207.              "Price = '" & Me.textPrice.Value & "' " & _
  208.              "WHERE Services.Service_ID = " & rsServicesID.Fields("Service_ID").Value
  209.     Debug.Print strSQL
  210.     DoCmd.RunSQL (strSQL)
  211. End Sub
  212. ' Add Service
  213. Private Sub AddService()
  214.     Dim db As DAO.Database
  215.     Dim strSQL As String
  216.    
  217.     Set db = CurrentDb
  218.    
  219.     strSQL = "INSERT INTO Services (Service_name, Price) " & _
  220.              "VALUES ('" & Me.sIDlist.Value & "', " & Me.textPrice.Value & ")"
  221.     Debug.Print strSQL
  222.     DoCmd.RunSQL strSQL
  223. End Sub
  224. ' Show full Software or Hardware list
  225. Private Sub ShowFullList(NAME As String)
  226.     Dim db As DAO.Database
  227.     Dim rsList As DAO.Recordset
  228.     Dim strSQL As String
  229.    
  230.     Set db = CurrentDb
  231.    
  232.     strSQL = "SELECT " & NAME & "." & NAME & "_ID, " & NAME & "." & NAME & "_ID, " & NAME & "." & NAME & "_name " & _
  233.              "FROM " & NAME
  234.     Debug.Print strSQL
  235.    
  236.     Set rsList = db.OpenRecordset(strSQL)
  237.     Select Case NAME
  238.         Case "Software"
  239.             Set Me.listSoft.Recordset = rsList
  240.         Case "Hardware"
  241.             Set Me.listHard.Recordset = rsList
  242.     End Select
  243. End Sub
  244. ' Add new Softwares to Service
  245. Private Sub AddSoftwareToService(ID As Integer)
  246.     Dim db As DAO.Database
  247.     Dim strSQL As String
  248.     Dim colSelects As Collection
  249.     Set colSelects = New Collection
  250.    
  251.     Set db = CurrentDb
  252.  
  253.     For Each i In Me.listSoft.ItemsSelected
  254.         colSelects.Add Me.listSoft.Column(1, i)
  255.     Next i
  256.     For i = 1 To colSelects.Count
  257.         Debug.Print "Collection Software ADD : " & colSelects.Item(i)
  258.         strSQL = "INSERT INTO [Services/Software] (Service_ID, Software_ID) " & _
  259.                  "VALUES (" & ID & ", " & colSelects.Item(i) & ")"
  260.         Debug.Print strSQL
  261.         DoCmd.RunSQL (strSQL)
  262.     Next
  263. End Sub
  264. ' Add new Hardwares to Service
  265. Private Sub AddHardwareToService(ID As Integer)
  266.     Dim db As DAO.Database
  267.     Dim strSQL As String
  268.     Dim colSelects As Collection
  269.     Set colSelects = New Collection
  270.    
  271.     Set db = CurrentDb
  272.  
  273.     For Each i In Me.listHard.ItemsSelected
  274.         colSelects.Add Me.listHard.Column(1, i)
  275.     Next i
  276.     For i = 1 To colSelects.Count
  277.         Debug.Print "Collection Hardware ADD : " & colSelects.Item(i)
  278.         strSQL = "INSERT INTO [Services/Hardware] (Service_ID, Hardware_ID) " & _
  279.                  "VALUES (" & ID & ", " & colSelects.Item(i) & ")"
  280.         Debug.Print strSQL
  281.         DoCmd.RunSQL (strSQL)
  282.     Next
  283. End Sub
  284. ' Delete selected Softwares from Service
  285. Private Sub DeleteSoftwareFromService(ID As Integer)
  286.     Dim db As DAO.Database
  287.     Dim strSQL As String
  288.     Dim colSelects As Collection
  289.     Set colSelects = New Collection
  290.    
  291.     Set db = CurrentDb
  292.    
  293.     For Each i In Me.listSoft.ItemsSelected
  294.         colSelects.Add Me.listSoft.Column(1, i)
  295.     Next i
  296.     For i = 1 To colSelects.Count
  297.         Debug.Print "Collection Software DEL : " & colSelects.Item(i)
  298.         strSQL = "DELETE FROM [Services/Software] " & _
  299.                  "WHERE Service_ID = " & ID & " AND Software_ID = " & colSelects.Item(i)
  300.         Debug.Print strSQL
  301.         DoCmd.RunSQL (strSQL)
  302.     Next
  303. End Sub
  304. ' Delete selected Hardwares from Service
  305. Private Sub DeleteHardwareFromService(ID As Integer)
  306.     Dim db As DAO.Database
  307.     Dim strSQL As String
  308.     Dim colSelects As Collection
  309.     Set colSelects = New Collection
  310.    
  311.     Set db = CurrentDb
  312.    
  313.     For Each i In Me.listHard.ItemsSelected
  314.         colSelects.Add Me.listHard.Column(1, i)
  315.     Next i
  316.     For i = 1 To colSelects.Count
  317.         Debug.Print "Collection Hardware DEL : " & colSelects.Item(i)
  318.         strSQL = "DELETE FROM [Services/Hardware] " & _
  319.                  "WHERE Service_ID = " & ID & " AND Hardware_ID = " & colSelects.Item(i)
  320.         Debug.Print strSQL
  321.         DoCmd.RunSQL (strSQL)
  322.     Next
  323. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement