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 :

Odbc Excel et variable


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Mai 2015
    Messages
    54
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Technicien Help Desk

    Informations forums :
    Inscription : Mai 2015
    Messages : 54
    Par défaut Odbc Excel et variable
    Bonjour,
    J'utilise une connexion odbc afin de récupérer des informations sur un fichier Excel. Dans la requête SQL au moment du where j'ai une date (12/10/2017) j'aimerai créer une variable sur une cellule afin de la mettre directement sur ma requête SQL est-ce possible ?
    Cordialement

  2. #2
    Membre averti
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Mai 2015
    Messages
    54
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Technicien Help Desk

    Informations forums :
    Inscription : Mai 2015
    Messages : 54
    Par défaut
    Je n'arrive pas à créer une variable locale, il me dit que la requête n'est pas graphique...

  3. #3
    Invité
    Invité(e)
    Par défaut
    Bonsoir,

    Quel code utilises tu?

  4. #4
    Membre averti
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Mai 2015
    Messages
    54
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Technicien Help Desk

    Informations forums :
    Inscription : Mai 2015
    Messages : 54
    Par défaut
    Je vais sur autre base de données, je vais vers base odbc, ensuite j'utilise une connexion postgres vers une base de données sur un serveur postgres. Je clique sur SQL, je colle la requête que je souhaite exploiter, et dans le where je mets (test=?) Afin d'utiliser un Paramètre, une variable sur Excel, afin de mettre une date dans la variable. Car par défaut dans ma requête SQL c'est un current_date et j'aime remplacer par ma variable exel...

  5. #5
    Invité
    Invité(e)
    Par défaut
    Bonjour,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Sub test()
    Const adChar = 129, adParamOutput = 2, adCmdText = 1
    Sql = "Select * From MyTable Where [Test]=?;"
     With CreateObject("ADODB.Command")
        .ActiveConnection = Connexion
        .CommandType = adCmdText  'adCmdFile=256,adCmdStoredProc=4,adCmdTable=2,adCmdTableDirect=521,adCmdText=1,nknown=8
        .CommandTimeout = 500
         .CommandText = Sql
          .Parameters.Append  CreateObject("ADODB.Command").CreateParameter("Test", adChar, adParamOutput, 50, "TOTO") 'ChampsName,TypeChamps, Directionr, size, Value
        Set CommadExecute = .Execute
      End With
    End Sub

  6. #6
    Membre averti
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Mai 2015
    Messages
    54
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Technicien Help Desk

    Informations forums :
    Inscription : Mai 2015
    Messages : 54
    Par défaut
    merci a la place de mettre ma requete sql dans le connecteur odbc, je le mets dans le script vba ?

  7. #7
    Membre averti
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Mai 2015
    Messages
    54
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Technicien Help Desk

    Informations forums :
    Inscription : Mai 2015
    Messages : 54
    Par défaut
    Il a l'air de ne pas accepter ma requete....

    dois-je continuer d'utiliser ma commande dans les propriétés de connexion

    Nom : Sans titre.png
Affichages : 915
Taille : 9,6 Ko



    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
     
    Sub test()
    Const adChar = 129, adParamOutput = 2, adCmdText = 1
    Sql = "with q1 as (
    select 
      intervention.objet_id as id_interne_aurion, 
      evenement.debut AS "Début.Événement", 
      intervenant.ind_nom AS "Nom.Intervenant", 
      D1.valeur AS "Libellé.Module", 
      D2.valeur AS "Libellé.Type d'activité", 
      ressource.code AS "Code.Ressource", 
      evenement.fin AS "Fin.Événement", 
      D3.valeur AS "Libellé.Type de ressource"
    from (select o.* from v_interventions_filtrees_par_service o where true and not o.obsolete) intervention
    left join auriga.t_evenements evenement on evenement.objet_id = intervention.evenement_id
    left join auriga.t_intervenants_evenements R1 on R1.evenement_id = evenement.objet_id
    left join auriga.t_individus intervenant on intervenant.objet_id = R1.intervenant_id
    left join auriga.t_evenements_cours R2 on R2.evenement_id = evenement.objet_id
    left join auriga.t_cours cours on cours.objet_id = R2.cours_id
    left join auriga.t_relations R3 on R3.source_id = intervention.objet_id and R3.relation_nom = 'intervention-matiere'
    left join auriga.t_matieres matiere on matiere.objet_id = R3.dest_id
    left join auriga.t_relations R4 on R4.source_id = R3.rel_objet_id and R4.relation_nom = 'intervention-matiere-type_enseignement'
    left join auriga.t_types_activite type_enseignement on type_enseignement.objet_id = R4.dest_id
    left join auriga.t_interventions_ressources R5 on R5.intervention_id = intervention.objet_id
    left join auriga.t_ressources ressource on ressource.objet_id = R5.ressource_id
    left join auriga.t_types_ressource type_ressource on type_ressource.objet_id = ressource.type_ressource_id
    left join auriga.t_multilangues D1 on (D1.objet_id, D1.attribut_id, D1.langue_id) = (cours.objet_id, 561, 94577)
    left join auriga.t_multilangues D2 on (D2.objet_id, D2.attribut_id, D2.langue_id) = (type_enseignement.objet_id, 781, 94577)
    left join auriga.t_multilangues D3 on (D3.objet_id, D3.attribut_id, D3.langue_id) = (type_ressource.objet_id, 471, 94577)
    where (true
    and (evenement.debut >= current_date and evenement.debut < (current_date+1))
    )
    )
    select
    distinct
    id_interne_aurion, 
      q1."Nom.Intervenant", 
      q1."Libellé.Module", 
      q1."Libellé.Type d'activité", 
      (cast(cast(q1."Début.Événement" as timestamp) as time)) as "Seulement heure ,Début.Événement", 
      (cast(cast(q1."Fin.Événement" as timestamp) as time)) as "Seulement heure ,Fin.Événement", 
      q1."Code.Ressource", 
      q1."Libellé.Type de ressource"
    from q1
    where true /* and id_interne_aurion= any(('{' || $P{ObjectID_List} || '}')::integer[]) */;"
     With CreateObject("ADODB.Command")
        .ActiveConnection = Connexion
        .CommandType = adCmdText  'adCmdFile=256,adCmdStoredProc=4,adCmdTable=2,adCmdTableDirect=521,adCmdText=1,nknown=8
        .CommandTimeout = 500
         .CommandText = Sql
          .Parameters.Append  CreateObject("ADODB.Command").CreateParameter("Test", adChar, adParamOutput, 50, "TOTO") 'ChampsName,TypeChamps, Directionr, size, Value
        Set CommadExecute = .Execute
      End With
    End Sub

  8. #8
    Invité
    Invité(e)
    Par défaut
    c'est ce que je ferais!

  9. #9
    Invité
    Invité(e)
    Par défaut
    difficile je n'utilise jamais ça! je suis pas très féru en connexion purement Excel!

    j'utilise une connexion ADODB!

    Excel GenereCSTRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Base & ";Extended Properties=""Excel 12.0;HDR=" & Array("No", "YES")(Abs(AvecTitre)) & ";"""
    ACCESS97
    GenereCSTRING = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & Fichier
    ACCESS2000
    GenereCSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Fichier & ";Persist Security Info=False"
    ACCESS2012
    GenereCSTRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Fichier & ";"
    MySQL
    GenereCSTRING = " DRIVER={MySQL ODBC 5.1 Driver};SERVER=" & Server & ";UID=" & User & ";DATABASE=" & Base & ";Password=" & PassWord
    ODBC
    GenereCSTRING = "Provider=MSDASQL.1;Password=" & PassWord & ";Persist Security Info=True;User ID=" & User & ";Data Source=" & Base
    ORACLE
    GenereCSTRING = "Provider=OraOLEDB.Oracle.1;Password=" & PassWord & ";Persist Security Info=True;User ID=" & User & ";Data Source=" & Base
    SQLSERVER2005
    GenereCSTRING = "Provider=SQLOLEDB.1;Password=" & PassWord & ";Persist Security Info=True;User ID=" & User & ";Initial Catalog=" & Base & ";Data Source=" & Server
    SQLServer2008R2
    GenereCSTRING = "Provider=SQLNCLI;Server=" & Server & ";Database=" & Base & ";UID=" & User & ";PWD=" & PassWord & ";"
    SQLite
    GenereCSTRING = "Provider=OleSQLite.SQLiteSource.3; Data Source=" & Fichier
    GenereCSTRING = "Driver={SQLite ODBC (UTF-8) Driver};Database=" & Fichier & ";StepAPI=;Timeout="
    SQLite3
    GenereCSTRING = "Driver={SQLite3 ODBC Driver};Database=" & Fichier & ";LongNames=0;Timeout=4000;NoTXN=0;SyncPragma=NORMAL;StepAPI=0;"
    CSV
    GenereCSTRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Server & ";Extended Properties=""Text;HDR=" & Array("No", "YES")(Abs(AvecTitre)) & ";FMT=Delimited;"""
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Set Cn=CreateObject("ADODB.Connection")
    Cn.Open  GenereCSTRING
    Sql = "Select * From MyTable Where [Test]=?;"
     With CreateObject("ADODB.Command")
        .ActiveConnection = Cn
    'Etc...

Discussions similaires

  1. Réponses: 5
    Dernier message: 25/09/2019, 23h16
  2. migration Excel 2007 - variables personnelles non reconnues
    Par youri007 dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 26/12/2007, 22h31
  3. Requetes acces lancées depuis Excel avec variables
    Par jackfred dans le forum Requêtes et SQL.
    Réponses: 3
    Dernier message: 19/06/2007, 15h37
  4. Access, export de données vers Excel - avec Variable
    Par jackfred dans le forum VBA Access
    Réponses: 7
    Dernier message: 04/06/2007, 16h37
  5. [VBA excel] une variable pour 2 classeurs
    Par totoche dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 05/01/2006, 09h42

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