0

My app is developed with Access.

I am trying to:

  • generate a select query
  • export the result in an Outlook mail (or at least in Excel)
  • destroy the query at the end so there are no duplicates

My code:

Private Sub Commande24_Click()
Dim db As Database
Dim Qdf As QueryDef

Dim strSQL As String    
Dim matr As Double

matr = DLookup("Matricule", "Employée", "Nom = '" & Me.Nom & "'")

strSQL = "SELECT Employée.Matricule, Employée.Département, Employée.Nom, Employée.Prénom, Employée.Grade, Employée.Silo, Entree.date_entree_g, Sortie.Date_sortie_e, Sortie.Type_s FROM (Employée INNER JOIN Entree ON Employée.N° = Entree.N_emp) INNER JOIN Sortie ON Employée.N° = Sortie.N_emp WHERE Employée.Matricule = '" & matr & "'"

Debug.Print sql
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Does this answer your question? [Exporting Recordset to Spreadsheet](https://stackoverflow.com/questions/16336025/exporting-recordset-to-spreadsheet). Can't export recordset to Outlook without looping through records and building a string to include in email body. Alternative for email is SendObject method that will attach a report as PDF. – June7 Nov 21 '19 at 19:28
  • Why would there be 'duplicates' - duplicate what? – June7 Nov 21 '19 at 21:56

1 Answers1

0

You dont have to write a single line of code to achieve this. Ms access using macros can do it.

  1. Create a delete query that will delete all values from the table
  2. Create a select query,for the values you want to export to excel
  3. Create a report using the select query as the record source
  4. Create a macro with two actions A. Export with formating( use the report name in 3 above as object name, and report as object type,output format-choose excel B Open query ,choose the delete query

Then attach the macro to a command button on click event.

Sola Oshinowo
  • 519
  • 4
  • 13