Bonjour au Forum,
Voici ma problématique : réaliser une requête sur deux fichiers distincts et fermés.
Au début, j'ai un seul fichier contenant deux onglets.
Onglet 'Scale' :
etc.
FILE_NUMBER PERSON_ID SCALE FEDERAL_STATE 2238239 1146534 S40 FRENCH_STATE 2235247 100004895785 S40 DUTCH_STATE 2235247 1698343 2235963 1483033 S42B GGC
Onglet 'Act' :
FILE_NUMBER PERSON_ID SOC_PROF_STATUS SOC_PROF_START_MONTH REASON_RIGHT_TYPE 48295 1146534 ACTIVE 195206 A56BIS1 2233955 100004895785 REFUSED 201607 G20071971 49007 1698343 ACTIVE 201803 NO_REASONRIGHT
Pour récupérer les données 'Refused' et 'S40', j'utilise ce code qui fonctionne très bien.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 Const DB_PATH = "H:\BackUp\Lionel B - 60106\Mod Excel\SQL\DB_Nom.xlsx" Public Const CONNSTR = "PROVIDER=MICROSOFT.ACE.OLEDB.12.0;" & _ "DATA SOURCE=" & DB_PATH & ";" & _ "Extended Properties=""Excel 12.0;HDR=YES;"";"
Comme je le disais, cela fonctionne très bien car c'était pour m'amuser. Mais, maintenant, je dois réaliser ce genre d'opération mais avec deux fichiers distincts : l'un contenant les données 'Scale' et l'autre contenant les données 'Act'.
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 Option Private Module Option Explicit Sub Comment_SQL() Sheets("Search").Range("a2").CurrentRegion.Select Selection.ClearContents 'settings connection DB Dim oConnection As New ADODB.Connection Dim oRecordset As New ADODB.Recordset Dim sSQL_Query As String: sSQL_Query = "SELECT [Act$].[FILE_NUMBER], [Act$].[PERSON_ID] " & _ "FROM [Act$] left join [Scale$] on [Scale$].[PERSON_ID] = [Act$].[PERSON_ID]" & _ "WHERE [SOC_PROF_STATUS] = 'REFUSED' and [SCALE]='S40'" Debug.Print sSQL_Query 'connection DB-------------------------------------------------------------------------------------- oConnection.Open CONNSTR oRecordset.Open sSQL_Query, oConnection, adOpenStatic, adLockOptimistic, adCmdText Dim nFound As Integer: nFound = oRecordset.RecordCount '#result If nFound <> 0 Then Sheets("Search").Range("A2").CopyFromRecordset oRecordset 'Entêtes de colonne Dim liCount As Integer For liCount = 0 To oRecordset.Fields.Count - 1 Sheets("Search").Cells(1, liCount + 1) = oRecordset.Fields(liCount).Name Next oRecordset.Close oConnection.Close Set oRecordset = Nothing Set oConnection = Nothing End Sub
J'ai bien essayé des petits trucs, des adaptations... mais rien ne fonctionne.
Quelqu'un aurait-il au moins un grand début de piste car même en regardant d'autres postes, je n'y arrive pas. MERCI.
Partager