Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Insert Records from Excel Into MS SQL Server

I've been able to execute select statements to retrieve
selective data from SQL Server inside of the VBA code in
Excel. What commands are necessary to Insert records into
the same database and table?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 179
Default Insert Records from Excel Into MS SQL Server

John

If you are using ADO, here's an example of how to add a record. You'll need
to set a reference to the ActiveX Data Objects Library under Tools -
References.

Sub AddRec()

Dim mycn As ADODB.Connection
Dim i As Integer
Dim mySQL As String
Dim stConn As String
Dim myRS As ADODB.Recordset

mySQL = "SELECT `Table 1`.number, `Table 1`.name, `Table 1`.TheDate"
mySQL = mySQL & " FROM `c:\Dick\db1`.`Table 1` `Table 1`"

stConn = "DSN=MS Access 97 Database;DBQ=c:\Dick\db1.mdb;"
stConn = stConn & "DefaultDir=c:\Dick;DriverId=281;"
stConn = stConn & "FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;"

Set mycn = New ADODB.Connection

mycn.Open stConn

Set myRS = New ADODB.Recordset

myRS.Open mySQL, mycn, adOpenDynamic, adLockOptimistic

With myRS
.AddNew
.Fields(0).Value = 40
.Fields(1).Value = "Name"
.Fields(2).Value = Now
.Update
End With

myRS.Close
mycn.Close

End Sub

--
Dick Kusleika
MVP - Excel
www.dicks-clicks.com
Post all replies to the newsgroup.

"John" wrote in message
...
I've been able to execute select statements to retrieve
selective data from SQL Server inside of the VBA code in
Excel. What commands are necessary to Insert records into
the same database and table?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Insert Records from Excel Into MS SQL Server

Thanks. I've been using ODBC but am going to give this a
try.

-----Original Message-----
John

If you are using ADO, here's an example of how to add a

record. You'll need
to set a reference to the ActiveX Data Objects Library

under Tools -
References.

Sub AddRec()

Dim mycn As ADODB.Connection
Dim i As Integer
Dim mySQL As String
Dim stConn As String
Dim myRS As ADODB.Recordset

mySQL = "SELECT `Table 1`.number, `Table 1`.name, `Table

1`.TheDate"
mySQL = mySQL & " FROM `c:\Dick\db1`.`Table 1` `Table 1`"

stConn = "DSN=MS Access 97 Database;DBQ=c:\Dick\db1.mdb;"
stConn = stConn & "DefaultDir=c:\Dick;DriverId=281;"
stConn = stConn & "FIL=MS

Access;MaxBufferSize=2048;PageTimeout=5;"

Set mycn = New ADODB.Connection

mycn.Open stConn

Set myRS = New ADODB.Recordset

myRS.Open mySQL, mycn, adOpenDynamic, adLockOptimistic

With myRS
.AddNew
.Fields(0).Value = 40
.Fields(1).Value = "Name"
.Fields(2).Value = Now
.Update
End With

myRS.Close
mycn.Close

End Sub

--
Dick Kusleika
MVP - Excel
www.dicks-clicks.com
Post all replies to the newsgroup.

"John" wrote in message
...
I've been able to execute select statements to retrieve
selective data from SQL Server inside of the VBA code in
Excel. What commands are necessary to Insert records

into
the same database and table?



.

Reply
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
insert records? jerry joe king Excel Worksheet Functions 1 March 31st 09 09:57 PM
insert records ah Excel Worksheet Functions 2 January 25th 07 02:13 PM
SQL Server -- Bulk Insert from Excel to SQL Server Madhan Excel Discussion (Misc queries) 0 December 12th 06 03:08 PM
Insert Next? Or insert a variable number of records...how? Tom MacKay Excel Discussion (Misc queries) 0 April 20th 06 10:44 PM
Excel 2003 Error Cannot Locate the Internet Server or Proxy Server Seabee Excel Discussion (Misc queries) 0 November 20th 05 12:03 AM


All times are GMT +1. The time now is 12:06 AM.

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"