vba用户窗体的sql源代码

vba用户窗体的sql源代码,sql,vba,Sql,Vba,我正在努力为VBA ListBox用户表单建立SQL连接/源。我已经能够将正确的sql表导入excel工作表(从下面的代码),但迄今为止,我加载与listbox源相同数据的所有尝试都失败了。下面的代码是我的同事给我的,并根据我们的数据库进行了修改 Sub SQL_VBA() Dim sConn As String Dim oQt As QueryTable Dim sSql As String 'defining the connection string sConn = "ODBC;DS

我正在努力为VBA ListBox用户表单建立SQL连接/源。我已经能够将正确的sql表导入excel工作表(从下面的代码),但迄今为止,我加载与listbox源相同数据的所有尝试都失败了。下面的代码是我的同事给我的,并根据我们的数据库进行了修改

Sub SQL_VBA()

Dim sConn As String
Dim oQt As QueryTable
Dim sSql As String

'defining the connection string

sConn = "ODBC;DSN=RISK_DB;UID=;PWD=;"
sConn = sConn & "WSID=;DATABASE=RISK_DB"

sSql = str_SQLText

Set oQt = Sheet1.QueryTables.Add(Connection:=sConn, Destination:=Sheet1.Range("A1"), Sql:=sSql)

With oQt
.Name = "Query from"
.FieldNames = True '' This returns the headers of the tables you need
.RowNumbers = False
.PreserveFormatting = True
.RefreshOnFileOpen = False '' Dont want to refresh file each time it opens
.BackgroundQuery = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=True

End With

End Sub
有没有人有过将SQL表作为userforms数据源加载的经验

我想我会回答你的问题

如果你还有任何问题,请留下评论