IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Connexion à Sql server 2000


Sujet :

Macros et VBA Excel

  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    829
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 829
    Par défaut Connexion à Sql server 2000
    Bonjour à tous,

    J'aurai voulu savoir si il est possible de réaliser une connexion en vba/macro a un serveur SQL 2000, puis de réaliser une requête qui récupèrer la condition dans une cellule ( ex: select id from toto Where age='&A1&' and taille='&G2&')


    Et renvoyé le résultat dans excel dans une feuille.

    Est-ce possible? Auriez-vous des exemples, des tutos .

    Merci d'avance pour votre aide

    guigui69

  2. #2
    pgz
    pgz est déconnecté
    Expert confirmé Avatar de pgz
    Homme Profil pro
    Développeur Office VBA
    Inscrit en
    Août 2005
    Messages
    3 692
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 72
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Office VBA
    Secteur : Conseil

    Informations forums :
    Inscription : Août 2005
    Messages : 3 692
    Par défaut
    Bonsoir.

    Pour bien commencer : le cours de Sebastien Curutchet.

    Au chap 2, les connections aux différentes BD, dont SQL Server.

    Bonne lecture

    PGZ

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2006
    Messages
    829
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2006
    Messages : 829
    Par défaut
    Bonjour,

    Voici mon code:

    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
    Sub Macro1()
     
    Dim valcel As String
    valcel = Excel.Range("Informations!B4").Value
    Dim cnBat As ADODB.Connection
    Set cnBat = New ADODB.Connection
     MsgBox (valcel)
    Dim strConn As String
    strConn = "PROVIDER=SQLOLEDB;"
    strConn = strConn & "DATA SOURCE=****;UID=*******;PWD=*****;DATABASE=i*****"
    cnBat.Open strConn
    '------------------
    Dim rsBat As ADODB.Recordset
    Set rsBat = New ADODB.Recordset
     
    With rsBat
         .ActiveConnection = cnBat
         .Open "SELECT * FROM tgEmpresa WHERE emp = '" & valcel & " '"
     
         Feuil3.Range("A2").CopyFromRecordset rsBat
       '----  MsgBox ("SELECT * FROM tgEmpresa WHERE emp = '" & valcel & " '")
     
        .Close
    End With
     
    cnBat.Close
    Set rsBat = Nothing
    Set cnBat = Nothing
    End Sub
    Cela fonctionne, mais j'ai des requetes qui sont tres long

    Exemple:

    Code sql : 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
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    SELECT FechaAperturaOT,ttOTCab.NumOT as nor, 
      ttOTCab.Matric as immat, 
      ttotcab.anoot,
      ttotcab.chasis,
      ttotcab.km,
    (SELECT coalesce (sum (ttotfichado.Tiempopasado),0)
         FROM  ttotfichado
         WHERE ttotfichado.hayabono<> 1 AND
               (NOT (ttOTFichado.RepartoPendiente = 0 AND ttOTFichado.FichajeContraOT > 0) OR ttOTFichado.RepartoPendiente IS NULL) AND
               ttOTIntervencion.Emp = ttOTFichado.Emp AND
               ttOTIntervencion.Numinterno = ttOTFichado.Numinterno AND  
               ttOTIntervencion.Intervencion = ttOTFichado.Intervencion AND
               ttotfichado.fecha <= '12-1-2011 23:59:59.000') AS tt, 
      Count(DISTINCT ttOTCab.NumOT),
      SUM(case tipocab
        when 0 then
          case tipolinea
            when 1 then
              case ttOTIntervencion.esabono
                when 1 then (-1 * ttotlinea.PrecioCosteMedio * coalesce (ttotcargolinea.cantidadhoras,ttotlinea.cantidadhoras))
                else             (ttotlinea.PrecioCosteMedio * coalesce (ttotcargolinea.cantidadhoras,ttotlinea.cantidadhoras))
              end
            else 0
          end	
        else 0
      end) as ImpPCPMrec,
      tgtaller.descrip as atelier,
      tgempresa.razon as societe
     
    FROM ttoTCab
     
      inner join tgempresa on
      ttotcab.emp = tgempresa.emp 
     
      inner join tgtaller on
      ttotcab.emp = tgtaller.emp and ttotcab.taller = tgtaller.taller
     
      INNER JOIN ttOTIntervencion ON 
      ttOTCab.Emp = ttOTIntervencion.Emp AND 
      ttOTCab.NumInterno = ttOTIntervencion.Numinterno 
     
      INNER JOIN ttOTLinea ON 
      ttOTIntervencion.Emp = ttOTLinea.Emp AND 
      ttOTIntervencion.Numinterno = ttOTLinea.NumIntOT AND 
      ttOTIntervencion.Intervencion = ttOTLinea.NumIntIntervencion 
     
      INNER JOIN ttSeccion ON 
      ttOTCab.Seccion = ttSeccion.Seccion 
     
      INNER JOIN ttSeccion seccion ON 
      ttOTIntervencion.Seccion = seccion.Seccion 
     
      LEFT OUTER JOIN TTrecepcionista ON
      ttOtCab.emp = TTrecepcionista.emp and
      ttOtCab.taller = TTrecepcionista.taller and 
      ttOtCab.recepcionista = TTrecepcionista.recepcionista
     
      LEFT OUTER JOIN Tgmarca ON
      ttOtCab.marca = tgmarca.marca
     
      LEFT OUTER JOIN ttOTCargoLinea ON 
      ttOTLinea.Emp = ttOTCargoLinea.Emp AND
      ttOTLinea.NumIntOT = ttOTCargoLinea.NumIntOT AND 
      ttOTLinea.NumIntIntervencion = ttOTCargoLinea.NumIntIntervencion AND
      ttOTLinea.LineaItem = ttOTCargoLinea.LineaItem 
     
      LEFT OUTER JOIN ttOTCargoInt on
      ttOTCargoLinea.Emp = ttotcargoint.emp AND
      ttOTCargoLinea.NumIntOT = ttotcargoint.Numintot AND 
      ttOTCargoLinea.NumIntIntervencion = ttotcargoint.numintintervencion AND
      ttOTCargoLinea.NumIntcargo = ttotcargoint.numintcargo 
     
      LEFT OUTER JOIN ttotcargo on
      ttotcargoint.emp=ttotcargo.emp and 
      ttotcargoint.NumIntOT=ttotcargo.NumInterno and
      ttotcargoint.NumIntCargo=ttotcargo.Cargo 
     
      LEFT OUTER JOIN ttgrupocargo on
      ttOTCargo.grupocargo = ttgrupocargo.grupo
     
      LEFT OUTER JOIN tgGrupoCont ON 
      ttOTLinea.GrupoCont = tgGrupoCont.GrupoCont
     
      LEFT OUTER JOIN tgMarca MarcaLinea ON 
      ttOTLinea.Marca = MarcaLinea.Marca
     
      LEFT OUTER JOIN tacategoriapieza ON	
      ttotlinea.marca = tacategoriapieza.marca and ttotlinea.codigocategoria 	= tacategoriapieza.codigo
     
      LEFT OUTER JOIN ttotstatus 	  ON ttOTCab.statusencurso = ttotstatus.Codigo
     
    WHERE   
    	(ttotintervencion.hayfactura = 0 ) and
    	(ttOTCab.Anulada = 0) and 
    	(ttOTLinea.Anulada = 0) AND 
       (ttOTCab.FechaAperturaOT <= '12-1-2011 23:59:59.000' ) AND  
    	(
    	   ttOTLinea.FechaAlta <= '12-1-2011 23:59:59.000'
    		or ttotlinea.tipocab =1
    	) and 	
    	(ttotlinea.tipocab <> 2) and 
       (ttOTCab.Status = 30 )  and
    (ttotlinea.cantidadhoras <> 0 or ttotlinea.almservido <> 1)
    group by FechaAperturaOT, tgempresa.razon, tgtaller.descrip, ttOTIntervencion.Emp, ttOTIntervencion.Numinterno, ttOTIntervencion.Intervencion, ttOTCab.NumOT, ttOTCab.Matric, ttotcab.anoot, ttotcab.chasis, ttotcab.km
    UNION   ALL
     
    SELECT FechaAperturaOT,ttOTCab.NumOT as nor, 
      ttOTCab.Matric as immat, 
      ttotcab.anoot,
      ttotcab.chasis,
      ttotcab.km,
    (SELECT coalesce (sum (ttotfichado.Tiempopasado),0)
         FROM  ttotfichado
         WHERE ttotfichado.hayabono<> 1 AND
               (NOT (ttOTFichado.RepartoPendiente = 0 AND ttOTFichado.FichajeContraOT > 0) OR ttOTFichado.RepartoPendiente IS NULL) AND
               ttOTIntervencion.Emp = ttOTFichado.Emp AND
               ttOTIntervencion.Numinterno = ttOTFichado.Numinterno AND  
               ttOTIntervencion.Intervencion = ttOTFichado.Intervencion AND
               ttotfichado.fecha <= '12-1-2011 23:59:59.000') AS tt, 
      Count(DISTINCT ttOTCab.NumOT),
     SUM( case tipocab
        when 0 then
          case tipolinea
            when 1 then
              case ttOTIntervencion.esabono
                when 1 then (-1 * ttotlinea.PrecioCosteMedio * coalesce (ttotcargolinea.cantidadhoras,ttotlinea.cantidadhoras))
                else             (ttotlinea.PrecioCosteMedio * coalesce (ttotcargolinea.cantidadhoras,ttotlinea.cantidadhoras))
              end
            else 0
          end
        else 0
      end) as ImpPCPMrec,
     
        tgtaller.descrip as atelier,
      tgempresa.razon as societe
    FROM     
      ttOTCab
        LEFT OUTER JOIN tgmarca ON ttotcab.marca = tgmarca.marca
    	 LEFT OUTER JOIN ttotstatus 	  ON ttOTCab.statusencurso = ttotstatus.Codigo
        LEFT OUTER JOIN ttrecepcionista ON ttOtCab.emp = TTrecepcionista.emp  and 
      ttOtCab.taller = TTrecepcionista.taller  and 
      ttOtCab.recepcionista = TTrecepcionista.recepcionista, 
      ttOTCargo, 
      ttOTCargoInt, 
      ttOTCargoLinea, 
      ttOTIntervencion,
      ttOTLinea
        LEFT OUTER JOIN tgGrupoCont ON tgGrupoCont.GrupoCont = ttOTLinea.GrupoCont
        LEFT OUTER JOIN tgMarca MarcaLinea ON ttOTLinea.Marca = MarcaLinea.Marca
    	 LEFT OUTER JOIN tacategoriapieza ON ttotlinea.marca = tacategoriapieza.marca and ttotlinea.codigocategoria = tacategoriapieza.codigo,
      ttSeccion, ttOTFac, tgtaller, tgempresa, ttgrupocargo, ttSeccion seccion
     
    WHERE 
     
      ttOTCargo.grupocargo = ttgrupocargo.grupo and 
      ttotfac.abono<> 1 and	
      ttotfac.fechafactura > '12-1-2011 23:59:59.000' and 
      ttotcab.emp = tgempresa.emp and 
      ttotcab.emp = tgtaller.emp and 
      ttotcab.taller = tgtaller.taller and 
      ttotcargo.emp = ttotfac.emp and 
      ttotcargo.NumInterno = ttotfac.NumInterno and 
      ttotcargo.Cargo = ttotfac.NumIntCargo and 
      ttotcargoint.emp = ttotcargo.emp and 
      ttotcargoint.NumIntOT = ttotcargo.NumInterno and 
      ttotcargoint.NumIntCargo = ttotcargo.Cargo and 
      ttotcargoLinea.emp = ttotcargoint.emp and 
      ttotcargoLinea.NumIntOT = ttotcargoint.NumIntOT and 
      ttotcargoLinea.NumIntCargo = ttotcargoint.NumIntCargo and 
      ttotcargoLinea.NumIntIntervencion = ttotcargoint.NumIntIntervencion and 
      ttotlinea.emp = ttotcargoLinea.emp and 
      ttotlinea.NumIntOT = ttotcargoLinea.NumIntOT and 
      ttotlinea.NumIntIntervencion = ttotcargoLinea.NumIntIntervencion and 
      ttotlinea.LineaItem=ttotcargoLinea.LineaItem and 
        (
    	  	ttOTLinea.FechaAlta <= '12-1-2011 23:59:59.000'  
    		or 	ttotlinea.tipocab =1
    	 ) and 
      ttotlinea.tipocab <> 2 and 
      ttotintervencion.emp = ttotlinea.emp and 
      ttotintervencion.Numinterno = ttotlinea.NumIntOT and 
      ttotintervencion.Intervencion = ttotlinea.NumIntIntervencion and 
      ttOTCab.Seccion = ttSeccion.Seccion and 
      ttOTIntervencion.Seccion = Seccion.SECCION and 
      ttotcab.emp = ttotintervencion.emp and 
      ttotcab.numinterno = ttotintervencion.Numinterno and
      ttotcab.fechaaperturaOT <= '12-1-2011 23:59:59.000' and
      ttotcab.status >=30  and
    (ttotlinea.cantidadhoras <> 0 or ttotlinea.almservido <> 1)
    group by FechaAperturaOT, tgempresa.razon, tgtaller.descrip, ttOTIntervencion.Emp, ttOTIntervencion.Numinterno, ttOTIntervencion.Intervencion, ttOTCab.NumOT, ttOTCab.Matric, ttotcab.anoot, ttotcab.chasis, ttotcab.km

    Comment faire pour les rajouter cette requete dans mon VBA le plus simple possible? Car je voudrait eviter de rajouter " & _ chaque fin de ligne .

    Merci

    guigui69

Discussions similaires

  1. Propriétés de connexions SQL Server 2000
    Par Te-Deum dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 20/04/2007, 11h50
  2. Connexion à SQL Server 2000 avec Visual Basic 6
    Par method_man dans le forum VB 6 et antérieur
    Réponses: 1
    Dernier message: 06/03/2007, 11h32
  3. pb connexion sql server 2000 avec ODBC
    Par prophete3d dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 21/11/2005, 17h19
  4. connexion SQL SERVER 2000
    Par christel1982 dans le forum ASP
    Réponses: 7
    Dernier message: 26/10/2005, 15h28
  5. [C#] Problème de connexion sql server 2000
    Par rabbiwan dans le forum ASP.NET
    Réponses: 8
    Dernier message: 22/12/2004, 16h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo