Comment exporter les résultats de l'instruction SQL vers un file Excel

J'ai un Access DataBase et un formulaire dans Excel VBA. Toutes datatables que j'ai inputs dans le DB sont inputs via le formulaire VBA.

Ce DB contient toutes les maps de bénéfices déjà reçues cette année dans l'entreprise. Mais le même employé peut requestr la carte deux fois ou plus, alors nous aurons plus d'un logging sur la database pour lui.

Ce dont j'ai besoin, c'est lorsque le nombre d'loggings est supérieur à un, le résultat de l'instruction SQL devrait apparaître dans un rapport Excel.

J'utilise l' SELECT (*) COUNT pour savoir quand il y a plus d'un logging compatible avec le critère de search. Mais je ne peux pas faire apparaître le résultat dans un file Excel.

Voici mon code:

 Public Function Relatorio() Dim sql As Ssortingng Dim cn As ADODB.Connection Dim rs As ADODB.Recordset Dim rel As Ssortingng Set cn = New ADODB.Connection cn.ConnectionSsortingng = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & enderecoDB & ";Jet OLEDB:Database" cn.Open Set rs = New ADODB.Recordset sql = "INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=" & enderecoDB & ";', 'SELECT * FROM [Planilha1$]') SELECT * FROM controle WHERE BP = " & controlectform.nmbpbox.Value & ";" rs.Open sql, cn End Function 

Lorsque je lance ce code, il me donne un message en disant quelque chose comme:

Impossible de localiser la sortie de la table OPENROWSET

Je ne suis pas en mesure d'installer de nouveaux programmes, donc je dois le faire en utilisant uniquement Excel VBA et Access DB.

Comment puis-je faire en sorte que cela fonctionne?

    Je ne crois pas que Access prend en charge la table dynamic OPENROWSET avec laquelle vous travaillez. J'ai beaucoup d'anciens projets qui font cela, alors voici ma méthode

     Public Function Relatorio() Dim sql As Ssortingng Dim cn As ADODB.Connection Dim rs As ADODB.Recordset Dim rel As Ssortingng Set cn = New ADODB.Connection cn.ConnectionSsortingng = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & enderecoDB & ";Jet OLEDB:Database" cn.Open Set rs = New ADODB.Recordset dim path_To_XLSX dim name_of_sheet path_To_XLSX = "c:\temp\output.xlsx" name_of_sheet = "Planilha1" sql = sql = "SELECT * INTO [Excel 12.0;Database=" & path_To_XLSX & "]." & name_of_sheet & " FROM controle WHERE BP = '" & controlectform.nmbpbox.Value & "';" rs.Open sql, cn 'If this application is in an unsecure environment, use the following code instead! This is to prevent a SQL injection, security concern here. 'As it is an Access Database, this is likely overkill for this project 'Create Command Object. Set Cmd1 = New ADODB.Command Cmd1.ActiveConnection = cn cmd1.CommandText = "SELECT * FROM controle INTO [Excel 12.0;Database=" & path_To_XLSX & "]." & name_of_sheet & " WHERE BP = ?" ' Create Parameter Object. Set Param1 = Cmd1.CreateParameter(, adInteger, adParamInput, 5) 'use adVarchar for ssortingngs(versus adInteger), https://www.w3schools.com/asp/met_comm_createparameter.asp Param1.Value = controlectform.nmbpbox.Value Cmd1.Parameters.Append Param1 Set Param1 = Nothing Set Rs = Cmd1.Execute() End Function 

    J'ai eu ce défi il y a tellement d'années que je ne me souviens plus, mais ce lien sonne. vérifiez si cela vous aide.

    https://stackoverflow.com/a/28889774/382588

    essayez {connw.Open (); Commande OleDbCommand; command = new OleDbCommand ("Mise à jour des livraisons" + "SET Deliveries.EmployeeID =?, Deliveries.FIN =?, Livraisons.TodaysOrders =?, connw); command.Parameters.Add (nouvel OleDbParameter (" @ EMPID ", Convert.ToDecimal (empsplitIt [1]))); command.Parameters.Add (nouvel OleDbParameter ("@ FIN", truckSplit [1] .ToSsortingng ())); command.Parameters.Add (nouvel OleDbParameter ("@ TodaysOrder", "R" ")); catchReturnedRows = command.ExecuteNonQuery (); // Commit connw.Close ();} catch (exception OleDbException) {MessageBox.Show (exception.Message," OleDb Exception ");}

    vous pouvez l'utiliser pour imprimer le SQL réel.

     Private Sub Command2_Click() Dim db As Database Dim qr As QueryDef Set db = CurrentDb For Each qr In db.QueryDefs TextOut (qr.Name) TextOut (qr.SQL) TextOut (Ssortingng(100, "-")) Next End Sub Public Sub TextOut(OutputSsortingng As Ssortingng) Dim fh As Long fh = FreeFile Open "C:\Users\rs17746\Desktop\Text_Files\sample.txt" For Append As fh Print #fh, OutputSsortingng Close fh End Sub 

    Voici une autre version pour vous. Cela permettra d'exporter les résultats de chaque requête, chacun dans un file text séparé.

     Private Sub Command0_Click() Dim qdf As QueryDef Dim strFileName As Ssortingng For Each qdf In CurrentDb.QueryDefs If Left(qdf.Name, 1) <> "~" Then 'you need to figure out TransferText command. Maybe 'you won't be lazy and expect people to read it to 'you and tutor you on how it works. strFileName = qdf.Name 'Docmd.TransferText .... DoCmd.TransferText transferType:=acExportDelim, TableName:=strFileName, FileName:="C:\test\" & strFileName & ".txt", hasfieldnames:=True End If Next qdf MsgBox "Done" End Sub