Can't access multiple records being returned by sql via ADODB.Recordset object - vbscript

When I execut the sql direct it returns 4 records. when I execute the oraCommand.Execute the oraResults object shows only 1 record returned it should have a count of 4.
Hence I can get the value and name for the single record. Question is how do I get the other 3 records? I have tried set oraResults=oraResults.NextRecordSet() and I get error saying not supported!
Any help will appreciated.
This is the snippet of the code:
set oraCommand = CreateObject("ADODB.Command")
'set connection and sql statement
set oraCommand.ActiveConnection = oraConnection
oraCommand.CommandText =oraSQLStatement
oraCommand.CommandType = 1 ' commandText
oraCommand.Prepared = True
set oraResults = CreateObject("ADODB.Recordset")
'run the sql
set oraResults = oraCommand.Execute
oraResults.Fields(0).Name - Name from first record is returned
oraResults.Fields(0)>Value - Value from first record is returned

Try something like this: loop through the oraResults recordset and read the records:
Do until oraResults.EOF
Response.Write oraResults("firstFieldName")
Response.Write oraResults("secondFieldName")
oraResults.MoveNext
Loop
Here's a link that might help.

Related

ADODB Recordset not able to extract the 'XMLData' (xml format data) from SQL database

I have a SQL table which is containing some xml data like below -
xmlData
I wrote a VBS code which is extract all the SQL column data excluding "XMLData"
my Dummy VBS Code -
Sub getdata()
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Connection String"
Set objRecordSet = CreateObject("ADODB.Recordset")
sqlquery = "select * from TableName WHERE IsExecute = 'Yes'"
Set rs = objConnection.Execute(sqlquery)
If Recordset.EOF Then
msgbox "There are no records to retrieve; Check that you have the correct job number."
Else
'if there are records then loop through the fields
Do While NOT Recordset.Eof
field = Recordset("xmlData")
if field <> "" then
Msgbox field
end if
Recordset.MoveNext
Loop
End Sub
Need a help to get xml format data from SQL database.
like -
***xmlData
<
EndResync/> ***
Thanks
RaviK

Unspecified error while opening a connection to database

Good eve guys.
I'm having a problem with my code.
The goal is, when I type a letter in the combobox, it wiill show a list of possible model names.
But whenever I type in that combobox, it gives me an error.
Here's the code I'm working on:
Private Sub cmbSearch_Change()
Dim conn As New ADODB.Connection
Dim record As New ADODB.Recordset
Dim model As String
model = cmbSearch.Text
If cmbSearch.Text <> "" Then
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & App.Path & "\Database.mdb"
cmbSearch.Clear
record.Open "SELECT Model FROM LaptopSpecs WHERE Model LIKE '" & model & "%' ORDER BY Model", conn, 3, 3
If record.RecordCount > 0 Then
Do While Not record.EOF
cmbSearch.AddItem record.Fields("Model").Value
record.MoveNext
Loop
End If
cmbSearch.Text = model
End If
Set record = Nothing
Set conn = Nothing
End Sub
In the part conn.open, the error message pops up.
run-time error '-2147467259(80004005)': unspecified error
Assuming you are using Access:
This question isn't about Access, but if it where:
Expanding on this answer, I recommend removing all the code in your example, and replacing it with this (done in Form design mode):
Set the cmbSearch.RowSource property to a fixed query like:
SELECT Model FROM LaptopSpecs ORDER BY Model
Set the cmbSearch Auto Expand to True.
This will work for 98% of all ComboBox type-to-select scenarios, and should work for your example.

How to browse a query result?

I have a query:
sqlPU = "SELECT num_uti FROM myTable WHERE C_IDENT_A = '5'
but I didn't know to browse my result.
I try this but it doesn't work
req.Open sqlPU, oConn
If Not req.EOF Then
Set resultSet = oConn.Execute(sqlPU)
For Each result In resultSet
WScript.Echo resultSet.Fields(num_uti).Value
Next
End if
Try restructuring the code like this;
Dim oConn, req, sqlPU
'Create connection
Set oConn = CreateObject("ADODB.Connection")
Call oConn.Open("yourconnectionstring")
'Set query
sqlPU = "SELECT num_uti FROM myTable WHERE C_IDENT_A = '5'"
'Open recordset
Set req = CreateObject("ADODB.Recordset")
Call req.Open(sqlPU, oConn)
'Loop the return data
Do While Not req.EOF
'Output field called num_uti.
WScript.Echo req.Fields("num_uti").Value
'Move to next record
Call req.MoveNext()
Loop
'Clean-up
Call req.Close()
Set req = Nothing
Call oConn.Close()
Set oConn = Nothing
The code in the question has a few issues,
The sqlPU query string is missing a string termination character (") and will cause a syntax error.
When you call .Open() on a ADODB.Recordset it is populated and can be traversed using a Do loop and .MoveNext() method, the extra .Execute() you do is not required and re-runs the same query again. The .MoveNext() method tells the ADODB.Recordset to move it's pointer 1 record, once it reaches the end the value of .EOF will equal True. Without .MoveNext() the pointer will not move past the end of the file (EOF) and the loop will run indefinitely or until the script falls over, neither of which is good.
Referencing columns using .Fields() collection requires either an index (ordinal numeric value starting from 0) or a string containing the alias of the column (in this case "num_uti"). In the original code the column alias wasn't a string which means VBScript would assume it's a variable called num_uti and because num_uti is uninitialised it would fail.

Classic ASP Iterate through and Object

I'm a PHP developer, learning ASP.
I've become very reliant on PHP's useful functions: print_r() and var_dump() to see what an array or object contains.
I don't always know what columns are in a Db Table. So, when a SELECT * From Tbl is queried, and the objRS is populated, would I be able to view what the entire object's contents are?
Is this possible in ASP?
<% `my simple Select statement
Dim strDbConnection
Dim objConn
Dim objRS
Dim strSQL
strDbConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test.mdb;"
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open(strDbConnection)
strSQL = "SELECT * FROM persons"
Set objRS = objConn.Execute(strSQL)
If objRS.EOF Then
Response.Write("No items found")
Else
Do While Not objRS.EOF
' show all columns I can extract here....
objRS.MoveNext()
Loop
End If
objRS.Close()
Set objRS = Nothing
objConn.Close()
Set objConn = Nothing
%>
-- I would like to see what's coming back at me in the objRS, and then cherry-pick the columns after I know what I have access to.
Is there something similar to what I'm used to?
ie: print_r()
You can use objRS.fileds(j).name and objRS.fields(j).value to get the name of columns and values.
For example:
[...]
for j = 0 to objRS.fields.count - 1
response.write(objRS.fields(j).name & " = " & objRS.fields(j).value)
next

How to deal with textbox validation?

Private Sub txtUserCode_Validate(Cancel As Boolean)
If RS!ID = txtUserCode.Text Then
SQL = "SELECT NAME,PRIVILEDGE FROM ADMIN WHERE CODE=" & txtUserCode.Text
Set RS = CN.Execute(SQL)
txtUserName.Text = RS!NAME
Else
MsgBox "ENTER VALID NO"
txtUserCode.Text = ""
Cancel = True
End If
End Sub
In this code I want to execute like:
If I enter the ID present in table then it'll show info but it's considering 1st record (RS!ID(0)) only not the next one
If I enter the ID which is not present in table then it should not throw error
3021- Requested operation requires current record but goto else part.
Please Help
I am assuming RS is a recordset.
Depending on the RS type, you can try and Find a record like this:
RS.MoveFirst
RS.Find("[CODE]=" & txtUserCode.Text)
If Not RS.EOF Then
' found!
End If
Link to the ADO Find function.

Resources