?
Solved

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

Posted on 2014-03-27
2
Medium Priority
?
402 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 36

Accepted Solution

by:
ste5an earned 2000 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 24

Author Closing Comment

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

That was perfect.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

850 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