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

Requêtes et SQL. Discussion :

Différence entre Date et Date()


Sujet :

Requêtes et SQL.

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre Expert Avatar de Gabout
    Homme Profil pro
    Utilisateur autodidacte
    Inscrit en
    Mai 2006
    Messages
    1 406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : France, Côtes d'Armor (Bretagne)

    Informations professionnelles :
    Activité : Utilisateur autodidacte

    Informations forums :
    Inscription : Mai 2006
    Messages : 1 406
    Par défaut Différence entre Date et Date()
    Bonjour,

    Je soumets à votre réflexion mon incompréhension :
    Je souhaite compter le nombre d'enregistrements d'une requête SQL par une autre requête SQL de façon à ne fonctionner qu'avec du code.

    la requête de comptage est la suivante (appelée Combien) :
    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
    Public Function Combien(strSql As String) As Long
        Dim oDb As DAO.Database
        Dim oRst As DAO.Recordset
        Set oDb = CurrentDb
        Set oRst = oDb.OpenRecordset(strSql, dbOpenDynaset)
        If oRst.BOF Then
            Combien = 0
        Else
            oRst.MoveLast
            Combien = oRst.RecordCount
        End If
        oRst.Close
        oDb.Close
        Set oRst = Nothing
        Set oDb = Nothing
    End Function
    Le lancement du décompte se fait sur click d'un bouton :
    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
    Private Sub CmdCombien_Click()
        Dim sSQL As String, NbEnreg As Long
        'Requête qui plante
        'sSQL = "SELECT tbl_UNITES.CUnite_ID, tbl_UNITES.Unite, tbl_AFFECTATIONS.Affectation, tbl_AGENTS.MAJ, tbl_AGENTS.Sortie, [Nom] & ' ' & PremMajMotsComp([Prenom]) AS Agent, tbl_GRADES.Grade, tbl_DEMANDES.CDemande_ID, tbl_CATEGORIES.Cat, tbl_CATEGORIES.Duree, tbl_DEMANDES.Formation, tbl_DEMANDES.Relance, DateSerial(Year([Formation]),Month([Formation])+[Duree],Day([Formation])) AS Validite " & _
               "FROM tbl_UNITES INNER JOIN (tbl_GRADES INNER JOIN (tbl_CATEGORIES INNER JOIN ((tbl_AFFECTATIONS INNER JOIN tbl_AGENTS ON tbl_AFFECTATIONS.CAffectation_ID = tbl_AGENTS.CAffectation) INNER JOIN tbl_DEMANDES ON tbl_AGENTS.Matricule_ID = tbl_DEMANDES.Matricule) ON tbl_CATEGORIES.CCategorie_ID = tbl_DEMANDES.CCategorie) ON tbl_GRADES.CGrade_ID = tbl_AGENTS.CGrade) ON tbl_UNITES.CUnite_ID = tbl_AFFECTATIONS.CUnite " & _
               "WHERE (((tbl_AGENTS.MaJ) = DMax('[MAJ]', 'tbl_AGENTS')) And ((tbl_AGENTS.Sortie) > Date) And ((tbl_CATEGORIES.Duree) > 0) And ((tbl_DEMANDES.Formation) Is Not Null) And ((tbl_DEMANDES.Relance) Is Null) And ((DateSerial(Year([Formation]), Month([Formation]) + [Duree], Day([Formation]))) < Date)) " & _
               "ORDER BY tbl_UNITES.Unite;"
     
        'Requête qui fonctionne
        sSQL = "SELECT tbl_UNITES.CUnite_ID, tbl_UNITES.Unite, tbl_AFFECTATIONS.Affectation, tbl_AGENTS.MAJ, tbl_AGENTS.Sortie, [Nom] & ' ' & PremMajMotsComp([Prenom]) AS Agent, tbl_GRADES.Grade, tbl_DEMANDES.CDemande_ID, tbl_CATEGORIES.Cat, tbl_CATEGORIES.Duree, tbl_DEMANDES.Formation, tbl_DEMANDES.Relance, DateSerial(Year([Formation]),Month([Formation])+[Duree],Day([Formation])) AS Validite " & _
               "FROM tbl_UNITES INNER JOIN (tbl_GRADES INNER JOIN (tbl_CATEGORIES INNER JOIN ((tbl_AFFECTATIONS INNER JOIN tbl_AGENTS ON tbl_AFFECTATIONS.CAffectation_ID = tbl_AGENTS.CAffectation) INNER JOIN tbl_DEMANDES ON tbl_AGENTS.Matricule_ID = tbl_DEMANDES.Matricule) ON tbl_CATEGORIES.CCategorie_ID = tbl_DEMANDES.CCategorie) ON tbl_GRADES.CGrade_ID = tbl_AGENTS.CGrade) ON tbl_UNITES.CUnite_ID = tbl_AFFECTATIONS.CUnite " & _
               "WHERE (((tbl_AGENTS.MAJ)=DMax('[MAJ]','tbl_AGENTS')) AND ((tbl_AGENTS.Sortie)>Date()) AND ((tbl_CATEGORIES.Duree)>0) AND ((tbl_DEMANDES.Formation) Is Not Null) AND ((tbl_DEMANDES.Relance) Is Null) AND ((DateSerial(Year([Formation]),Month([Formation])+[Duree],Day([Formation]))) < Date())) " & _
               "ORDER BY tbl_UNITES.Unite;"
     
        NbEnreg = Combien(sSQL)
        MsgBox (NbEnreg)
    End Sub
    J'ai affiché la requête qui plantait et la correction qui fonctionne. La seule différence est que j'ai du rajouter les deux parenthèses après Date... et c'est cela que je ne comprends pas !

    Peut-être ne direz vous que du moment que j'ai mon code qui fonctionne, je devrais être content mais je voudrais comprendre !!

    Une explication ?

  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
    Bonjour.

    En SQL, Date représente une variable et Date() la fonction date du jour.

    Bon courage,

    PGZ

  3. #3
    Membre Expert Avatar de Gabout
    Homme Profil pro
    Utilisateur autodidacte
    Inscrit en
    Mai 2006
    Messages
    1 406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : France, Côtes d'Armor (Bretagne)

    Informations professionnelles :
    Activité : Utilisateur autodidacte

    Informations forums :
    Inscription : Mai 2006
    Messages : 1 406
    Par défaut
    Bonjour pgz...

    et merci de la réponse, j'ai "ramé" pendant plusieurs soirées.

    A+

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 3
    Dernier message: 06/11/2014, 16h07
  2. [MCD] Différence entre un Attribut Date et une Entité Date ?
    Par hugouu dans le forum Schéma
    Réponses: 9
    Dernier message: 20/12/2009, 23h52
  3. différence entre 2 dates
    Par MANU_2 dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 12/09/2005, 16h30
  4. Différence entre deux dates
    Par pittzz dans le forum Oracle
    Réponses: 5
    Dernier message: 18/07/2005, 12h24
  5. Comment obtenir la différence entre deux dates ?
    Par jbat dans le forum Langage
    Réponses: 4
    Dernier message: 02/06/2005, 09h34

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