vba 从没有列名的查询中选择数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6359834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:28:50  来源:igfitidea点击:

Select data from query without column names

sqlvbaselectexcel-vbaexcel

提问by JJRhythm

I'm using SQL in VBA to populate a spread sheet, but when I do this I get the data including the column headers. I am trying to find away to pull just the information out and no column names.

我在 VBA 中使用 SQL 来填充电子表格,但是当我这样做时,我得到了包括列标题的数据。我试图找出只提取信息而不提取列名称。

For example,

例如,

id name job
0  Tom  Repair
1  Bob  Tech

instead I want,

相反,我想要,

0  Tom  Repair
1  Bob  Tech

采纳答案by AllisonC

I'm not sure that you can have no column headers... Note: this is mysql, not sure about your dbms, but this is the closest I got:

我不确定您是否可以没有列标题...注意:这是 mysql,不确定您的 dbms,但这是我得到的最接近的:

SELECT id as "", name as "", job as "" FROM table

回答by nassao

You can run the mysql client with --skip-column-names

您可以使用 --skip-column-names 运行 mysql 客户端

回答by Hari Seldon

Understanding that you did not indicate MySQL, at my work we use the following (MySQL) query using the ADODB reference in excel. I am posting it as you may be able to adapt it for your use:

了解您没有指定 MySQL,在我的工作中,我们使用以下 (MySQL) 查询使用 Excel 中的 ADODB 参考。我发布它,因为您可以对其进行调整以供您使用:

Sub AdaptMeToYourUse()
  ' Connection variables
      Dim conn As ADODB.Connection
      Dim server_name As String
      Dim database_name As String
      Dim user_id As String
      Dim password As String
      Dim port_number As String

  ' Establish connection to the database
      server_name = "myserverName" ' 127.0.0.1 if running from a locally
      database_name = "myDBname" ' Enter your database name here
      user_id = "LoginName" ' enter your user ID here
      password = "PWforLoginName" ' Enter your password here
      port_number = "1234"

      Set conn = New ADODB.Connection
      conn.Open "DRIVER={MySQL ODBC 3.51 Driver}" _
        & ";SERVER=" & server_name _
        & ";PORT=" & port_number _
        & ";DATABASE=" & database_name _
        & ";UID=" & user_id _
        & ";PWD=" & password _
        & ";OPTION=16427" ' Option 16427 = Convert LongLong to Int

      Set rs = New ADODB.Recordset

      sqlstr = "SELECT  count(" & SomeField & ") FROM " _
             & SomeTable & " WHERE " & SomeField & " = '" & var2Compare2SomeField & "' "
      rs.Open sqlstr, conn, adOpenStatic, adLockReadOnly, adCmdText 'set appropriate options for you

      Worksheets("SomeWB").Range(SomeRNG).CopyFromRecordset rs

End Sub