Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > SQL UPDATE Query from within Excel 2002

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-28-02, 18:58
kenneth900 kenneth900 is offline
Junior Member
 
Join Date: May 2002
Posts: 2
Question SQL UPDATE Query from within Excel 2002

Hi all, I'm new to VBA coding (especially for Excel) and was wondering if anyone knows how I might perform a SQL UPDATE Query from within an Excel worksheet? I have the worksheet setup to query from a MS SQL Server db (via ODBC) and populate the spreadsheet. I would *LOVE* the capability of editing particular cells and then performing a macro command to export the data back, updating the original database. I can do it manually via MS-Query because all data comes from a single table, but I would really like to automate the process. If anyone could point me in a direction to get me started, I'd be grateful. Thanks-
Kenneth
Reply With Quote
  #2 (permalink)  
Old 05-31-02, 23:43
rnealejr rnealejr is offline
Registered User
 
Join Date: Feb 2002
Posts: 2,232
While in vba go to tools -> references. Add the MS activex data objects library.

Dim rst As ADODB.Recordset
Dim Cnxn As ADODB.Connection
Dim strCnxn As String
Dim strSQL As String

' Open connection
Set Cnxn = New ADODB.Connection
strCnxn = "Provider=sqloledb;Data Source=MyServer;Initial Catalog=mytable;User Id=sa;Password=; "
Cnxn.Open strCnxn

Set rst = New ADODB.Recordset
strSQL= "your update statement"
rst.Open strSQL, Cnxn, adOpenDynamic, adLockOptimistic, adCmdText

Cnxn.Close
Set rst = Nothing
Set Cnxn = Nothing

Last edited by rnealejr : 05-31-02 at 23:47.
Reply With Quote
  #3 (permalink)  
Old 06-03-02, 01:12
kenneth900 kenneth900 is offline
Junior Member
 
Join Date: May 2002
Posts: 2
thank you!

That did the trick!!! THANK YOU THANK YOU THANK YOU!! It worked like a charm! I had an idea of what the logic would be like, but I didn't understand quite how to get there. ADO works great! Thank you so much.

kenneth
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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