LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Help - Update a SQL table from a specific range in Excel

Help - I am trying to update a SQL table from a specific range: Here is the
code I am using. The array is returning null values. I will appreciate any
help I can get.

Sub OneMoreTry()
Dim cnt As ADODB.connection
Dim rst As ADODB.recordset
Dim xlCalc As XlCalculation
Dim rnData As Range, rnCell As Range
Dim stDB As String, stConn As String
Dim vaData() As Variant
Dim i As Long

'Change settings in order to increase the performance.

With Application
xlCalc = .Calculation
.Calculation = xlCalculationManual
.EnableEvents = False
.ScreenUpdating = False
End With

Set rnData = ActiveSheet.Range("J21:L23")


'Instantiate the ADO COM's objects.
Set cnt = New ADODB.connection
Set rst = New ADODB.recordset

'Create the connectionstring - The database is not protected with a
password.
stConn =
"Provider=SQLOLEDB;Server=CorpDyndb;Trusted_Connec tion=Yes;Initial
Catalog=GPReports;UID=;"

'Populate the array with data from the range.
vaData = rnData.Value

'If the data is stored in rows instead of columns then the
'solution would be the following:
Set rnData = ActiveSheet.Range("J21:L23")
'vaData = Application.Transpose(rnData.Value)


'Create the connection.
cnt.Open stConn

'Open the recordset.
rst.Open "My_Employees", cnt, adOpenKeyset, adLockOptimistic,
adCmdTableDirect

'Read data, add new data and update the recordset.

For i = 1 To UBound(vaData)
With rst
.AddNew
.Update VBA.Array("ID", "FName"), VBA.Array(vaData(i, 1), vaData(i, 2))
End With
Next i


MsgBox "Successfully updated the table!", vbInformation

'Close recordset and connection.
rst.Close
cnt.Close

'Release objects from memory.
Set rst = Nothing
Set cnt = Nothing

'Clear inputrange.
rnData.ClearContents

'Restore the settings.

With Application
.Calculation = xlCalc
.EnableEvents = True
.ScreenUpdating = True
End With


End Sub

Thank you

Norma


 
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
update pivot table data range JN[_2_] Charts and Charting in Excel 2 September 29th 08 08:40 PM
vlookup: source is specific and table is a range AJB Excel Discussion (Misc queries) 2 April 30th 08 12:07 PM
Auto Update Range of a Pivot Table TJDeborah[_2_] Excel Programming 1 April 19th 06 01:31 PM
How do I set the pivot table to auto-update the data range? Toni @ Fidelity Excel Discussion (Misc queries) 3 March 17th 06 09:45 PM
Update Data Field in Pivot Table with Dynamic Excel Range ExcelMonkey[_97_] Excel Programming 1 March 3rd 04 08:16 PM


All times are GMT +1. The time now is 11:40 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"