Sharepoint VBA ADODB update statement not working - vba

While using VBA to update a certain record in a SharePoint list, the program is stuck at update command or a at certain field
Code:
JRVCS = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;DATABASE=https://sites.123.com/sites/abc/def/;LIST="
JRVList = JRVCS & "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX};"
Set JRVConn = New ADODB.Connection Set JRVRSet = New ADODB.Recordset
With JRVConn
.ConnectionString = JRVList
.Open
End With
mySQL = "SELECT * FROM [List1] WHERE [ID] = 200;"
JRVRSet.Open mySQL, JRVConn, adOpenDynamic, adLockOptimistic
If Not (JRVRSet.BOF And JRVRSet.EOF) Then
JRVRSet.Fields("Feild 1") = "Value 1"
JRVRSet.Fields("Date") = CDate(Now() + 1)
JRVRSet.Update
End If
If CBool(JRVRSet.State And adStateOpen) = True Then JRVRSet.Close: Set JRVRSet = Nothing
If CBool(JRVConn.State And adStateOpen) = True Then JRVConn.Close: Set JRVConn = Nothing
In above program the program is stuck at JRVRSet.Fields("Date") = CDate(Format(Now() + 1)) and JRVRSet.Update.
This issue is occurring in certain SharePoint sites only. In a SharePoint site which I created, this code works perfectly. Is this related to permission?

Related

VBA to update sharepoint list inside a folder

I am trying to update a sharepoint list inside a folder. The structure us as below
NewGLJE Log_List_New : 2018 : 12 Dec 18
With the below code I am able to add a new item to the main folder which is NewGLJE Log_List_New however want to update in the 12 Dec 18 folder. the code is below
Option Explicit
Sub AddNew_SP()
Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim mySQL As String
Set cnt = New ADODB.Connection
Set rst = New ADODB.Recordset
mySQL = "SELECT * FROM [NewGLJE Log_List_New];"
With cnt
.ConnectionString = _
"Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;DATABASE=http://staging.gl.firstam.net/sites/IFSC/GL/;LIST={71D20BD2-393D-4055-AFC7-476BB8776DE3};"
.Open
End With
rst.Open mySQL, cnt, adOpenDynamic, adLockOptimistic
rst.AddNew
rst.Fields("JE/Batch Name") = "JE1234Test"
rst.Fields("Division") = "DBS"
rst.Fields("Description") = "TEST"
rst.Fields("Control Total") = "10000"
rst.Update
If CBool(rst.State And adStateOpen) = True Then rst.Close
Set rst = Nothing
If CBool(cnt.State And adStateOpen) = True Then cnt.Close
Set cnt = Nothing
End Sub
I tried editing the sql statement to include the sub folders, however that gives a syntax error.
Can anyone please help me how to get to updating the list in the sub folders.
Thanks

Send outlook attachment to Sharepoint list

We are working on a small VBA code that transmits the content of an e-mail message in Outlook to SharePoint. Our code reads the e-mail and filters out some key components (which are stored in variables). We then use ADODB to create a new item in a SharePoint list.
For that we use the following code:
Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim mySQL As String
Set cnt = New ADODB.Connection
Set rst = New ADODB.Recordset
mySQL = "SELECT * FROM [xxxx];"
With cnt
.ConnectionString = _
"Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;DATABASE=xxxxxx;LIST={xxxxx};"
.Open
End With
rst.Open mySQL, cnt, adOpenDynamic, adLockOptimistic
rst.AddNew
rst.Fields("Titel") = TheName
rst.Fields("ValidFrom") = ValidFrom
rst.Fields("ValidUntil") = ValidUntil
rst.Fields("VersionNr") = Version
rst.Update
If CBool(rst.State And adStateOpen) = True Then rst.Close
Set rst = Nothing
If CBool(cnt.State And adStateOpen) = True Then cnt.Close
Set cnt = Nothing
However, we want to send the attachment of the e-mail (if there is an attachment) also to the sharepoint list, and we are at a loss how to achieve this. After some Googling we found that it is possible to upload the file to a document library, and then use a hyperlink in the sharepoint list to that document. But for us it is possible to have multiple files in one e-mail... Does somebody know a way to achieve this?

Populate field in ms access form from field in ado recordset record of the same name

I have a form in Access that I am trying to populate during it's Current event using the fields from an ADO Recordset. I am using Sql Server for the database and am using the recordset to try to populate to corresponding fields on the form with what is in the recordset. Right now it works like this:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_FormOpen
Set rst As new ADODB.Recordset
With rst
.ActiveConnection = CurrentProject.AccessConnection
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Source = "SELECT * FROM [Outage Summary] ORDER BY OutageSummaryID"
.Open
End With
Set Me.Recordset = rst
End Sub
Private Sub Form_Current()
OutageSummaryID.Value = Me.Recordset!OutageSummaryID
Dispatcher.Value = Me.Recordset!Dispatcher
StartDateTime.Value = Me.Recordset!StartDateTime
Location.Value = Me.Recordset!Location
CityRelated.Value = Me.Recordset!CityRelated
Scheduled.Value = Me.Recordset!Scheduled
CustomerEquip.Value = Me.Recordset!CustomerEquip
DispatchBusHrs.Value = Me.Recordset!DispatchBusHrs
TotalCount.Value = Me.Recordset!TotalCount
Substation.Value = Me.Recordset!Substation
CompletionDateTime.Value = Me.Recordset!CompletionDateTime
CustCallDateTime.Value = Me.Recordset!CustCallDateTime
FirstRespDateTime.Value = Me.Recordset!FirstRespDateTime
Feeder.Value = Me.Recordset!Feeder
SwitchingSchedule.Value = Me.Recordset!SwitchingSchedule
Cause.Value = Me.Recordset!Cause
ActionTaken.Value = Me.Recordset!ActionTaken
Me.ME.Value = Me.Recordset!ME
MI.Value = Me.Recordset!MI
End Sub
But I would like the current subroutine to work something like this:
Dim fld As ADODB.Field
Dim nam As String
For Each fld In Me.Recordset.Fields
Debug.Print fld
nam = fld.Name
Me.Controls(nam).Value = fld.Value
Next
With the code as it stands I am getting an error "The recordset is not updatable"
Thanks for any help!
This is because you are not binding to the recordset. It would be better if you stored your query and attached it to the form as the recordsource so that way it will bind it to the form. Then you set the record source of each field in design mode and no code is needed and it will be updateable depending on your permission to the SQL Server.

use SQL Query in asp to populate scripting dictionary

this is an answer I received from another question. I was just wondering how I use an SQL Query in asp to add users to the scripting dictionary using a database instead of writing them manually.
Set bannedUsers = CreateObject("Scripting.Dictionary")
bannedUsers.Add "johnsmith", True
bannedUsers.Add "cmanson", True
...
For Each opt In document.getElementById("frmNew").options
If opt.selected And bannedUser.Exists(opt.text) Then
MsgBox "This user is banned."
End If
Next
You need to establish a connection to your database (in case it isn't already established), e.g.:
connectionString = "..."
Set conn = CreateObject("ADODB.Connection")
conn.open connectionString
This place has a collection of connection strings for various database backends.
With the connection established you run a query against the database. There are several ways to do this, e.g. like this:
query = "SELECT fieldname FROM table WHERE condition"
Set cmd = CreateObject("ADODB.Command")
cmd.CommandText = query
Set rs = cmd.Execute
or like this:
query = "SELECT fieldname FROM table WHERE condition"
Set rs = CreateObject("ADODB.Recordset")
rs.CursorPosition = 3
rs.open query, conn, 3, 1
Adjust fieldname, table and condition according to your actual data and requirements.
Fill the dictionary with the values from the database like this:
Set bannedUsers = CreateObject("Scripting.Dictionary")
Do Until rs.EOF
bannedUsers.Add rs("fieldname").Value, True
rs.MoveNext
Loop
If the table doesn't have a unique index on fieldname you may want to check the dictionary for the existence of the key before adding it:
If Not bannedUsers.Exists(rs("fieldname").Value) Then
bannedUsers.Add rs("fieldname").Value, True
End If
Since you're querying a database anyway you don't even have to use a dictionary. You could disconnect the recordset and check it directly for user names:
query = "SELECT fieldname FROM table WHERE condition"
Set bannedUsers = CreateObject("ADODB.Recordset")
bannedUsers.CursorPosition = 3
bannedUsers.open query, conn, 3, 1
bannedUsers.ActiveConnection = Nothing 'disconnect recordset
For Each opt In document.getElementById("frmNew").options
If opt.selected Then
bannedUsers.Filter = "[fieldname] = '" & opt.text & "'"
If bannedUser.recordCount > 0 Then
MsgBox "This user is banned."
End If
End If
Next
This should do :
Set bannedUsersSet = conn.execute "SELECT DISTINCT LOGIN FROM BANNED_USERS /* Here goes your query */"
Set bannedUsers = CreateObject("Scripting.Dictionary")
While not bannedUsersSet.EOF
bannedUsers(bannedUsersSet("LOGIN")) = True
bannedUsersSet.MoveNext
WEnd
bannedUsersSet.close
Set bannedUsersSet = Nothing

Invalid Use of Property in VB6 Error. How to fix this?

Private Sub aTbBar_Change()
Set con = New ADODB.Connection
With con
.CursorLocation = adUseClient
.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;persist security info=false;data source=" & App.Path & "\Event_Participants.accde"
.Open
End With
Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = con
.CursorType = adOpenDynamic
.Source = "select * from Participants"
.Open
'check from table if user and pwd matches
If rs.RecordCount <> 0 Then
rs.MoveFirst
While Not rs.EOF
If rs!Bar_Code_No = Val(Me.aTbBar) Then
Me.aTbName = rs!Full_Name
Me.aTbSection = rs!Section
Me.aTbArrtime = Time()
End If
rs.MoveNext
Wend
End If
.Close
Set rs = Nothing
End With
'save to the database
'check from table if user and pwd matches
Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = con
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
.Source = "select * from Participants"
.Open
If rs.RecordCount <> 0 Then
rs.MoveFirst
While Not rs.EOF
If rs!Bar_Code_No = Val(Me.aTbBar) Then
.Update
rs!Arr_Time = Me.aTbArrtime
End If
rs.MoveNext
Wend
End If
End With
rs.Close
Set rs = Nothing
End Sub
Invalid Use of Proper error always occur when I type in to that textbox name aTbBar
The error occurs at Me.aTbName = rs!Full_Name. Can you help me on this one. Sorry, im new in this forums and in VB. I really need help
The default property triggered for a TextBox is the Text property. So, if there is a TextBox with the name Text1, then this statement: Text1 = "Hello" would be equivalent to Text1.Text = "Hello". But I always prefer using the property name along with the control name, when accessing it(ie, Text1.Text = "Hello").
Anyway, test it out by using this line: Me.aTbArrtime.text = rs!Full_Name
Another thing that I have in mind is, if you have used some other component, say a custom made TextBox control (instead of the default one), and in the case of load failure, VB would replace the control(the custom made textbox) with a PictureBox in your forms. For checking that, click on the TextBox in the form and view it's properties. And see whether the control type is a TextBox. If it is a PictureBox, then double check whether your OCX or DLL for the custom made textbox is present in the project.
A small suggestion on your SQL code is that, you could have included the comparison in your query itself, instead of looping through all the records. For example:
.Source = "select * from Participants WHERE Bar_Code_No = " & Val(Me.aTbBar.Text) & " LIMIT 1"
This would return a single record if it matches the Bar_Code_No. After executing this query, you only need to check if it returns any record. If so, a match is found. Otherwise, no match is found. By this way, you can avoid looping, which might make your program Non-Responding if the number of records in the table Participants is enormously large !
Hope this would help you. Wish you good luck :)