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
| Public Sub Connect()
Set cn = New ADODB.Connection
cn.Provider = "Microsoft.jet.oledb.4.0"
cn.Properties("Data Source") = App.Path & "C:\kante\exemple.mdb"
cn.Open
End Sub
Public Sub Close_Base()
cn.Close
Set cn = Nothing
End Sub
Private Sub Command1_Click()
' Déclaration des variables
Dim strTable, strSQL As String
Dim blnValide As Boolean
Dim intCode As Integer
Dim strnum, strnom As String
' Initialisation des variables ( + contrôle de saisie )
blnValide = True
' Note : le nom est une valeur obligatoire
If Trim(txtnum.Text) <> "" Then strnum = Trim(txtnum.Text) Else blnValide = False
If Trim(txtnom.Text) <> "" Then strnom = Trim(txtnom.Text)
'----------------------------------------------------------------------
' Si les valeurs sont correctement renseignées, on les ajoute à la table
If blnValide = True Then
strTable = "exemple"
' Correction des chaines avec apostrophes éventuels
' Note : les apostrophes, dans les requêtes SQL, peuvent provoquer des erreurs
strnum = Replace(strnum, "'", "''")
strnom = Replace(strnom, "'", "''")
'======================================================================
' AJOUTE LES DONNEES DANS LA TABLE
'----------------------------------------------------------------------
' Requête SQL d'insertion ( modulable selon les valeurs saisies ou pas )
strSQL = "INSERT INTO " & strTable & " ("
strSQL = strSQL & "num, nom"
If strnum <> "" Then strSQL = strSQL & ",num"
If strnom <> "" Then strSQL = strSQL & ",nom"
strSQL = strSQL & ") VALUES ("
strSQL = strSQL & strnum & "'"
If strnom <> "" Then strSQL = strSQL & ",'" & strnom & "'"
strSQL = strSQL & ")"
Else
MsgBox ("Données de saisies obligatoires manquantes..."), vbExclamation
End If
End Sub |
Partager