?
Solved

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

Posted on 2014-03-27
2
Medium Priority
?
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

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 23

Author Closing Comment

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

That was perfect.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

764 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