LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default Update Excel Column Data from SQL Fields

I'm not sure if you have a lot of product numbers, if this represents
everything in the table or only a subset, or if you can easily define
the subset in a query.
It would be more straightforward to overwrite the speadsheet with a
straight dump of the table or a dump of some sql statement that limits
the products to only the ones you want to see. However, I'll assume
that you only have a few hundred products in the sheet and this is a
small subset of the table, without the ability to easily write a sql
statement to find only the ones you want. Maybe this will give you a
start.

You need a reference to Microsoft ActiveX Data Objects for the code
below to work

Public Const SQLConnection As String = "Data Source=[server
name];Initial Catalog=[database name];Network=[windows nt
network];User Id=[username];Password=[password]"


Public Sub UpdateInformation()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String, sResult As String
Set cn = New ADODB.Connection
cn.CursorLocation = adUseClient
cn.Provider = "SQLOLEDB"
cn.Open SQLConnection
Set rs = New ADODB.Recordset
cn.CommandTimeout = 600
Dim sht As Worksheet, lRow As Long
Set sht = ActiveSheet
For lRow = 2 To sht.UsedRange.Rows.Count
sql = "select description, cost from productinfo where
productnum='" & sht.Cells(lRow, 1).Value & "'"
rs.Open sql, cn, adOpenStatic, adLockReadOnly
If Not rs.EOF Then
sht.Cells(lRow, 2).Value = rs("description").Value
sht.Cells(lRow, 3).Value = rs("cost").Value
End If
rs.Close
Next lRow
cn.Close
Set rs = Nothing
Set cn = Nothing
Set sht = Nothing
End Sub
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Keep data in fields if no update while updating MS Excel Worksheet Functions 2 August 28th 08 08:06 AM
Can I pivot multiple data fields and display them in a column? Marc Forget Excel Discussion (Misc queries) 2 December 19th 06 05:29 PM
Splitting Column Data into 2 fields Kevin Excel Worksheet Functions 4 January 28th 06 07:03 AM
function to show if two fields in a column have the same data. j-p-c Excel Worksheet Functions 1 June 22nd 05 10:55 AM
How to get data from pivot table with multiple column fields? RADO[_3_] Excel Programming 3 November 18th 03 08:03 PM


All times are GMT +1. The time now is 09:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"