error -2147217865 in Microsoft.ACE.OLEDB provider reading a SharePoint list - excel

Please your help, I have the error -2147217865 in Microsoft.ACE.OLEDB provider reading a SharePoint list with VBA, where the Microsoft Access database engine could not find the object. Sometimes it connects without problems, but at other times it generates the error with the same query.
Set cnt = New ADODB.Connection
Set rst = New ADODB.Recordset
mySQL = "SELECT * FROM [" & SPListName & "];"
With cnt
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=1;RetrieveIds=Yes;" & _
"DATABASE=" & SERVERUrl & ";" & _
"LIST={" & SPListID & "}"
.Open
End With
rst.Open mySQL, cnt, adOpenDynamic, adLockOptimistic
I think the problem is with authentication in Sharepoint. Not if you can include the username and password in the connection string.

Related

Export Named Table from Excel to Access

OK. I'm sorry for wasting everyone's time. Like a DUMMY i didn't think simple solution first. The amount of data i am dealing with isn't too large and will actually work better just exporting to an excel file (i'm pretty sure). I would like to thank all that helped (June7, Parfait, and HansUp). The support you guys (everyone on this forum) give has made my job easier by far.
I'm trying to export an Excel Table from my active excel file to an Access database file.
I was getting an error at
"con.excecute sql"
"Run-time error '-2147467259 (80004005)': [Microsoft][ODBC Microsoft Access Driver] Query input must contain at least one table or query."
Sub updateAccess()
Dim con As New ADODB.Connection
Dim connectionString As String
Dim sql, newTable As String
Filename = "C:\Desktop\Quote-Size_Contacts.accdb"
connectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" & Filename
con.Open connectionString
' Save current table ("ContactsTbl_Data") to another table ("ContactsTbl_Data_yyyymmdd_hh_mmss")
newTable = "Quote-Size_Contacts_" & Format(Date, "yyyymmdd") & "_" & Format(Now, "hhmmss")
sql = "SELECT CODE, STORE INTO " & newTable & "FROM ContactsTbl_Data"
con.Execute sql
' Delete rows of current table ("ContactsTbl_Data")
sql = "DELETE FROM ContactsTbl_Data"
con.Execute sql
' Insert new rows into current table ("ContactsTbl_Data") from my Excel Sheet
sql = "INSERT INTO ContactsTbl_Data ([CODE], [STORE]) " & _
"SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" & ThisWorkbook.FullName & "].[" & ThisWorkbook.Sheets("Sheet2").Name & "$]"
con.Execute sql
con.Close
Set con = Nothing
End Sub
EDIT::
I'm not sure standard protocol for these forums on cleaning up the code and asking more questions so i'll just put an "Edit" here.
I applied the suggestions and matched the fields it was trying to save to my access file. I now get the error: "Method 'Execute' of object '_Connection' failed"
Public Sub updateAccess()
Dim con As New ADODB.Connection
Dim connectionString As String
Dim sql, newTable As String
Filename = "C:\Desktop\Quote-Size_Contacts.accdb"
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" & Filename & "'"
con.Open connectionString
' Save current table ("ContactsTbl_Data") to another table ("ContactsTbl_Data_yyyymmdd_hh_mmss")
newTable = "Quote-Size_Contacts_" & Format(Date, "yyyymmdd") & "_" & Format(Now, "hhmmss")
sql = "SELECT Company, Contact, Initials, Position, Address, AddressContd, CityStatePost, MainNo, CellNo, FaxNo, Email INTO [" & newTable & "] FROM ContactsTbl_Data"
con.Execute sql
' Delete rows of current table ("ContactsTbl_Data")
sql = "DELETE FROM ContactsTbl_Data"
con.Execute sql
' Insert new rows into current table ("ContactsTbl_Data") from my Excel Sheet
sql = "INSERT INTO ContactsTbl_Data ([Company], [Contact], [Initials], [Position], [Address], [AddressContd], [CityStatePost], [MainNo], [CellNo], [FaxNo], [Email]) " & _
"SELECT * FROM [Excel 12.0 Xml;HDR=Yes;Database=" & ThisWorkbook.FullName & "].[" & ThisWorkbook.Sheets("Sheet2").Name & "$]"
con.Execute sql
con.Close
Set con = Nothing
End Sub
See if this helps.
Table name has hyphen (-) character so use [ ] characters to delimit. Add a space in front of FROM so text doesn't run together in compiled SQL string.
sql = "SELECT CODE, STORE INTO [" & newTable & "] FROM ContactsTbl_Data"
As for connection to Access database, don't think I've ever used or seen Driver, I use Provider:
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" & Filename & "'"

Is there any way of automating certain query execution and storing the O/P of that query in a excel file?

I have around 80 queries which I execute on a daily basis for monitoring purpose. All of them being SELECT queries, we capture the mostly the counts. This is turning out to be a boring task that's just running the query and manually capturing the output in an excel file.
For example, these are my queries with their sample respective outputs:
Query#1: SELECT count(*) from table WHERE certain_condition = 'True'
OUTPUT: 985
Query#2: SELECT count(*) from another_table WHERE yet_another_condition = 'True'
OUTPUT: 365
…
Query#80: SELECT count(*) from another_table WHERE yet_another_condition = 'True'
OUTPUT: 578
My requirement is this:
Capture the output of these 80 queries and paste them in an excel file in a certain order.
In Excel, I'll already have a heading (condition) in a cell. So I want the output of each query to be mapped to a specific cell corresponding to the heading (condition).
Is there any way of automating this boring task, or am I stuck for eternity as a bot?
PS: I am using Toad for Oracle v 12.9.0.71 database
Like Tim was saying ADO is your best bet here. Lucky for you I just had to do this myself so hopefully this should work for you.
Sub SQLQuery(sqlServer As String, strDatabase As String, strQuery As String, _
exportLocation As Variant, strUserId As String, strPassword As String)
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set conn = Nothing
Set rs = Nothing
'create the Connection and Recordset objects
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
'open the connection
conn.Open _
"Provider=SQLOLEDB;" & _
"Data Source=" & sqlServer & ";" & _
"Initial Catalog=" & strDatabase & ";" & _
"User ID=" & strUserId & ";" & _
"Password=" & strPassword & ";" & _
"Trusted_Connection=" & "True" & ";"
'execute
Set rs = conn.Execute(strQuery)
'check if data exists
If Not rs.EOF Then
'if so, copy to location
exportLocation.CopyFromRecordset rs
'close the recordset
rs.Close
End If
'clean up
conn.Close
Set conn = Nothing
Set rs = Nothing
End Sub
An example of using this subroutine:
Call SQLQuery( _
oSERVER, _
oDB, _
"SELECT count(*) from table WHERE certain_condition = 'True'", _
ThisWorkbook.Sheets("Sheet1").Cells(1, 1), _
oUSER, _
oPW)
Just for reference you will likely have to enable Microsoft ActiveX Data Objects 2.8 Library in your References for this to work.

Excel VBA error "Query ' ' is corrupt" while executing update query using ADODB.Connection & Microsoft.ACE.OLEDB.12.0

I have an update query in Excel 2013 vba.
Till recently it worked.
Now I got the error "Query ' ' is corrupt" in the "adoConn1.Execute" command.
I know there is an Access Issue caused by November Office update.
The problem is that I got the error in Excel and the problematic Office updates
where not installed in my PC.
Can you help me?
strUpdateQuery= "Update [Table$] " & " SET FieldName = " & FieldValue
sConn1 = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & PathDB & "\" & FileNameDB & ";" & _
"Extended Properties=""Excel 12.0;HDR=YES"""
Set adoConn1 = New ADODB.Connection
adoConn1.Open sConn1
adoConn1.Execute strUpdateQuery, flagUpdateResult, adExecuteNoRecords
adoConn1.Close
If Not adoConn1 Is Nothing Then Set adoConn1 = Nothing

Read Excel file into ADODB recordset

I have an Excel VBA application that reads information from other excel files using ADODB recordsets. This application was working just fine, but a company software update just broke it (I guess, can't explain otherwise).
Copied below is a stripped down version that replicates the error I'm getting (also shown below). I've tried to review the many posts that reference Microsoft.Jet.OLEDB.4.0, but I'm not sure this is related. They all reference a specific error that I'm not seeing.
The identified error is caused by the CN.Open command. Thanks to comment from Matt'sMug, I have determined that I can execute the connection with no issue, provided the subject workbook is already open in Excel. If it is closed (as it should be), the error returns. Any ideas?
Public Sub GetExcelContent()
Dim Excelbook As String
Dim CN As ADODB.Connection, RS As ADODB.Recordset
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1
Excelbook = Sheets(1).Cells(4, 3)
Set CN = CreateObject("ADODB.Connection")
Set RS = CreateObject("ADODB.Recordset")
CN.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source= " & Excelbook & ";" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
RS.Open "SELECT * FROM [M2_F$]", _
CN, adOpenStatic, adLockOptimistic, adCmdText
End Sub
So, I have not figured out why the previous provider has ceased to work. However, I was able to switch to an alternate that works just as well. The coding is a little cleaner as well, so I'm happy. Here's my new protocol:
Dim CN as New ADODB.Connection
Dim RS as New ADODB.Recordset
Cstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Excelbook & _
& "; Extended Properties='Excel 12.0 Xml;HDR=YES';"
CN.Open Cstring
RS.Open "SELECT * FROM [M3_F$]", CN, adOpenStatic, adLockOptimistic, adCmdText

Microsoft.Ace.OLEDB.16.0

Can anyone please help me understand why Microsoft.Ace.OLEDB.16.0 doesn't work on Windows 10 x64 with Office 2016 x86?
I get runtime error -2147467259 (80004005) when the connection to OLEDB 16.0 is opened using the following code:
Public Sub ValidityDateCheck_SRT_Templates()
NameFile = "x"
sConn = "Provider=Microsoft.ACE.OLEDB.16.0;WSS;IMEX=1;RetrieveIds=Yes;" & _
"DATABASE=" & sSHAREPOINT_SITE & ";" & _
"LIST=" & sDEMAND_ROLE_GUID_ML & ";"
Set cn = New ADODB.Connection
Set rst = New ADODB.Recordset
With cn
.ConnectionString = sConn
.Open '--> **HERE I GET THE ERROR**
End With
Rs = "SELECT * FROM [Template Library];"
rst.Open Rs, cn, adOpenStatic, adLockOptimistic
Do Until rst.EOF
If Left(rst![Name], InStr(1, rst![Name], "#") - 1) = TemplateID Then
NameFile = rst![Name]
On Error GoTo Skip
ActualVdate = rst![Validity Date]
TransVdate = rst![Transition Period End]
Exit Do
Else
rst.MoveNext
End If
Loop
Skip:
rst.Close
The interesant part is that in DEBUG, if I use 12.0 first and then change to 16.0 and save, it will open it. Then, if I reopen file, i get same error.
Please help me guys.
Are you using VBA? If so, please ensure you have enabled the Microsoft ActiveX Data Objects 6.1 Library on the reference menu.
Try this;
cn.Open "Provider=Microsoft.Ace.OLEDB.16.0;" & _
"Data Source=" & UserForm2.TextBox32.Text & ";Jet OLEDB:Database Password=1101010;Persist Security Info=False;"

Resources