poolmwv said:
Can you explain this in simple English to someone who has rudimentary Excel knowledge?
In Excel, I have written a query against a DB2 database that will return anything from our inventory that has been updated since the last time it was run (variable number of rows). It is imported as an SQL table and I query a different SQL table to populate 2 more fields (indices based on Asset_Owner and Asset_Type). If there is a typo, the index won't get populated and the update to the SQL database will bomb.
I would like to populate those two fields while still in Excel so that I can eyeball the data before sending it to SQL.
I have copied the code posted here into Visual Basic in Excel and made some changes that I *think* will work (I'm not sure what to do for that Microsoft driver string since I am not going against Access but SQL. I just ommitted it).
Although I programmed in Basic many years ago, this is the first time I have tried using VB in an Office Application. Now what? I feel like I am sitting on a saddle next to a horse and am puzzled why I'm not moving. I'm not even sure I pasted the code into the write place. I just clicked on "View Code" and pasted it.
Hi,
Nate's code can easily be modified to work with SQL Server. The beauty of ADO is that once a connection is established i.e. the Connection object is opened, you can use a standard set of methods to access many different data sources. Here is some sample code for connecting to SQL Server. It won't work as it is because I don't know the name of your server, database, etc. and will require some modifying before it does what you need.
I used the excellent resource from
here in my code - it has a heap of examples of connecting to various data sources.
PLEASE NOTE : You must open the VB Editor (Alt F11), click Tools, References and select Microsoft ActiveX Data Objects 2.n Library before this code will work.
<font face=Courier New><SPAN style="color:darkblue">Sub</SPAN> ConnectToSQLServer()
<SPAN style="color:darkblue">Dim</SPAN> adoCN <SPAN style="color:darkblue">As</SPAN> ADODB.Connection, adoRS <SPAN style="color:darkblue">As</SPAN> ADODB.Recordset
<SPAN style="color:darkblue">Dim</SPAN> strSQL <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">String</SPAN>
<SPAN style="color:darkblue">Set</SPAN> adoCN = <SPAN style="color:darkblue">New</SPAN> ADODB.Connection
<SPAN style="color:green"><SPAN style="color:green"><SPAN style="color:green">'///////////////////////////////////////////////////////////</SPAN></SPAN></SPAN>
<SPAN style="color:green">'CRUCIAL PART - ONCE YOU GET THIS WORKING YOU'RE LAUGHING!</SPAN>
<SPAN style="color:green">'This part will open a connection to a SQL Server database</SPAN>
<SPAN style="color:green">'You will need to change the Data Source, Initial Catalog,</SPAN>
<SPAN style="color:green">'user ID and password to suit your needs</SPAN>
adoCN.<SPAN style="color:darkblue">Open</SPAN> "Provider=sqloledb;" & _
"Data Source=myServerName;" & _
"Initial Catalog=myDatabaseName;" & _
"User Id=myUsername;" & _
"Password=myPassword"
<SPAN style="color:green"><SPAN style="color:green"><SPAN style="color:green">'///////////////////////////////////////////////////////////</SPAN></SPAN></SPAN>
<SPAN style="color:green">'Now that the connection is open, you can create recordsets and work</SPAN>
<SPAN style="color:green">'with them in Excel e.g.</SPAN>
<SPAN style="color:darkblue">Set</SPAN> adoRS = <SPAN style="color:darkblue">New</SPAN> ADODB.Recordset
<SPAN style="color:green">'Change this SQL statement to whatever you need</SPAN>
strSQL = "SELECT Field1, Field2 FROM YourTable"
<SPAN style="color:green">'Open the recordset</SPAN>
adoRS.<SPAN style="color:darkblue">Open</SPAN> strSQL, adoCN, adOpenForwardOnly, adLockReadOnly
<SPAN style="color:green">'Copy the recordset into a worksheet (will only work with Excel 2000 onwards)</SPAN>
ActiveCell.CopyFromRecordset adoRS
<SPAN style="color:green">'You should disconnect from the database at the end of any code which opens</SPAN>
<SPAN style="color:green">'it. Leaving a connection open could cause you problems.</SPAN>
adoRS.<SPAN style="color:darkblue">Close</SPAN>
adoCN.<SPAN style="color:darkblue">Close</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN>
</FONT>