Can't insert records from Excel to SharePoint - sql

Currently I am suffering Excel ADO issue.
Here is the thing taht
I was trying to insert some records
from "Excel table" to "SharePoint list" Using ADO connection.
But failed (Can't recognize Excel table in SQL Statement)
Dim SQL As String
Dim CN As New ADODB.Connection
Dim OLEDB As String
Dim LIST As String
Dim SITE As String
LIST = "{EE028282-3D7E-4D37-93EE-50FB69C4432C}"
SITE = "https://asml.sharepoint.com/teams/FFKR_DUV_YS_Installation_and_Relocation/Product"
OLEDB = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;" & _
"DATABASE=" & SITE & ";" & _
"LIST=" & LIST & ";"
Set CN = New ADODB.Connection
CN.Open OLEDB
SQL = SQL & "INSERT INTO Schedule_DB (NAME,TYPE_W) "
SQL = SQL & "SELECT * "
SQL = SQL & "FROM [" & ThisWorkbook.FullName & "].[S_RAW$] "
CN.Execute CommandText:=SQL
CN.Close
If I run it I got error ->
Error image
I have already check the miss-spell, and the amount of item is too much, so I would prefer to process it as a one SQL statement.
"Excel to Excel" works well But still have no ideas for "Excel to SharePoint List".
Please share your advice.

When you want to read data from your Excel sheet you need to have a ADODB.Connection with Read permission, then to write data to your SharePoint List you need to have another ADODB.Connection with Write permission.
Note: You can't transport your whole data in that way, You can generate a big command with whole data then use it or generate command for each record of your Excel data.
About your exception it is simply says it can't find your sheet name in SharePoint Lists.
A sample to guide you can be something like this:
Dim cnnXl As New ADODB.Connection
Dim rsXl As New ADODB.Recordset
Dim cnnShP As New ADODB.Connection
conStrXl = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\yourExcel.xlsx';" & _
"Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;"";"
commandXl = "SELECT [Field1], [Field2] FROM [Worksheet$$A1:D7] WHERE [Thing1] > 1"
conStrShP = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;" & _
"DATABASE=yourSite;LIST={yourListCLSID};"
Then open cnnXl and run commandXl and read data to rsXl.
Then open cnnShP and loop over rsXl records and create your commandShP and execute it.

Related

Trying to update Excel-Table through Access-VBA Code. Error: Cannot find database Object "Ware"

I have a number of Excel-Files that contain data which I need to combine into one table.
Is it possible and reasonable to do this from within Access VBA code, which I would prefer? Or should I just do it in Excel VBA?
My idea was to iterate through the Excel-Files and use an SQL statement for UPDATE Column WHERE MyCondition, to get the Values I want to the Column I want. Is it possible this way or what would be a suggestion to solving this?
I have tried it this way until now from Access:
Sub Execute_UpdateQuery()
Dim conn As ADODB.Connection
Dim NumOfRec As Integer
Dim strPath As String
strPath = "D:\Sortierliste August_September\Test.xlsx"
Set conn = New ADODB.Connection
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strPath & ";Extended Properties=Excel 12.0 Xml;HDR=YES;"
conn.Execute "UPDATE Ware SET Name = 'test'" & _
" WHERE Name = 'Ackermann'", NumOfRec, adExecuteNoRecords
Debug.Print NumOfRec & " records were updated."
conn.Close
Set conn = Nothing
End Sub
But that results into an error "Cannot find access-database module "Ware". I guess it is because it is just an excel-table and not a database? I thought it can find an excel-file containing the table called Ware and execute an sql query. Any help or suggestions would be great on how to solve this issue.
Greetz

Automation Error when Attempting to Query Access Database Using VBA

I've made the following ADODB object declarations in code.
Dim OConn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim fld As ADODB.Field
Set OConn = New ADODB.Connection
Set rs = New ADODB.Recordset
I would like to use the following code to read from a table on a MS Access database file and generate a recordset, rs.
'Get the table name from the search results.
tableName = ThisWorkbook.Sheets("PLC Module Data").Cells(2, 9).Value
'Set the SQL string.
strSql = "SELECT Code, Points, Type, Description, Rating " & _
"FROM " & tableName
'Set the connection string and open the connection to the Access DB.
OConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=Q:\AutoCAD Improvements\PLC IO Utility Docs\PLC IO Spreadsheet
App\PLC IO App\ace_plc.mdb"
OConn.Open
'Open the recordset and error out if nothing is returned
Set rs = OConn.Execute(strSql)
If rs.EOF Then
MsgBox "No matching records found."
rs.Close
OConn.Close
Exit Sub
End If
I've checked the query statement within the Access file itself and it works fine. I always get the error
Run-time error'-2147217900 (80040e14)': Automation Error
on the line,
Set rs = OConn.Execute(strSql)
If anyone could take a look over my code and determine why this is happening it would be much appreciated. I've looked at similar examples online and it seems like this should be correct.
I added the brackets around the tableName string and it works now. Thanks for all the feedback.
'Set the SQL string.
strSql = "SELECT Code, Points, Type, Description, Rating " & _
"FROM [" & tableName & "]"

SQL Select Statement in Microsoft Access VBA - Need help inserting in Access table as block, not loop

I am trying to do a Select statement from an SQL server using an ADODB connection, within Microsoft Access VBA. The select statement is very complex with calculations and would work better to keep that to try and turn in to a connection with the DB.
I can append the data row by row, but am wondering how I could append as simple as possible as a block of data to save time/vba effort. (Lots of data!)
In order to simplify the example, I have put a basic SQL statement, but if you could review this and let me know if there is a way to append as a recordset block or what would be a better option... The code below works for pasting as a block into excel, and I would think I only need to change that commented out line's method to append in Access...
Option Private Module
Private Const DataServer = "GLSSQLMADPS2"
Private Const sqlconstring = "Driver={SQL Server};Server=" & DataServer & "; Database=PERF_MGMT_BWRSRV_PROD; Trusted_Connection=yes; Connect Timeout = 600"
Sub M2DSQLPull()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
Dim sql As String
sql = "SELECT " & _
"Employee_Token_NR, " & _
"Null as 'Sum_Month', " & _
"1000 AS 'Data_ID', " & _
"DATEADD(m, DATEDIFF(m, 0, MIN(CALL_TS)), 0) AS 'Metric_1' " & _
"FROM PMBSED_PhoneEvaluations " & _
"GROUP BY Employee_Token_NR "
Set cn = New ADODB.Connection
cn.CommandTimeout = 120
cn.Open sqlconstring
Set rs = New ADODB.Recordset
rs.Open SqlString(i), cn, adOpenStatic, adLockReadOnly
'How I would paste in an excel spreadsheet, how do I block paste in an access database?
'ThisWorkbook.Worksheets("RawData").Cells(RowNumber, 1).CopyFromRecordset rs
RowNumber = RowNumber + rs.RecordCount
rs.Close
cn.Close
End Sub
Thank you for any assistance!
-Robert

Performing SQL queries on basic Excel 2013 worksheet as table using ADO with VBA triggers Errors

I'm developping modules on a client XLSm with 32-bits 2013 Excel.
I'd like to use datas on worksheet as if it is an Access table.
With a lot of difficulties, I think connection is now OK.
Still, I have error : 3001 Arguments are of wrong type, are out of acceptable range. Error that I cannot understand.
Here excerpts of VBA lines :
In addition, I added 20 lines in data Worksheet below the header line to permit to Excel to interpret for the type of each columns.
varCnxStr = "Data Source=" & G_sWBookREINVOICingFilePath & ";" & "Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=15';"
With conXLdb
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Mode = adModeShareExclusive
.Open varCnxStr
End With
strSQL = "SELECT * "
strSQL = strSQL & " FROM [ReInvoiceDB$B2B5072] inum "
strSQL = strSQL & " WHERE inum.InvoiceNum LIKE '1712*' "
strSQL = strSQL & ";"
'>> TRIGGERs ERROR with the current Where Clause !!'
adoXLrst.Open strSQL, conXLdb, dbOpenDynamic, adLockReadOnly, adCmdText
If adoXLrst.BOF And adoXLrst.EOF Then
'no records returned'
GoTo Veloma
End If
adoXLrst.MoveFirst
Do While Not adoXLrst.EOF
'Doing stuff with row'
adoXLrst.MoveNext
Loop
sHighestSoFar = adoXLrst(1).Value '> just to try for RecordSet : Codes are not completed...
sPrefixeCURR = Mid(sHighestSoFar, 1, 4)
Highest = CInt(Mid(sHighestSoFar, 5))
'> Increment >'
Highest = Highest + 1
HighestStr = sPrefixeCURR & Format(Highest, "00")
strGSFNumber = HighestStr
adoXLrst.Close
conXLdb.Close
Veloma:
On Error Resume Next
Set adoXLrst = Nothing
Set conXLdb = Nothing
Exit Sub
Etc.
Any idea about what seems be wrong ?
Thank you
Below is an old example I have been using successfully. Note that the sheet name in the book are Sheet1 and Sheet2, but in the query I had to use sheet1$ and sheet2$. I noticed you had $ signs in the middle of your sheet names. perhaps that's the issue ?
Sub SQLUpdateExample()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Set con = New ADODB.Connection
con.Open "Driver={Microsoft Excel Driver (*.xls)};" & _
"DriverId=790;" & _
"Dbq=" & ThisWorkbook.FullName & ";" & _
"DefaultDir=" & ThisWorkbook.FullName & ";ReadOnly=False;"
Set rs = New ADODB.Recordset
Set rs = con.Execute("UPDATE [Sheet1$] inner join [Sheet2$] on [Sheet1$].test1 = [Sheet2$].test1 SET [Sheet1$].test3 = [Sheet2$].test2 ")
Set rs = Nothing
Set con = Nothing
End Sub
To give more details about the whole module to be implemented : it is to perform a Transaction unit.
This transaction will comprise 3 operations : get a max value from a column (Invoice number) to increment it, record the new number inside an Access table (by DAO), the same Excel file (by ADO) and generating document on HDD.
So it is aimed to use the Excel file as a table not as a file manipulated with Windows script or Excel VBA. My end user is disturbed by the pop-uping of an Excel opening file operation. As a developer, I'm feeling more comfortable with using SQL statements as much as possible inside Transaction session. Is that your opinion too ?

Excel Insert Into Access Table from Excel defined Table with a Where Clause

A little stuck. I have a an excel table with lets say the following columns
[Date],[Name],[PostCode],[City]
And an access table with the same column names.
I'm trying to insert the excel data table into the access table but want to apply a WHERE clause so that it ignores dates already in the access table.
I can happily inset all data into the access table but cannot apply a WHERE clause successfully ignore the existing dates. And annoyingly I cant reference the actual table name correct so I am referencing the sheet (but works fine).
This is the code I have so far:
Sub TestInsert()
Dim cnn As ADODB.Connection
Dim dbPath As String
Dim xlPath As String
Dim uSQL As String
dbPath = "C:\Test\TestDB.accdb"
xlPath = "C:\Test\TestXL.xlsm"
'Set Connection string
Set cnn = New Connection
cnnstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
'Open connection to db
cnn.Open cnnstr
uSQL = "INSERT INTO tbl_ApolloBrochurewareReportTEST " _
& "SELECT * FROM [Excel 12.0;HDR=YES;DATABASE=" & xlPath & "].[Sheet1$]"
cnn.Execute uSQL
cnn.Close
End Sub
Any input or knowledge would be great, thank you.
You should be able to expand the SQL expression:
uSQL = "INSERT INTO tbl_ApolloBrochurewareReportTEST " _
& "SELECT * FROM [Excel 12.0;HDR=YES;DATABASE=" & xlPath & "].[Sheet1$] As T " & _
"WHERE T.Date NOT IN (SELECT [Date] FROM tbl_ApolloBrochurewareReportTEST)"