SP from SQL Server 2005 is not returning records in recordset (VB6) - vb6

Thanks dretzlaff17 for replying,
I am giveing details..........
SP from SQL Server 2005 is not returning the records in recordSet (VB6) records return are -1. If access the records using query and through record set, record set are filling with records.
Same connection string is used. I checked properly and there is no issue in code written in VB6 for command object, then what is the wrong?
Is there any thing else that we have to do while accessing SQL Server 2005.
my code is like
Dim Conn as new ADODB.Connection
Dim RS as new ADODB.RecordSet
Dim CMD as new ADODB.Command
Conn.Open "Connection String" ' Its working
CMD.ActiveConnection = Conn
CMD.CommandType = adCmdStoredProc
CMD.CommandText = "SPName"
Set RS = CMD.Execute
Debug.Print RS.RecordCount ' /* here result is -1 means CMD is not executing and RS is not filling with records */
and if use
RS.Open "Select query", conn 'then this record set is filling with records.
I also check by setting RS (Cursor) location values to client side and SP is simple only select query is present in SP no I/O parameters.
One more thing records are present into database table are not empty.
on this Your thoughts please
Thanks

RS.RecordCount ' /* here result is -1
means CMD is not executing and RS is
not filling with records
No it doesn't: it means the default cursor type does not support the RecordCount property.
As a better test of contents, try:
Debug.Print RS.GetString

Could you show text of you stored proc?
VB6 is old stuff, may be you just need put
set nocount on
at the beginning procedure or may be even execute this as query after opening connection.
If it does not help try simplify stored procedure for something is definitely working like
create stored proc as
set nocount on
select 1

If you want a record count, it's usually best to do this:
set RS = new ADODB.Recordset
RS.Open cmd, , adOpenDynamic, adLockReadOnly
If Not RS.EOF then Debug.Print RS.RecordCount
RS.Close

Related

VB6 insert into table from Recordset

There are one view and one table
both has truly the same columns
but they are in diffrent servers
what I want to do is like below
cn1.ConnectionString = "Server1"
cn2.ConnectionString = "Server2"
sql = "SELECT * FROM VIEW"
Set rs.1ActiveConnection = cn1
rs1.Open sql, cn1
sql = "INSERT INTO table SELECT * FROM view"
cn2.Execute (sql)
I can access to view by cn1, but table by cn2
So this cant be done
I want to know how can it be done
table and view are exactly same
I searched a lot, but there was no good examples for me
I think there are two ways of doing it
inserting recordset into table or inserting each of field to another
easy example would be very helpful thank you
This won't work because, even though you are pulling the records from your first server into your recordset, you are trying to insert directly from the view, rather than inserting from the recordset.
You can loop through your recordset, and insert the records one by one into the other database, but that's a lot of round trips and is very slow. A better way is use UpdateBatch to insert from the recordset all in one go. Here's the basic idea:
cn1.ConnectionString = "Server1"
cn2.ConnectionString = "Server2"
sql = "SELECT * FROM VIEW"
Set rs.ActiveConnection = cn1
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient 'you need a client-side recordset
rs1.Open sql, cn1, adLockBatchOptimistic 'you need to set the locktype to this
Set rs.ActiveConnection = cn2 'now you're connecting your recordset to the other server
rs.UpdateBatch 'and updating the database all in one batch
That should get you started.

ORA-01461: can bind a LONG value only for insert into a LONG column via Access

I am trying to run the below query in Access on a Oracle database :-
UPDATE tblQuotesNew SET tblQuotesNew.Quote_Status = 'Expired'
WHERE tblQuotesNew.Quote_Status='In Progress' AND tblQuotesNew.Date_Quote_Sent<DateAdd('m',-1,Date())
The data type of Quote_Status is VARCHAR2 size 255. The data type of Date_Quote_Sent is Date.
I am connecting to the Oracle database using the code below :-
Dim mydb As DAO.Database
Dim myq As DAO.QueryDef
connectstring = "ODBC;DSN=Comsales;UID=Comsales;PWD=******;SERVER=PDBREPT"
sqltext = "UPDATE tblQuotesNew SET tblQuotesNew.Quote_Status = 'Expired' WHERE tblQuotesNew.Quote_Status='In Progress' AND tblQuotesNew.Date_Quote_Sent<DateAdd('m',-1,Date());"
myq.ReturnsRecords = False
myq.Connect = connectstring
myq.SQL = sqltext
myq.Execute
myq.Close
When I run this query I get a ORA-01461: can bind a LONG value only for insert into a LONG column error.
You're running an Access query (using Access SQL functions) as a Pass-Through query (by setting connectstring = "ODBC;...).
This won't work. Either use Oracle syntax in a Pass-Through query, or Access syntax in a "regular" Access query.
For the latter, tblQuotesNew must be a linked table, and the query connect string must be empty.

Only 1 row in recordset but all rows in table get updated

The query retrieves a single record as is confirmed by the recordcount but every single row in the table gets updated
I am using vb6 and ms ado 2.8
The Firebird version is 2.5.4.26856 (x64).
Firebird ODBC driver 2.0.3.154
The computer is windows 7 home edition 64 bit
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cs As String
Dim dbPath As String
dbPath = "c:\Parkes\Parkes.fdb"
cs = "DRIVER={Firebird/Interbase(r) Driver}; DBNAME=localhost:" & dbPath & "; UID=SYSDBA; PWD=masterkey;"
cn.ConnectionString = cs
cn.Open
Dim sQuery As String
sQuery = "select memo from clients where clientID = 10021 "
rs.Open sQuery, cn, adOpenStatic, adLockOptimistic
If rs.BOF <> True Or rs.EOF <> True Then
'putting msgbox rs.recordcount here confirms only 1 record in recordset
rs.Movefirst
rs.Fields("memo") = "blah"
rs.Update
End If
Set rs = Nothing
Set cn = Nothing
If I alter the query slightly by also selecting a second column, the client surname then only rows with the same value in the surname column as that of of the row where the clientid is 10021 get edited.
sQuery = "select memo, surname from clients where clientID = 10021 "
I cannot understand how more than one row should be edited when the recordset contains only a single row
EDIT: Having read around the web a bit this is my understanding of what is happening.
It seems that the update method identifies which records to update based on the selected columns in the recordset.
So if you select fields a,b,c,d and are updating field a, it will only update records in the database whose values for a,b,c,d match those in the recordset.
The best way to ensure that you only update a single record is to include the primary key in the selected fields.
So if I had written my query as in the line below, only a single record would have been updated because the clientID column contains unique values.
sQuery = "select memo, clientID from clients where clientID = 10021 "
It makes sense thinking about it but the way I wrote the query originally seems to work fine, in my experience, with other databases or am I wrong?
I tested your code everything was fine and only update one row. I only want to suggest you a simple way to check whether record exist or not. that could be like this:
if rs.rows.count > 0 then
' no need to move recordset to first by default its on the first row
end if

How to deal with DBGrid control?

SQL> desc admin
Name Null? Type
-------------------------------------
NAME VARCHAR2(20)
PRIVILEDGE VARCHAR2(20)
CODE NUMBER(38)
PASS VARCHAR2(20)
Private Sub Form_Load()
Set RS = New ADODB.Recordset
If RS.State = 1 Then RS.Close
opencn
RS.Open "ADMIN", CN, adOpenDynamic, adLockOptimistic, adCmdTable
Set DBGrid1.DataSource = RS
RS.Close
End Sub
Error I'm getting
*Error 430 at line : Set DBGrid1.DataSource = RS
Class does not support automation or does not support expected interface*
The Data Bound Grid (DBGrid) is for use with DAO, while the DataGrid is for use with ADO.
In the toolbox: right click, select 'Components...', and be sure 'Microsoft DataGrid Control 6.0 (SP6) (OLEDB)' is selected, not 'Microsoft Data Bound Grid Control 5.0 (SP3)'.

VB6 Recordsets and SQL count

I am noticing that the number of records in a database table (select reference from datetable) suddenly increases when I start running the program below even though there are no new records added. Please note that I establish that the number of rows increases by running a query in SQL Studio Manager i.e. select reference from datetable. When the program stops; the number of records falls back to the original level. Here is the code. Why does this happen? There is no Primary Key in the table though Reference is unique.
rs.Open "select reference,value1,datefield from datetable where field1 = 'value1' " & _
"order by reference", objAuditCon.ActiveCon, adOpenStatic, adLockPessimistic
Do While Not rs.EOF
intReadCount = intReadCount + 1
DoEvents
If Not IsNull(rs("value1")) Then
testArray = Split(rs("value1"), ",")
rs2.Open "SELECT Date FROM TBL_TestTable WHERE Record_URN = '" & testArray(1) & "'", objSystemCon.ActiveCon, adOpenStatic, adLockReadOnly
If rs2.EOF Then
End If
If Not rs2.EOF Then
rs("DateField") = Format$(rs2("Date"), "dd mmm yy h:mm:ss")
rs.Update
intWriteCount = intWriteCount + 1
End If
rs2.Close
Else
End If
rs.MoveNext
Loop
rs.Close
It's a little confused, but if you are referring to the total given by "intReadCount" against how many rows you have in the table then it looks as though you are not correctly clearing this value. At the beginning of the procedure you would want to set "intReadCount" back to 0 before starting, you should then get constant results.
Updated: See comments below

Resources