Home |
Search |
Today's Posts |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Depends on what you mean by "large amount". You could just run the update
SQL in a loop and performance should be pretty good (depending partly on the performance of your DB). -- Tim Williams Palo Alto, CA "Rubble" wrote in message ... This is a helpful post for my situation -- is there any way I can push data directly into the MS SQL database w/o having to push one line at a time? In the example you showed it says " sSQL = "INSERT INTO Contacts (FirstName, LastName,Phone, Notes) " & _ " VALUES ('Bob','Phillips','01202 345678','me')" If I wanted to add a large amount of data directly from Excel (as opposed to the one record shown above) is there a good way to do that from VBA? Maybe where I have VBA export the info that I want to update or insert out to a csv file and then use the vba to pick up the csv file and push it into the MS SQL database? Thanks -- "Bob Phillips" wrote: Here are three routines that work with Access, you will need a different connection string for SQL Server, probably something like oConn.Open "Provider=sqloledb;" & _ "Data Source=myServerName;" & _ "Initial Catalog=myDatabaseName;" & _ "User Id=myUsername;" & _ "Password=myPassword" Sub AddData() Dim oConn As Object Dim oRS As Object Dim sSQL As String Set oConn = CreateObject("ADODB.Connection") oConn.Open = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & "c:\bob.mdb" sSQL = "INSERT INTO Contacts (FirstName, LastName,Phone, Notes) " & _ " VALUES ('Bob','Phillips','01202 345678','me')" oConn.Execute sSQL oConn.Close Set oConn = Nothing End Sub Sub GetData() Const adOpenForwardOnly As Long = 0 Const adLockReadOnly As Long = 1 Const adCmdText As Long = 1 Dim oRS As Object Dim sConnect As String Dim sSQL As String Dim ary sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & "c:\bob.mdb" sSQL = "SELECT * From Contacts" Set oRS = CreateObject("ADODB.Recordset") oRS.Open sSQL, sConnect, adOpenForwardOnly, _ adLockReadOnly, adCmdText ' Check to make sure we received data. If Not oRS.EOF Then ary = oRS.getrows MsgBox ary(0, 0) & " " & ary(1, 0) & ", " & ary(2, 0) Else MsgBox "No records returned.", vbCritical End If oRS.Close Set oRS = Nothing End Sub Sub UpdateData() Const adOpenForwardOnly As Long = 0 Const adLockReadOnly As Long = 1 Const adCmdText As Long = 1 Dim oConn As Object Dim oRS As Object Dim sConnect As String Dim sSQL As String Dim ary sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & "c:\bob.mdb" sSQL = "SELECT * From Contacts" Set oRS = CreateObject("ADODB.Recordset") oRS.Open sSQL, sConnect, adOpenForwardOnly, _ adLockReadOnly, adCmdText ' Check to make sure we received data. If oRS.EOF Then MsgBox "No records returned.", vbCritical Else sSQL = "UPDATE Contacts " & _ " SET Phone = 'None' " & _ "WHERE FirstName = 'Bob' AND LastNAme = 'Phillips'" oRS.ActiveConnection.Execute sSQL sSQL = "SELECT * From Contacts" oRS.ActiveConnection.Execute sSQL ary = oRS.getrows MsgBox ary(0, 0) & " " & ary(1, 0) & ", " & ary(2, 0) End If oRS.Close Set oRS = Nothing End Sub -- HTH Bob Phillips (remove nothere from email address if mailing direct) "keithb" wrote in message ... I need to use VBA code to update a table in a SQL Server database. I have not done anything like this before. Can someone point me in the right direction? Thanks, Keith |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Transmitting data from a server spreadsheet to a client spreadsheet | Excel Programming | |||
html form to update excel file on server. | Excel Programming | |||
Automatically update links when server names change | Excel Worksheet Functions | |||
SQL Server UPDATE Database from Excel Table | Links and Linking in Excel | |||
Worksheet gets update from DDE server | Excel Programming |