Solved

ADODB.RecordSet For updating a record in MSMSQL DB Error Object Does Not Support Updating

Posted on 2014-03-27
2
394 Views
Last Modified: 2014-03-27
I have two VBS scripts that are used to write to a MSSQL DB.

Logon Script: (this one works perfect)
on error resume next
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adUseClient = 3
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")

Dim StrComputerName, StrUser, strManufacturer, StrModel, StrSN

'************************************************************************************
'Data Collection																	*
'************************************************************************************
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\." _
 &"\root\cimv2")
Set colSystems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each objSystem In colSystems
		strComputerName = objSystem.name
		strModel = objsystem.Model
		strManufacturer = objsystem.Manufacturer
		If Not (ISNULL(objsystem.UserName)) then 
			strUser = Split(objsystem.UserName,"\")
			strUser(1) = UCase(Left(strUser(1),1))_
			& Trim(Mid(strUser(1),2,20))
		else
			Struser = split("\boscam","\")
			strUser(1) = UCase(Left(strUser(1),1))_
			& Trim(Mid(strUser(1),2,20))
		End If
    next
	Set colSMBIOS = objWMIService.ExecQuery _
		("Select * from Win32_bios")
	For Each objSMBIOS in colSMBIOS
    strSN = objSMBIOS.SerialNumber
    Next
Set ColDM =objWMIService.ExecQuery("Select * from Win32_DesktopMonitor")
	For Each objDM in ColDM
	strScreenHeight = objDM.ScreenHeight
	StrScreenWidth = objDM.ScreenWidth
	next

Set ColLogon = objwmiservice.execquery("Select * from Win32_LogonSession") ' Where LogonType = 2")

	For Each objLogon in ColLogon
		strDate = WMIDateStringToDate(objlogon.StartTime)
		strTime = WMIDateStringTotime(objlogon.StartTime)
		objSWbemDateTime.value = Objlogon.starttime
		vtdLogonTime = objSWbemDateTime.GetVarDate(true)
		StrDateDiff = DATEDIFF("s", vtdLogonTime ,now)
		
	Next
'msgbox  vtlogonTime
StrID= year(vtdLogonTime) & month(vtdLogonTime) & day(vtdLogonTime) & Hour(vtdLogonTime) & Minute(vtdLogonTime) & Second(vtdLogonTime)
'Msgbox StrID

'********************************************
 



'********************************************
'Connection to the Datadase		    *
'********************************************
strConnect = "Provider = SQLOLEDB.1;Data Source=SANDBOXHOST;Initial Catalog=Inventory;User ID='sa';Password='********'"

objConnection.Open strConnect

If lcase(strComputerName) <> "*********" then

objRecordset.CursorLocation = adUseClient
objRecordset.Open "SELECT * FROM Table1_1" , objConnection, _
    adOpenStatic, adLockOptimistic
    
    	objRecordset.AddNew
    	objRecordset("ID") = strID
    	objRecordset("ComputerName")= StrComputerName
    	objRecordset("UserName")= StrUser(1)
    	objRecordset("ComputerManufacturer")= strManufacturer
    	objRecordset("ComputerModel")= strModel
    	objRecordset("ComputerSN")= strSN
   	objrecordset("Recorddate") = now
   	objrecordset("RecordTime") = Now
    	objRecordset.Update
    
    else
    	wscript.quit
    end if

objRecordset.Close
objConnection.Close

'************************************************************************************
'Function																	*
'************************************************************************************

Function WMIDateStringToDate(dtmInstallDate)

 WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & _
 Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4))
End Function


Function WMIDateStringTotime(dtmInstallDate)

 WMIDateStringTotime = CDate(Mid(dtmInstallDate, 9, 2) & ":" & _
 Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate,13, 2))

End Function

Open in new window


Logoff Script (not working)
Used to update a record based on the Select statement.
When Recordset.open SQL, Conn,3,3 I have the result output to a msgbox and I am  gets what I need it do, but when I added Recordset("Field") = Now or even a String  like "TEST" It returns an error
"Current RecordSet does not support Updating. This may be a limitation of the provider, or the selected locktype.
'************************************************************************************
'Data Collection																	*
'************************************************************************************
Set objSWbemDateTime = CreateObject("WbemScripting.SWbemDateTime")

Dim StrComputerName, StrModel, strManufacturer, strUser

set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\." _
 &"\root\cimv2")
Set colSystems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each objSystem In colSystems
		strComputerName = objSystem.name
		strModel = objsystem.Model
		strManufacturer = objsystem.Manufacturer
		If Not (ISNULL(objsystem.UserName)) then 
			strUser = Split(objsystem.UserName,"\")
			strUser(1) = UCase(Left(strUser(1),1))_
			& Trim(Mid(strUser(1),2,20))
		else
			Struser = split("\boscam","\")
			strUser(1) = UCase(Left(strUser(1),1))_
			& Trim(Mid(strUser(1),2,20))
		End If
'strUser = objsystem.UserName
    next






'************************************************************************************
'Connect to DB																	*
'************************************************************************************
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adUseClient = 3
Set objConn = CreateObject("ADODB.Connection")
Set objRS = CreateObject("ADODB.Recordset")

strConnect = "Provider = SQLOLEDB.1;Data Source=SANDBOXHOST;Initial Catalog=Inventory;User ID='sa';Password='5QLsa@745'"

objConn.Open strConnect






SQL = "Select Computername,username,Logoffdate, Max(Recorddate) as Lastdate" & _ 
			" From Table1_1 where computername ='" & strComputerName & "' "  & _ 
			" and username = '" & StrUser(1) & "' and logoffdate is null Group by  ComputerName,UserName,logoffdate"
Msgbox SQL

objRS.open SQL, ObjConn,adOpenKeyset, adLockOptimistic, adCmdText
'MSGBOX objRS("ComputerName").value & VBcrLF & objRS("username").value & vbCRLF & objRS("LASTDATE").value
 
	
	objrs("LogoffDate") = "TEST"
	 
	objRS.Update

'objRS.Close
objConn.Close

Set objConnection = Nothing
set objRecordset = Nothing

Open in new window

0
Comment
Question by:yo_bee
2 Comments
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 39959883
You need normally to call objRS.Edit first, before changing a value.

But: The GROUP BY clause makes the recordset not updateable.

So you need to filter for the latest user row, e.g.

SQL = _
  "SELECT * FROM Table1_1 " & _
  "WHERE computername ='{0}' AND username = '{1}' " & _
  "AND ID = ( SELECT MAX(ID) FROM Table1_1 WHERE username = '{1}' ); "
SQL = Replace(SQL, "{0}", strComputerName)
SQL = Replace(SQL, "{1}", strUser(1))
Msgbox SQL

Open in new window

0
 
LVL 22

Author Closing Comment

by:yo_bee
ID: 39959982
Thank you for the help.

That was perfect.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Duplicated data in GROUP_CONCAT 2 15
Building JSON Results Table FROM DB 9 32
sql select total by week ending 3 21
What Is an Error? 2 21
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question