如何将数据从SQL Server填充到Excel ComboBox中

时间:2014-02-25 14:07:15

标签: sql sql-server excel vba excel-vba

我正在尝试将一些数据从SQL Server填充到Excel文件中的组合框中。

以下是活动代码:

Private Sub Workbook_Open()

ActiveWorkbook.Sheets("Generation").Activate

Set cn = New ADODB.Connection

On Error Resume Next

With cn
 .ConnectionString = "Provider=SQLOLEDB.1;" & _
                        "Integrated Security=SSPI;" & _
                        "Server=" & "192.160.160.150;" & _
                        "Database=" & "em_Consumer;" & _
                        "User Id= " & "User" & _
                        "Password = " & "server123"
    .Open
End With

Set rs = New ADODB.Recordset

sqltextexec = " SELECT name FROM sys.tables WHERE  schema_id = 7 AND name LIKE 'FinalCalculated%' ORDER BY create_date "

    rs.Open sqltextexec, cn

    rs.MoveFirst

With Sheets("Generation").ComboBox1
    .Clear
    Do
        .AddItem rs![Name]
        rs.MoveNext
    Loop Until rs.EOF

End With

End Sub

程序是否可以使用Windows身份验证连接到数据库???

因为这段代码在我的计算机上和我的同事上运行良好(我们来自数据库团队),但是不使用DB的分析师不会在文件中填充列表。

感谢你知道什么是错的?

2 个答案:

答案 0 :(得分:0)

连接字符串错误

您的连接字符串似乎有错误。用户ID后面需要有一个分号。 改变这个

With cn
 .ConnectionString = "Provider=SQLOLEDB.1;" & _
                        "Integrated Security=SSPI;" & _
                        "Server=" & "192.160.160.150;" & _
                        "Database=" & "em_Consumer;" & _
                        "User Id= " & "User" & _
                        "Password = " & "server123"

到此

With cn
 .ConnectionString = "Provider=SQLOLEDB.1;" & _
                        "Integrated Security=SSPI;" & _
                        "Server=" & "192.160.160.150;" & _
                        "Database=" & "em_Consumer;" & _
                        "User Id= " & "User;" & _
                        "Password = " & "server123"

这是一个难以捉摸的小虫子。


修改

我无法在这里查明问题,所以也许一个有效的例子会更好地帮助你......

Function getSqlData(queryString As String, myUsername As String, myPassword As String, database As String) As Recordset

Dim conn As New ADODB.Connection
Dim rst As Recordset
Dim serverName As String


serverName = "192.160.160.150"


With conn
        .ConnectionString = "Provider=SQLOLEDB.1;" & _
        "Data Source=" & serverName & ";" & _
        "Initial Catalog=" & database & ";User Id=" & myUsername & ";" & _
        "Password=" & myPassword & ";Trusted_Connection=no"
        .Open
End With


Set rst = conn.Execute(queryString)

Set getSqlData= rst

End Function

这将返回您的记录集。

答案 1 :(得分:0)

今天我尝试使用@lopsided帮助从头开始编写它。这是代码:

Private Sub Workbook_Open()

ActiveWorkbook.Sheets("generation").Activate

Dim rstt As Recordset

MsgBox "1"

Set rstt = getData()

End Sub

-------------------------------------------------

Private Function getData()

Dim conn As New Connection
Dim rst As Recordset
Dim sqlstring As String
Dim rwcnt As Integer


MsgBox "2"

sqlstring = "SELECT productname FROM dbo.products WHERE recalc = 1"

With conn

 .ConnectionString = "Provider=SQLOLEDB.1;" & _
                     "Data Source=192.160.160.150;" & _
                     "Initial Catalog=em_Consumer;" & _
                     "User Id=User;" & _
                     "Password=server!;" & _
                      "Trusted_Connection=no"

.Open

End With

MsgBox "3"

Set rst = conn.Execute(sqlstring)

rwcnt = rst.RecordCount

MsgBox rwcnt

MsgBox "5"

Set getData = rst

MsgBox "6"

End Function

所以当我打开文件时,我收到消息:

1表示程序已启动;

2表示它进入了函数;

3表示连接没有问题;

<强> !!然后我得到-1值作为记录计数,这意味着出了问题

我尝试在management studio中运行此查询,它返回50行

然后程序更进一步,我得到5和6 ......

您对代码有什么问题有什么想法吗?

---------------------------------------------

也许它可以提供帮助,代码工作正常但在同一文档中返回表而不是记录集:

Sub Button3_Click()

    ActiveSheet.Cells.Clear

    Dim qt As QueryTable

    sqlstring1 = "SELECT * FROM dbo.Report"

    With ActiveSheet.QueryTables.Add(Connection:=getConnectionStr2, Destination:=Range("A3"), Sql:=sqlstring1)

    .Refresh

    End With

End Sub

----------------------------------

Private Function getConnectionStr2()
'DRIVER={SQL Server};
getConnectionStr2 = "ODBC;DRIVER={SQL Server};" & _
                   "DATABASE=em_Consumer;" & _
                   "SERVER=192.160.160.150;" & _
                   "UID=user;" & _
                   "PWD=server!;"
End Function