Execution of VBA Access query

spartaboom Posted messages 14 Status Member -  
 Tessel75 -
Bonjour,
I am trying to execute a query already saved in my Access database called "Moins_Values_derives_Coefficientes ", and I want to run the execution of this query by passing the name of the query directly in my VBA code (without rewriting the SQL query). Is this possible?

Below is my code that does not work.
DoCmd.OpenQuery "Moins_Values_derives_Coefficientes
chemin = "path to my database", I added the instruction DoCmd.OpenQuery "Moins_Values_derives_Coefficientes" in my code, but that doesn't work either.
Thanks for your help

Dim db As DAO.Database
Dim q As QueryDef
Dim rs As DAO.Recordset

Set db = DAO.OpenDatabase(chemin, False, False)

Set q = db.QueryDefs("Moins_Values_derives_Coefficientes")
Set rs = db.OpenRecordset(q.Sql)

End Sub

I searched everywhere but couldn't find much (I am a beginner in VBA).

4 answers

  1. Tessel75
     
    Hello,
    I've never had a problem with writing:
    DoCmd.OpenQuery "[Blabblabla]"

    Be careful with all your quotes, you're missing one at the end of the statement you're presenting, and Access usually doesn't like spaces in object names very much, and it might be unnecessary to have a name as long and complicated as "Moins_Values_derives_Coefficientes", "MoinsValDerivesCoeff" would have done the job just as well without risking a writing mistake.
    Good luck
    3
  2. spartaboom Posted messages 14 Status Member
     
    Quand je lance l'exécution de mon code ci-dessous, une erreur s'affiche :
    Erreur d'exécution '424': objet requis
    en surlignant en jaune la ligne "Set q = CurrentDb.QueryDefs("MAXhistovl")"
    Merci pour votre réponse

    Sub bal()

    Dim db As DAO.Database
    Dim q As DAO.QueryDef
    Dim rs As DAO.Recordset

    Set q = CurrentDb.QueryDefs("MAXhistovl")
    Set q = Nothing

    End Sub
    0
  3. Tessel75
     
    Hello,
    What is "Error 424"? Your correspondents are not necessarily walking indexes of errors listed by Access. Furthermore, you don't explain what "MAXhistovl" is. In any case, I repeat: Be careful with spelling errors. This is probably the case.
    0