Bonjour à tous,
j'utilise depuis un bon moment le code suivant pour récupérer des données du serveur central via excel.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Public Sub ReadFileI5Full(ByVal Input_File As String, ByVal Target_worksheet As String, ByVal Target_range As String)
'ReadFileI5Full : get I5 data from Lib.file(member) and insert that in Worksheet at désired range cell
'usage : ReadFileI5Full (File, Worksheet, Range)
' Input_File : can be either Lib.file
'                    or Lib.File(member)
' Target_Worksheet : Must be a valid Worksheet Name : if exists : cleared before fullfillment
'                                              if not exists : will be created
' Target_Range     : Must be a valid cell name : Example "A1"

    On Error GoTo ErrorHandler
 
    Dim cn          As New ADODB.Connection
    Dim rs          As New ADODB.Recordset
    Dim sConn       As String
    Dim SqlString   As String
    Dim FileName    As String
    Dim DSN_Name As String
    Dim ws          As String 'Results Storing Worksheet name
    Dim Rg          As String 'results Cells top left
    Dim cellule     As Range
    Dim CompA       As Integer
    Dim idx         As Long
    Dim sheet_found As Boolean
    Dim ActShName   As String
    

        
        
 
    sConn = "provider=IBMDA400;Data source=xxx.xxx.xxx.xxx;USER ID=TAGA;PASSWORD=DADA"
    FileName = Trim(Input_File)
    ws = Trim(Target_worksheet)
    Rg = Trim(Target_range)
        
    'check target Worsksheet exists and if not build
    sheet_found = False
    For idx = 1 To Sheets.Count
        If Sheets(idx).Name = ws Then
            Application.DisplayAlerts = False
            Sheets(idx).Cells.Clear
            Application.DisplayAlerts = True
            sheet_found = True
            Exit For
        End If
    Next idx
    'Build Ws
    If Not sheet_found Then
        ActShName = ActiveSheet.Name
        Sheets.Add
        ActiveSheet.Name = ws
        Sheets(ActShName).Activate
    End If
 
    'Open Connection
    cn.ConnectionString = sConn
    cn.Open
    SqlString = "SELECT * FROM " & FileName & ""
    
    'open record Set
    rs.Open SqlString, cn
    
    
    
    'Paste Fields names in line 1
    Set cellule = Worksheets(ws).Range(Rg)
    For CompA = 0 To rs.Fields.Count - 1
        cellule.Offset(0, CompA).Value = rs.Fields(CompA).Name
    Next CompA
    'Paste results line above names
    Worksheets(ws).Range(Rg).Offset(1, 0).CopyFromRecordset rs

    rs.Close
    cn.Close
    Set cn = Nothing
    Set rs = Nothing
 
    Exit Sub
ErrorHandler:
    ' clean up
    If Not cn Is Nothing Then
        If cn.State = adstateopen Then cn.Close
    End If
    Set cn = Nothing
    If Not rs Is Nothing Then
        If rs.State = adstateopen Then rs.Close
    End If
    If Err <> 0 Then
      MsgBox Err.Source & "-->" & Err.Description, , "Error"
    End If
 
End Sub
ce code fonctionne parfaitement bien sauf qu'il lui manque une clause Where dont j'ai impérativement besoin (avec plusieurs conditions).

Je n'ai pas pu trouver de doc ADO pour m'aider pour ce WHERE et j'ai fait tout un tas d'essais dont aucun ne fonctionne : le résultat de mon code est toujours l'ensemble des records du fichier source.

Si quelqu'un peut me donner un exemple qui fonctionne d'une méthode OPEN du recordset avec une clause WHERE (idéallement plusieurs conditions) cela me serait très utile.

Pour info, voici un exemple de la clause SELECT que je voudrais mettre en place
Code : Sélectionner tout - Visualiser dans une fenêtre à part
SELECT * FROM TRANSFERT.FCOL(FCOL) WHERE FCOL.CONCL=17495
Le fichier source est TRANSFERT.FCOL(FCOL)
La colonne à filtrer dans mon test est CONCL qui est numérique.

J'ai essayé tout un tas de syntaxe sans jamais aucun succès.

Merci d'avance.