Code:
Const sConnect As String = "driver={SQL Server};server=[removed by admin];uid=[removed by admin];pwd=[removed by admin];database=[removed by admin]"
Public Sub UpdateSQL()
Dim Page As String
Dim Cellule As String
Dim Selection As String
Dim Exclusion As String
Dim Annee As String
Annee = Sheets("Paramêtres").Range("B3")
Dim x As Integer
With Sheets("Paramêtres")
For x = 10 To 108
Page = .Range("B" & x)
Cellule = .Range("C" & x)
Selection = .Range("D" & x)
Exclusion = .Range("E" & x)
Call Afficher(Page, Cellule, SelectionSQL(Selection, Exclusion) & Annee & "'")
Next x
End With
End Sub
Code:
Private Sub Afficher(Page As String, Cellule As String, SQL As String)
On Error Resume Next
Dim rs As ADODB.Recordset
Sheets(Page).Select
Set rs = New ADODB.Recordset
rs.Open SQL, sConnect, asOpenForwardOnly, adLockReadOnly
'Range(Cellule).Select
If rs.Fields(0).Value <> 0 Then
Range(Cellule) = CDec(rs.Fields(0).Value)
Else
Range(Cellule) = 0
End If
If CBool(rs.State And adStateOpen) Then rs.Close
Set rs = Nothing
End Sub
Code:
Private Function SelectionSQL(Selection As String, Exclusion As String) As String
Dim SQL As String
Dim Caractere As String
Dim chaine As String
Dim Increment As Integer
Increment = 1
SQL = "SELECT SUM(SLDE) FROM dbo.FIN_GLG WHERE (NO_CMPT like '"
'Calcul de la selection
Do While Increment <> Len(Selection) + 1
Caractere = Mid(Selection, Increment, 1)
If Caractere = "*" Then
chaine = chaine & "_"
ElseIf Caractere = "/" Then
SQL = SQL & chaine & "' OR NO_CMPT like "
chaine = "'"
Else
chaine = chaine & Caractere
End If
Increment = Increment + 1
Loop
SQL = SQL & chaine & "')"
'Calcul de l'exclusion
If Exclusion <> "" Then
chaine = ""
Caractere = ""
Increment = 1
SQL = SQL & " AND NOT NO_CMPT like '"
Do While Increment <> Len(Exclusion) + 1
Caractere = Mid(Exclusion, Increment, 1)
If Caractere = "*" Then
chaine = chaine & "_"
ElseIf Caractere = "/" Then
SQL = SQL & chaine & "' AND NOT NO_CMPT like "
chaine = "'"
Else
chaine = chaine & Caractere
End If
Increment = Increment + 1
Loop
SQL = SQL & chaine & "'"
End If
SQL = SQL & " AND EXER_FIN = '"
SelectionSQL = SQL
End Function
[Edited by admin]