Multiple ADODB.Connection in vbScript - vbscript

I have an array of database servers, and want to execute the same query for all of them in a loop.
But after the first iteration the I get following error :
- Error Number: 3704
- Description: Operation is not allowed when the object is closed
The code that I've implemented is:
Dim username
Dim password
Dim serverList(4)
serverList(0) = "ServerAddress0"
serverList(1) = "ServerAddress1"
serverList(2) = "ServerAddress2"
serverList(3) = "ServerAddress3"
'username and password are properly set
for counter = 0 to UBound(serverList)-1
Set connObj = CreateObject("ADODB.Connection")
Set rsObj = CreateObject("ADODB.Recordset")
connString = ..........
connObj.Open connString, username, password
'ERROR comes here, in second iteration.
sqlScript = "SELECT * FROM ......"
rsObj.open sqlScript, connObj
Do While Not rsObj.EOF
'record set is fetched.....
rsObj.MoveNext
Loop
'current connection is closed
rsObj.close
connObj.close
Next
Note: For the first iteration this code works perfectly. Error come for second iteration.

(0) Disable "On Error Resume Next" (if you use it)
(1) As UBound() returns the last index, not the size, change
for counter = 0 to UBound(serverList)-1
to
for counter = 0 to UBound(serverList)
(2) As it's possible that you messed up the connection string, publish real code for
connString = ..........
(3) As VBScript has Until change
Do While Not rsObj.EOF
to
Do Until rsObj.EOF
(4) As your recordset is named rsObj change
rsNice.MoveNext
to
rsObj.MoveNext
If you did construct the connection string properly, (4) is the cause of the error.

I just found the solution for this issue:
While iterating over the servers, I was also trying to close connection object for each connection.
......
rsObj.close
connObj.close
Next 'end of iteration over servers.
Now I moved the close statements out of the loop. i.e. while iterating let the objects be open.
......
Next 'end of iteration over servers.
rsObj.close
connObj.close
So, it was all related to the open/close state of the connection.

Related

How to fix error of "Object connection lost from its client" in VBscript"?

my code where I try to login into the website automatically. But it keeps on throwing
The object is lost connection from its client
Dim IE
set IE = WScript.CreateObject("InternetExplorer.Application")
IE.Visible = true
Call IE.navigate("http://finrpa:8080/controlroom/")
Do While IE.ReadyState <> 4
WScript.Sleep 10
Loop
IE.Document.all.username-inputEl.Value = "parthiban.nadar#thirdware.com"
IE.Document.all.password-inputEl.Value = "Thirdw#re1"
Call IE.Document.all.gaia_loginform.submit
Set IE = Nothing
What line the exception is thrown?
I see at least one problem with the script. Because the control names contain dashes, they should be strings. So, instead of
IE.Document.all.username-inputEl.Value = "parthiban.nadar#thirdware.com"
IE.Document.all.password-inputEl.Value = "Thirdw#re1"
should be
IE.Document.getElementById("username-inputEl").Value = "parthiban.nadar#thirdware.com"
IE.Document.getElementById("password-inputEl").Value = "Thirdw#re1"

ASP Classic "Microsoft VBScript runtime error '800a01a8' Object required: 'hashHelp'"

Here I have a double Directory that is suppose to be carrying information from a database in the form of the object hashHelp. Clearly, this isn't working.
Based on everything I've found on this website and around the web, this error message seems to imply that the object hashHelp isn't being created, but you can clearly see it being created above. I have check Any idea what could be happening?
do until rs.eof
if valid(cart, rs, data) = true then
Dim hashHelp
Set hashHelp = new HashHelper
hashHelp.setCode(rs.Fields("Code"))
hashHelp.setDateTime(rs.Fields("ScanTime"))
Dim entry
entry = DateDiff("d", beginDate, DateValue(rs.Fields("ScanTime")))
hash.Item(rs.Fields("ScanTime")).Item(arr(entry)) = hashHelp
arr(DateDiff("d", beginDate, rs.Fields("ScanTime"))) = arr(DateDiff("d", beginDate, rs.Fields("ScanTime"))) + 1
End If
rs.movenext
loop
rs.close
The line the error happens on is hash.Item(rs.Fields("ScanTime")).Item(arr(entry)) = hashHelp
I've checked all the other variable and they are being created and used just fine.

dbDenyWrite still in force after recordset closed

I have some code that gets a value from another table, then updates the value in the table. This code is in a loop that performs for each item in recordset based on another table. The code works the first time through but the next time it errors on the first line saying access is denied because the table is being held by another use or the user interface. The code uses DAO.
Anyone have a clue as to why the dbDenyWrite is still in force after closing the recordset and destroying its reference?
Here is a code snippet:
Set rsRR = DataDB.OpenRecordset("Railroads", dbOpenTable, dbDenyWrite)
rsRR.Index = "Railroads_RRIx"
rsRR.Seek "=", RTrs!RR
If rsRR.NoMatch Then
' Write ERROR MESSAGE
rsRR.Close
Set rsRR = Nothing
GoSub CleanUp
ReverseRouteDataCollect = 0
Exit Function
End If
If Not dWork Is Nothing Then Set dWork = Nothing
Set dWork = New Scripting.Dictionary
FieldsSave dWork, rsRR
i = FieldsCopy(drr, dWork, "TemplatesRailroad")
If dWork(rsRR.Name & "$LastWaybillNo") = "999999" Then
rsRR.Edit
rsRR!LastWaybillNo = 2001
rsRR.Update
Else
rsRR.Edit
rsRR!LastWaybillNo = dWork(rsRR.Name & "$LastWaybillNo") + 1
rsRR.Update
End If
rsRR.Close
Set rsRR = Nothing
.. why the dbDenyWrite is still in force after closing the recordset
and destroying its reference?
Because you only do this in case of a NoMatch.
So either change the dbDenyWrite to allow for edits, or (slower) reset the recordset before starting editing it:
Set rsRR = DataDB.OpenRecordset("Railroads", dbOpenTable)

Looping datareader using For Next VB.NET :There is already an open DataReader associated with this Connection which must be closed first

For x=0 To 2
Dim FO As String
Dim DataQuery1 As String
DataQuery1 = "Select * from dole_employees WHERE fullname like '%" & Temp_Employees_Name & "%'"
mysqlcommand = New MySqlCommand(DataQuery1, mconnection)
readme = mysqlcommand.ExecuteReader()
While readme.Read()
FO = readme(1)
End While
readme.Close()
MsgBox(FO)
Next
After it displays the value in FO once, an unhandled exception error occurs. What should I do with that?
Here is the additional information to unhandled exemption error.
Additional information: There is already an open DataReader associated with this Connection which must be closed first.
But there is already a readme.close() so why does this happen?

Failed to handle null or empty recordset in VB6

It's been a day I've cracked my head to solve this....I've googled for solutions but none of it resolve my issue...
The code is like this:
Private Sub guh()
Dim oConn As Connection
Dim Record As Recordset
Dim SqlStr As String
SqlStr = "select * from dbo.Msg_History where Client_ID='2' AND Update_Msg='4'"
Set oConn = New Connection
With oConn
.CursorLocation = adUseClient
.CommandTimeout = 0
.Open "Provider=SQLOLEDB;Server=127.0.0.1;Initial Catalog=Table_Msg;UID=Admin;PWD="
End With
Set Record = oConn.Execute(SqlStr)
If IsNull(Record) Then
MsgBox "There are no records"
Else
MsgBox "There are records"
End If
oConn.Close
Set oConn = Nothing
End Sub
The sql statement is returning null recordset ..when i run the code...it always go to the "else" condition which is the line MsgBox "There are records"
I've tried change the line : If IsNull(Record) Then
to
If IsNull(Record.Fields(0).Value) Then
but then it throws an error like this:-
error: Either BOF or EOF is true, or the current record has been deleted. Requested operation requires a current record.
I've checked http://support.microsoft.com/kb/304267 and use eof and bof to the condition...n still get the same error..
please anyone help me...
I would use something like this:
' returns true if there is non empty recordset
Function isRSExists(rs) AS boolean
' has to exists as object
If Not rs Is Nothing Then
' has to be opened with recordset (could be empty)
If rs.State > 0 Then
' has to have some records
If Not rs.EOF Then
isRSExists = true
End If
End If
End If
End Function
Change this
If IsNull(Record) Then
to
If Record.RecordCount = 0 Then
I think you can test for if not Record.Eof.
If I recall correctly (it's been a long time), it only works with one type of cursor, I think it should be adUseServer. (EDIT No, it's actually RecordCount which has this problem)
I'll try and dig out some old code to check.
Thanks for the replies guys :D ...will test it later anyway...I havent's tested all of your suggestions at the time I'm posting this reply...I tested this: –
If Record.BOF And Record.EOF Then
and this works...

Resources