Advertisement
Guest User

Untitled

a guest
Dec 30th, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 4.38 KB | Software | 0 0
  1. 'Delete Snippet using rs.find'
  2.  
  3. Private Sub cmdDelete_Click()
  4.     'Declare Variables
  5.    Dim index As Integer
  6.     Dim conn As ADODB.Connection
  7.     Dim rs As ADODB.Recordset
  8.     Dim answer As Integer
  9.     Dim selectedName as string
  10.  
  11.     'Set up Database Connection
  12.    Set conn = New ADODB.Connection
  13.     conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\SnippetsDatabase.mdb"
  14.  
  15.     index = lstSnippets.ListIndex
  16.  
  17.     'If Index is Valid
  18.    If index <> -1 Then
  19.  
  20.         'Prompt user
  21.        answer = MsgBox("Do you want delete this Snippet?", vbQuestion + vbYesNo, App.Title)
  22.         'If user Clicked Yes
  23.        If answer = vbYes Then
  24.  
  25.             'Get the currently selected text from the lstSnippets Listbox. If this is not the snipped name then this won't work!
  26.            selectedName = lstSnippets.Text
  27.  
  28.             lblSnippetNamePreview.Caption = "Snippet Name: "
  29.             lblSnippetLangPreview.Caption = "Snippet Language: "
  30.             txtSnippetCodePreview.Text = ""
  31.  
  32.             'Open Database connection
  33.            conn.Open
  34.  
  35.             'Open Table
  36.            Set rs = New ADODB.Recordset
  37.             rs.Open "tblSnippets", conn, adOpenKeyset, adLockOptimistic
  38.            
  39.             Do While not rs.eof
  40.                 rs.Find "[Snippet_Name] ='" & selectedName & "' AND [Snippet_Lang] = '" & Snippet_Lang & "'"
  41.                 if not rs.eof then
  42.                     rs.Delete
  43.                 end if
  44.             Loop
  45.             rs.Close
  46.             conn.Close
  47.  
  48.             'Cleanup
  49.            Set rs = Nothing
  50.             Set conn = Nothing
  51.  
  52.             'Update Form
  53.            lstSnippets.RemoveItem index
  54.             MsgBox "Snippet deleted sucessfully", vbInformation + vbOKOnly, App.Title
  55.         End If
  56.     End If
  57. End Sub
  58.  
  59.  
  60. 'Delete Snippet using ado command
  61.  
  62. Private Sub cmdDelete_Click()
  63.     'Declare Variables
  64.    Dim index As Integer
  65.     Dim conn As ADODB.Connection
  66.     Dim rs As ADODB.Recordset
  67.     Dim sqlcmd as ADODB.Command
  68.     Dim answer As Integer
  69.     Dim selectedName as string
  70.  
  71.     'Set up Database Connection
  72.    Set conn = New ADODB.Connection
  73.     conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\SnippetsDatabase.mdb"
  74.  
  75.     index = lstSnippets.ListIndex
  76.  
  77.     'If Index is Valid
  78.    If index <> -1 Then
  79.  
  80.         'Prompt user
  81.        answer = MsgBox("Do you want delete this Snippet?", vbQuestion + vbYesNo, App.Title)
  82.         'If user Clicked Yes
  83.        If answer = vbYes Then
  84.  
  85.             'Get the currently selected text from the lstSnippets Listbox. If this is not the snipped name then this won't work!
  86.            selectedName = lstSnippets.Text
  87.  
  88.             lblSnippetNamePreview.Caption = "Snippet Name: "
  89.             lblSnippetLangPreview.Caption = "Snippet Language: "
  90.             txtSnippetCodePreview.Text = ""
  91.  
  92.             'Open Database connection
  93.            conn.Open
  94.  
  95.             'Create SqlCmd
  96.            set sqlcmd = new ADODB.Command
  97.  
  98.             'Command text is the SQL query to run. We want to DELETE the rows from tblSnippets where the Columns Snippet_Name and Snippet_Lang match what we selected in the form
  99.            '? - a placeholder for a parameter
  100.  
  101.             with sqlcmd
  102.                 .CommandText = "DELETE FROM tblSnippets WHERE [Snippet_Name] = ? AND [Snippet_Lang] = ?"
  103.                 'Add in selectedName and Snippet_Lang as parameters
  104.                'Append a Text Parameter as an Input, with a Max Length of 255, with value of selectedName
  105.                .Parameters.Append .CreateParameters(,adVarChar, adParamInput, 255, selectedName)
  106.                 'Append a Text Parameter as an Input, with a Max Length of 255, with value of Snipped_Lang
  107.                .Parameters.Append .CreateParameters(,adVarChar, adParamInput, 255, Snippet_Lang)
  108.                 'Set Sqlcmd as prepared
  109.                .Prepared = True
  110.                 'Set the connection to the one we just opened
  111.                .ActiveConnection = conn
  112.                 'Run the query
  113.                .Execute
  114.             end with
  115.  
  116.             'Cleanup
  117.            Set rs = Nothing
  118.             set sqlcmd = Nothing
  119.             Set conn = Nothing
  120.  
  121.             'Update Form
  122.            lstSnippets.RemoveItem index
  123.             MsgBox "Snippet deleted sucessfully", vbInformation + vbOKOnly, App.Title
  124.         End If
  125.     End If
  126. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement