1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| Option Compare Database
Option Explicit
Private Sub cmdFillRightList_Click()
Dim strSelection As String
  If IsNull(Me!lstTempTableSource) Then Exit Sub
  strSelection = Me!lstTempTableSource
  If NotAlreadyExists(strSelection) Then
    AddNewItem strSelection, "TBLTempDestination", "TBLTempSource"
    lstTempTableDestination.RowSource = "TBLTempDestination"
  Else
    MsgBox "Élément déjà ajouté !", 48
  End If
End Sub
Private Sub Form_Load()
Dim SQLDelete As String
Dim SQLInsert As String
  SQLDelete = "DELETE * FROM TBLTempSource"
  CurrentDb.Execute SQLDelete, dbSeeChanges
  SQLInsert = "INSERT INTO TBLTempSource ( [Code catégorie], " & _
  "[Nom de catégorie], Description,  Illustration ) SELECT " & _
  "Catégories.[Code catégorie], Catégories.[Nom de catégorie], " & _
  "Catégories.Description, Catégories.Illustration FROM Catégories;"
  CurrentDb.Execute SQLInsert, dbSeeChanges
  SQLDelete = "DELETE * FROM TBLTempDestination"
  CurrentDb.Execute SQLDelete, dbSeeChanges
  lstTempTableSource.Requery
End Sub
Private Function NotAlreadyExists(ByVal ItemToCheck As String) As Boolean
Dim oRS As Recordset
Dim SQLSelect As String
  SQLSelect = "SELECT * FROM TBLTempDestination WHERE ItemSelected = " & Chr(34) & _
  ItemToCheck & Chr(34)
  Set oRS = CurrentDb.OpenRecordset(SQLSelect, 2)
    NotAlreadyExists = oRS.EOF
    oRS.Close
  Set oRS = Nothing
End Function
Private Sub AddNewItem(ByVal WhatToAdd As String, ByVal TargetTable As String, _
ByVal SourceTable As String)
Dim SQLInsert As String
Dim SQLDelete As String
  
  SQLInsert = "INSERT INTO " & TargetTable & " (ItemSelected) VALUES (" & _
  Chr(34) & WhatToAdd & Chr(34) & ");"
  CurrentDb.Execute SQLInsert, dbSeeChanges
  lstTempTableDestination.Requery
  SQLDelete = "DELETE * FROM " & SourceTable & " WHERE [Nom de catégorie] = " & _
  Chr(34) & WhatToAdd & Chr(34) & ";"
  CurrentDb.Execute SQLDelete, dbSeeChanges
  lstTempTableSource.Requery
End Sub |
Partager