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

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

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.

Read one column values from database and and sort randomly and put the sorted values into other column

I have a requirement to read one complete column values from Access database and need to sort it in a random order/ Descending order and update the sorted values to another column so , that one name is assigned to another name. That to only using VB script.
I know only how to read the values from Access database i don't know further steps..
Could any one help me in this?
One Recordset sort Desc, second Recordset sort Asc. Then loop through the first RS and update the table with the first RS value where table value equals second RS value.
In this example change ColumnName, ColumnName2 and TableName according to your database. ColumnName2 is output column so it will be replaced with new values.
Sub AddDescToAsc()
Dim MyDB As DAO.Database, MyRS As DAO.Recordset, MyRS2 As DAO.Recordset
Set MyDB = CurrentDb()
Set MyRS = MyDB.OpenRecordset("SELECT ColumnName FROM TableName ORDER BY ColumnName DESC", dbOpenForwardOnly)
Set MyRS2 = MyDB.OpenRecordset("SELECT ColumnName FROM TableName ORDER BY ColumnName ASC")
MyRS2.MoveFirst
With MyRS
Do While Not .EOF
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE TableName SET ColumnName2 = '" & .Fields("ColumnName") & "' WHERE ColumnName = '" & MyRS2(0) & "'"
DoCmd.SetWarnings True
MyRS2.MoveNext
.MoveNext
Loop
End With
MyRS.Close
MyRS2.Close
End Sub

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

SP from SQL Server 2005 is not returning records in recordset (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

When I select the value from the combo box, related value should appear in the text box

When I select the value from the combo box, related value should appear in the text box
ComboBox code.
cmd.CommandText = "select distinct PERSONID from T_PERSON"
Set rs = cmd.Execute
While Not rs.EOF
If Not IsNull(rs("PersonID")) Then
txtno.AddItem rs("PersonID")
End If
rs.MoveNext
Wend
In comboBox list of ID is displaying, when I select the particular person id, Name should display in text box related to the personid
Text Box
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno & " '"
Set rs = cmd.Execute
While Not rs.EOF
If Not IsNull(rs("Name")) Then
txtName.Text = rs("Name")
rs.MoveNext
End If
Wend
I put the above code in Form_Load Event, Nothing displaying in Text Box.
What wrong in my code.
Need VB6 code Help
You would want the 2nd block of code in the the click event for the combobox.
Edit
There looks like another couple of issues in your code at this line:
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno & " '"
2 Issues:
You are passing in the control itself as the person ID, not the selected value.
You have an extra space in your query after the person ID
You should change that line to be:
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno.SelectedItem.Text & "'"
Why not have the combobox display the name and hold the personID as it's item data?
cmd.CommandText = "select distinct PERSONID, Name from T_PERSON WHERE PersonID IS NOT NULL"
Set rs = cmd.Execute
While Not rs.EOF
combo.AddItem rs("Name").value
combo.ItemData(combo.NewIndex) = rs("PERSONID").value
rs.MoveNext
Wend
Then, if you need the PersonID for the selected name you can just grab combo.ItemData(combo.ListIndex).

Resources