Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 10th, 2012  |  syntax: None  |  size: 2.97 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. VBA - Generate Excel File from Access (QueryTable)
  2. With MeuExcel.Worksheets(4)
  3.     .QueryTables.Add connection:=rs, Destination:=.Range("A2")
  4. End With
  5.        
  6. invalid procedure call or argument (erro '5')
  7.        
  8. Sub GeraPlanilhaDT()
  9.  
  10. Dim MeuExcel As New Excel.Application
  11. Dim wb As New Excel.Workbook
  12.  
  13. Set MeuExcel = CreateObject("Excel.Application")
  14. MeuExcel.Workbooks.Add
  15.  
  16. MeuExcel.Visible = True
  17.  
  18. Dim strNomeServidor, strBaseDados, strProvider, strConeccao, strStoredProcedure As String
  19.  
  20. strNomeServidor = "m98DES;"
  21. strBaseDados = "SGLD_POC;"
  22. strProvider = "SQLOLEDB.1;"
  23. strStoredProcedure = "SP_ParametrosLeads_DT"
  24.  
  25. strConeccao = "Provider=" & strProvider & "Integrated Security=SSPI;Persist Security Info=True;Data Source=" & strNomeServidor & "Initial Catalog=" & strBaseDados
  26.  
  27. Dim cnt As New ADODB.connection
  28. Dim cmd As New ADODB.command
  29. Dim rs As New ADODB.recordset
  30. Dim prm As New ADODB.parameter
  31.  
  32. cnt.Open strConeccao
  33.  
  34. cmd.ActiveConnection = cnt
  35. cmd.CommandType = adCmdStoredProc
  36. cmd.CommandText = strStoredProcedure
  37. cmd.CommandTimeout = 0
  38.  
  39. Set prm = cmd.CreateParameter("DT", adInteger, adParamInput)
  40. cmd.Parameters.Append prm
  41. cmd.Parameters("DT").Value = InputBox("Digite o Código DT", "Código do Distribuidor")
  42.  
  43. Set rs = cmd.Execute()
  44.  
  45. Dim nomeWorksheetPrincipal As String
  46. nomeWorksheetPrincipal = "Principal"
  47.  
  48. Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = nomeWorksheetPrincipal
  49.  
  50.  
  51.  
  52. With MeuExcel.Worksheets(4)
  53.     .QueryTables.Add connection:=rs, Destination:=.Range("A2")
  54. End With
  55.  
  56.  
  57. cnt.Close
  58. Set rs = Nothing
  59. Set cmd = Nothing
  60. Set strNomeServidor = Nothing
  61. Set strBaseDados = Nothing
  62. Set strProvider = Nothing
  63.  
  64. If (ActiveSheet.UsedRange.Rows.Count > 1) Then
  65.     FormataDadosTabela
  66. Else
  67.     MsgBox ("Não foi encontrado nenhum Distribuidor com esse DT")
  68. End If
  69.  
  70.  
  71. End Sub
  72.        
  73. With wb.Worksheets(4)
  74.     .QueryTables.Add Connection:=rs, Destination:=.Range("A2")
  75. End With
  76.        
  77. Public Sub ExportToExcel()
  78.   Dim appXL As Excel.Application
  79.   Dim wbk As Excel.Workbook
  80.   Dim wst As Excel.Worksheet
  81.   Dim cn As ADODB.Connection
  82.   Dim rs As ADODB.Recordset
  83.  
  84.   Set appXL = CreateObject("Excel.Application")
  85.   appXL.Visible = True
  86.   Set wbk = appXL.Workbooks.Add
  87.   Set wst = wbk.Worksheets(1)
  88.  
  89.   Set cn = CurrentProject.AccessConnection
  90.   Set rs = New ADODB.Recordset
  91.   With rs
  92.     Set .ActiveConnection = cn
  93.     .Source = "SELECT * FROM tblTemp"
  94.     .Open
  95.   End With
  96.  
  97.   With wst
  98.     .QueryTables.Add Connection:=rs, Destination:=.Range("A1")
  99.     .QueryTables(1).Refresh
  100.   End With
  101.  
  102. End Sub
  103.        
  104. With MeuExcel.Worksheets(4).QueryTables.Add( _
  105.     connection:=recordset, _
  106.     Destination:=Range("A2"))
  107. End With
  108.        
  109. Dim recordset As recordset
  110. Set recordset = New recordset
  111.        
  112. Dim rs As recordset
  113.        
  114. Dim rs As New ADODB.Recordset
  115.        
  116. strConnect = "Provider=Microsoft.Access.OLEDB.10.0;Persist Security Info=True;" _
  117. & "Data Source=XYZSQLEXPRESS;Integrated Security=SSPI;" _
  118. & "Initial Catalog=TestDB;Data Provider=SQLOLEDB.1"
  119.        
  120. With MeuExcel.Worksheets.ListObjects.Add(Connection:=rs, Destination:=Range("A2")).QueryTable