Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 49948

[SOLVED] Help editing VBA code that uses MySQL queries to show also column names

$
0
0
Dear all,

I'm using one old excel file with a VBA code that allows to do MySQL queries on other excel files.

The file works great, but whenever I do queries, the pasted data does not contain the column names, which I would like to have. Can someone help me out on editing this code to whenever I do queries I get also the column names please?

Here is the code that I'm using, and I will also attach the file and one example database file too.

MODULE:
Code:

Option Explicit
'Criação das variáveis globais para utilização em todos os módulos
Global str_Conexao 'As String
Global ado_Conexao 'As ADODB.Connection
Global str_Versao 'As String


Sub Conectar_Excel()
 
    Dim Caminho As String
    Dim Arquivo As String
   
    Caminho = Planilha2.Range("c4")
    Arquivo = Planilha2.Range("c6")
   
    'Define string de Conexão
    str_Conexao = _
        "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" & _
                "DSN=TESTE_SQL;DBQ=" & Caminho & Arquivo & ";" _
                & "ReadOnly=0;DefaultDir=" & Caminho & ";" _
                & "DriverId=1046;FIL=excel 12.0;MaxBufferSize=2048;PageTimeout=5;"
   
    'Seta ADODB
    Set ado_Conexao = CreateObject("ADODB.Connection")
   
    'Abre Conexão
    ado_Conexao.Open str_Conexao
   
   
End Sub

WORKSHEET
Code:


Private Sub CommandButton1_Click()

listar_dados

End Sub


Sub listar_dados()
 
 
  'Chama rotina de Conexão
  Call Conectar_Excel
                 
  'Seta RecordSet
  Set rs_Consulta = CreateObject("ADODB.Recordset")

  'Define da Query
  str_consulta = Planilha1.Range("G5")
                             
  'Abre Recordset
  rs_Consulta.Open str_consulta, ado_Conexao
   
  'Limpa o Relatório
  limpar
   
  'Cola Recordset na planilha
  Planilha1.Range("b13").CopyFromRecordset rs_Consulta
         
  'FechaConexão
  rs_Consulta.Close
  Set rs_Consulta = Nothing
             
End Sub

Sub limpar()

Planilha1.Range("b12:aa5000").ClearContents

End Sub

Thank you all for any help!
Attached Files

Viewing all articles
Browse latest Browse all 49948

Trending Articles