|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
MODULE ACCESS : REQUETESQL->QUERY->OUERTURE REPORT->EXPORTATION DU REPORT
Information sur la source
Description
bah ... Tu as une requetes SQL en chaine de caracteres (genre tu la generes dans un formulaire ...) Tu veux la stocker dans une Query (objet requete d'ACCESS) et ensuite ouvrir un report qui se refaire a la dite query, et exporter ce report Et bien tu appeles la fonction avec tout les arguments
Source
-
- Function EditReportFromQuery(StrSQL As String, Query_name As String, Report_Name As String, Filename As String, Format As String) As Boolean
- ' Edit the report named R_name after having repalce the query Q_name by the StrSQL
- ' if a param is NULL then try to ignore it ... or error
- ' return false if error
-
- '* Variable :
- Dim Q_select As QueryDef ' the querydef that will contains the "select" query (i.e a string)
- Dim query As String ' the "select" query
- Dim Q_name As String ' name of the query in this database
- Dim R_name As String ' name of the report
- Dim query_def As QueryDef
- Dim ook As Boolean
-
- '*Begin Sub
-
- On Error GoTo Erro
- If IsNull(Report_Name) Then
- MsgBox "no report name ...?", vbCritical
- End If
- If IsNull(Query_name) Then
- MsgBox "no query ...?", vbCritical
- EditReportFromQuery = False
- Exit Function
- End If
- R_name = Report_Name
- Q_name = Query_name
- If Not IsNull(StrSQL) Then
- ' create a query with this SQL string
- For Each query_def In CurrentDb.QueryDefs
- If query_def.Name = Q_name Then
- CurrentDb.QueryDefs.Delete (Q_name)
- Exit For
- End If
- Next query_def
- 'creation
- Set Q_select = CurrentDb.CreateQueryDef(Q_name, StrSQL)
- Else
- 'if this querydef existing ?
- ook = False
- For Each query_def In CurrentDb.QueryDefs
- If query_def.Name = Q_name Then
- ook = True
- Exit For
- End If
- Next query_def
- If ook = False Then
- MsgBox "no query ...?", vbCritical
- EditReportFromQuery = False
- Exit Function
- End If
- End If
-
- 'open the query as a table
- DoCmd.OpenQuery Q_name, acViewNormal
- 'open the correponding report
- DoCmd.OpenReport R_name, acViewPreview
- 'output to a txt file
- If Not IsNull(Filename) And Filename <> "NILL" Then
- Select Case Format
- Case "HTML"
- DoCmd.OutputTo acOutputReport, R_name, acFormatHTML, Filename, True
- Case "RTF"
- DoCmd.OutputTo acOutputReport, R_name, acFormatRTF, Filename, True
- Case "TXT"
- DoCmd.OutputTo acOutputReport, R_name, acFormatTXT, Filename, True
- Case "XLS"
- DoCmd.OutputTo acOutputReport, R_name, acFormatXLS, Filename, True
- End Select
- End If
-
- EditReportFromQuery = True
- Exit Function
-
- Erro:
- If Err.Number <> 2501 Then
- EditReportFromQuery = False
- MsgBox "EditReportFromQuery : " & Err.Description, vbCritical, Err.Number
- DoCmd.Close
- End If
- End Function
-
Function EditReportFromQuery(StrSQL As String, Query_name As String, Report_Name As String, Filename As String, Format As String) As Boolean
' Edit the report named R_name after having repalce the query Q_name by the StrSQL
' if a param is NULL then try to ignore it ... or error
' return false if error
'* Variable :
Dim Q_select As QueryDef ' the querydef that will contains the "select" query (i.e a string)
Dim query As String ' the "select" query
Dim Q_name As String ' name of the query in this database
Dim R_name As String ' name of the report
Dim query_def As QueryDef
Dim ook As Boolean
'*Begin Sub
On Error GoTo Erro
If IsNull(Report_Name) Then
MsgBox "no report name ...?", vbCritical
End If
If IsNull(Query_name) Then
MsgBox "no query ...?", vbCritical
EditReportFromQuery = False
Exit Function
End If
R_name = Report_Name
Q_name = Query_name
If Not IsNull(StrSQL) Then
' create a query with this SQL string
For Each query_def In CurrentDb.QueryDefs
If query_def.Name = Q_name Then
CurrentDb.QueryDefs.Delete (Q_name)
Exit For
End If
Next query_def
'creation
Set Q_select = CurrentDb.CreateQueryDef(Q_name, StrSQL)
Else
'if this querydef existing ?
ook = False
For Each query_def In CurrentDb.QueryDefs
If query_def.Name = Q_name Then
ook = True
Exit For
End If
Next query_def
If ook = False Then
MsgBox "no query ...?", vbCritical
EditReportFromQuery = False
Exit Function
End If
End If
'open the query as a table
DoCmd.OpenQuery Q_name, acViewNormal
'open the correponding report
DoCmd.OpenReport R_name, acViewPreview
'output to a txt file
If Not IsNull(Filename) And Filename <> "NILL" Then
Select Case Format
Case "HTML"
DoCmd.OutputTo acOutputReport, R_name, acFormatHTML, Filename, True
Case "RTF"
DoCmd.OutputTo acOutputReport, R_name, acFormatRTF, Filename, True
Case "TXT"
DoCmd.OutputTo acOutputReport, R_name, acFormatTXT, Filename, True
Case "XLS"
DoCmd.OutputTo acOutputReport, R_name, acFormatXLS, Filename, True
End Select
End If
EditReportFromQuery = True
Exit Function
Erro:
If Err.Number <> 2501 Then
EditReportFromQuery = False
MsgBox "EditReportFromQuery : " & Err.Description, vbCritical, Err.Number
DoCmd.Close
End If
End Function
Conclusion
c est hyper long , mais y a des appels de fonctions utiles ...
Sources du même auteur
Sources de la même categorie
Sources en rapport avec celle ci
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
ouverture d'un report access [ par matlepirate ]
HI Comment faut-il procéder pour ouvrir un état access sous VB ?J'arrive à l'imprimer mais pas à l'ouvrir ....Je ne trouve pas d'exemple..Thanks
ouverture d'un report access [ par matlepirate ]
HI Comment faut-il procéder pour ouvrir un état access sous VB ?J'arrive à l'imprimer mais pas à l'ouvrir ....Je ne trouve pas d'exemple..Thanks
access 2000 ouverture et edition de requete en code [ par Gauthier62 ]
Bonjour a tous, voila je vous demande de l'aide concernant mon probleme, je developpe sous access 2000 en vba et je suis ammener dans mon code a ouvri
erreur 2465 lors de l'ouverture d'un report access [ par zolt4n ]
Voila j'ai un programme qui ouvre un etat c'est bidon quand je clic sur un bouton jai ce code la Docmd.openReport "Etat_nom",AcViewPreview sa mar
ouverture d'un report access [ par risque ]
Slt comment ouvrir un report access a partir de VB
résultat d'une requete Access dans un fichier txt [ par lulusky ]
comment lire ligne par ligne le résultat d'une requete access pour pouvoir écrire plusieurs lignes dans un fichier txtlulusky
VB6 et Crystal report 10 [ par DOMBUG ]
Bonjour à tous,Je reprend une application, VB6, Access 2002 utilisant Crystal Report 8.5 afin de la faire évoluer vers une base MS SQL.Dans un permier
Requete parametree sous access [ par eulmaraud ]
Salut à tousun truc tout bête pour certains mais moi, ce soir j'y arrive po et je vais me coucher en ragej'ai, sous access, une requete parametree app
Vue paramétrée Access et VB [ par fairy22 ]
Bonjour, Après de nombreuses recherches sur internet, je n'ai toujours pas de réponse à mon problème...Voici mon souci : J'ai une requete parametrée q
requete access [ par chaudier37 ]
bonjourje travaille dans accessj'ai trois tablesune table sociétéavec une liste de sociétésune table métieravec une liste de métiersune table employéa
|
Téléchargements
Logiciels à télécharger sur le même thème :
Comparez les prix Nouvelle version
|