Excel 2007 VBA SQL Server Connection - Obtain Database String

Jourden

New Member
Joined
Apr 3, 2013
Messages
3
Hi,

Recently started using VBA (Excel 2007) at work so not an expert! My issue is I'm connecting to SQL Server through the VBA below to select data from a database using an ODBC link on my computer. The code below works fine accept for the fact I have to hard code the database I am using into the SQL select. The server I am connecting to has many databases stored on it but say 5 of them are used regularly. The table structure in all the databases is identical but the records in these tables is different. Ideally I would like it so that when the end user is prompted to select the database the SQL code below points at that. At the moment it just points at one database, which might not be required every time. Ideally I would like to declare the database name as a variable from the ODBC connection and insert that in my SQL code so which ever database is chosen the variable will pick it up. However, I dont know how to do this and I have been searching the web for quite a long time trying to find something.

I'm obviousy happy to look at alternatives that do the same thing. Sorry for the very long introduction! And thank you in advance for any help.

See VBA code below:

'Setting the network ODBC connections
Dim sConn As String
Dim oQt As QueryTable
Dim sSql As String
Dim myCriteria As String
myCriteriaAcc = Trim(Range("C4").Value)
myCriteriaPeril = Trim(Range("C5").Value)
Info = MsgBox("Select EDM", vbOKOnly, "Instructions")
ScreenUpdating = False
'defining the connection string
sConn = "ODBC;DSN=;UID=;PWD=;APP=Microsoft Office 2007;"
sConn = sConn & "WSID=;DATABASE=;Network=DBMSSOCN"

sSql = "select a.ACCGRPNUM, a.ACCGRPNAME, sum(valueamt)TIV " & "from OM_EDM_Quote.dbo.accgrp a " & "inner join OM_EDM_Quote.dbo.loc b " & "on a.accgrpid = b.accgrpid " & "inner join OM_EDM_Quote.dbo.loccvg c " & Chr(13) & _
"on b.locid = c.locid " & "where a.ACCGRPNUM = '" & myCriteriaAcc & "'" & "and peril = '" & myCriteriaPeril & "'" & "Group by a.ACCGRPNUM, a.ACCGRPNAME"

Sheets("Sheet1").Columns("E:G").Select
Selection.ClearContents
Range("A1").Select

Set oQt = Sheet1.QueryTables.Add( _
Connection:=sConn, _
Destination:=Sheet1.Range("E4"), _
Sql:=sSql)
With oQt
.Name = "TIV_SUM"
.Name = False
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=True
End With
ScreenUpdating = True
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top