N
NormaD
I am having trouble with the Array portion of the attached code - as I step
through it works fine until it hits where I am loading the Excel information
into the variable.
.Update VBA.Array("ID", "Fname", "Lname"), Array(vaData(i, 1),
vaData(i, 2), vaData(i, 3)) when I hover over vaData it will show me the
information from the cells but on the Next step I get an error Message
"Cannot insert the Value NULL into column ID, table
GPReports.dbo.My_Employees; column does not allow nulls. Insert fails. If I
were to remove restrictions from the table it post NULL in the row.
Here is the full code:
Sub Export_Data_Excel()
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
stDB = ThisWorkbook.Path & "\" & "FirstTry"
'Create the connectionstring
stConn =
"Provider=SQLOLEDB;Server=CorpDyndb;Trusted_Connection=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)
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", "Lname"), Array(vaData(i, 1),
vaData(i, 2), vaData(i, 3))
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
Any ideas will be greatly appreciated.
Norma
through it works fine until it hits where I am loading the Excel information
into the variable.
.Update VBA.Array("ID", "Fname", "Lname"), Array(vaData(i, 1),
vaData(i, 2), vaData(i, 3)) when I hover over vaData it will show me the
information from the cells but on the Next step I get an error Message
"Cannot insert the Value NULL into column ID, table
GPReports.dbo.My_Employees; column does not allow nulls. Insert fails. If I
were to remove restrictions from the table it post NULL in the row.
Here is the full code:
Sub Export_Data_Excel()
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
stDB = ThisWorkbook.Path & "\" & "FirstTry"
'Create the connectionstring
stConn =
"Provider=SQLOLEDB;Server=CorpDyndb;Trusted_Connection=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)
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", "Lname"), Array(vaData(i, 1),
vaData(i, 2), vaData(i, 3))
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
Any ideas will be greatly appreciated.
Norma