Updating SQL from Excel

llan_man

New Member
Joined
Nov 6, 2005
Messages
45
Hi,

I have quite a complex excel vba budgeting model that has in built workflows. There's still loads of little bits that I need to iron out but the big "crease" I have at the moment is that I need to take a few fields and from within the spreadsheet and add a new record to an external SQL database.

I've gathered that I need to use the "INSERT" and "INTO" commands but haven't got a clue how. I would be very grateful for any advice and any experience users have had.

Thanks

Harv
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Harv

Hope this will help. There is a database c:\temp\test.mdb with a table called MyData. There are 6 fields (cusip, Company, Industry, Coupon, Rating and Price). Coupon and Price are Number, while the rest are Text.

In the existing sheet range A2:F2 are the values to be added to the table.

Code:
Sub ccc()
' references
' Microsoft DAO 2.5 or above compatibility library

 Dim wrkjet As Workspace
 Dim db As Database
 Dim rst As Recordset
 
 Set wrkjet = createworkspace("", "admin", "", dbusejet)
 Set db = wrkjet.OpenDatabase("c:\temp\test.mdb")
 Set rst = db.OpenRecordset("Mydata", dbOpenDynaset)

 With rst
  .AddNew
   !cusip = Range("a2")
   !company = Range("b2")
   !industry = Range("c2")
   !coupon = Range("d2")
   !ratings = Range("e2")
   !price = Range("f2")
  .Update
 End With

 rst.Close
 db.Close
End Sub

You will have to add in the relevant reference. In the VBE, go tools, references and scroll down until you find the DAO reference and select it.

HTH

Tony
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

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