首页 > 解决方案 > 试图将 MS SQL Server 表中的值放入 Excel VBA 中的表单中的列表框

问题描述

我正在尝试以表单的形式显示 ListBox 中的表中的数据,但没有成功。以下是我正在使用的代码:

    Dim cnt As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim stDB As String, stConn As String, stSQL As String
    Dim xlCalc As XlCalculation
    Dim vaData As Variant
    Dim k As Long
    Dim cs As String
     
    Set cnt = New ADODB.Connection
     
    cs = "DRIVER=SQL Server;"
    cs = cs & "DATABASE=dbname;"
    cs = cs & "SERVER=."


   
     
    cnt.ConnectionString = cs
     
     
    stSQL = "SELECT * from table"
    
   
     
    With cnt
        .CursorLocation = adUseClient
        .Open stConn
        Set rst = .Execute(stSQL)
    End With
     
    With rst
        Set .ActiveConnection = Nothing
        vaData = .GetRows
    End With
     
    cnt.Close
    
    ListBoxReqJobs.Value = vaData
    'this is the part I need help with. 
     
    Set rst = Nothing
    Set cnt = Nothing


End Sub

关于如何正确显示数据的任何建议?我似乎无法格式化数据以正确显示行,并且大约有 5-6 列。

标签: sql-serverexcelvba

解决方案


推荐阅读