Thursday, 8 August 2013

sql source vba userform

sql source vba userform

I am struggling to establish a SQL connection/source for a VBA ListBox
userform. I have been able to import the correct sql table to an excel
sheet (from the code below) but all my attempts so far to load the same
data as a source for the listbox have failed. The code below was given to
me my a colleague and adapted to our db.
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
Has anyone experience with loading SQL tables as data sources for userforms?

No comments:

Post a Comment