VB6 Insert query

I am writing code in VB6 which inserts data into a database on an SQL 2000 server, I have no problem inserting data but I cannot read it back. Please see code below.
For some reason it does not read data and rst.RecordCount is always -1

Public cnn As ADODB.Connection
Public rst As ADODB.Recordset

Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset

strConnect = ("DSN=Test1;Uid=menta;Pwd=e62185;")
cnn.Open strConnect

strSQL = "insert Staging (A) VALUES ('7')

rst.Open CStr(strSQL), cnn

strSQL = "select * from Staging"
rst.Open CStr(strSQL), cnn

if rst.RecordCount > 0 then

End If
[702 byte] By [atuvy] at [2007-11-20 11:54:13]
# 1 Re: VB6 Insert query
I have an error in the title, it should be "select"
atuvy at 2007-11-9 19:32:05 >
# 2 Re: VB6 Insert query
Several things.

1. Please use CODE Tags.
2. Copy and paste your code between the code tags. If you re-type your code you are liable to make mistakes and mis-lead people.
3. This may be a typing mistake, but Insert is usually in the form of INSERT INTO TableName...
4. You are using a recordset for the execution of your Insert Command. Look to this (http://www.dev-archive.com/forum/showthread.php?t=313038&highlight=ado+INSERT) to see about executing the INSERT command from the connection.
5. It is generally not a good idea to put username and passwords into post. Get into the habit of scrubbing all info like this when posting to forums. In your case, it won't make much difference, however, in a future case it is possible it might.

You SELECT code looks like if might be OK.
sotoasty at 2007-11-9 19:33:06 >
# 3 Re: VB6 Insert query
Thanks, Actualy my problem is with the Select portion, I have no problem inserting but nuthing comes up when I select
atuvy at 2007-11-9 19:33:59 >
# 4 Re: VB6 Insert query
You must have left out the INTO in your post. Try closing the rs.

Set rst = Nothing
Set rst = New ADODB.Recordset

and then reopening it
dglienna at 2007-11-9 19:35:10 >
# 5 Re: VB6 Insert query
u have to use cursor at side of the client


rst.CursorLocation=adUseClient
rst.Open CStr(strSQL), cnn

if rst.RecordCount > 0 then

End If
hensa22 at 2007-11-9 19:36:09 >
# 6 Re: VB6 Insert query
Thats right !

To read the recordcount you should have static copy of the data. with server side cursor you are having dynamic copy with you.
Shaikh.Riyaz.a at 2007-11-9 19:37:08 >
# 7 Re: VB6 Insert query
what do you mean by "use curser"
atuvy at 2007-11-9 19:38:04 >
# 8 Re: VB6 Insert query
what do you mean by "use curser"

When you open a recordset, you can fetch the data in two ways, viz dynamic and static.

In static fetching you get the copy of whole data at client side so that you can not see changes made by the other user. In client side cursor you can get recordcount while in server side cursors data is fetched dynamically so that you can see changes made by other users. For dynamic data all queries, search and filter are executed on the server hence it is slow.
Shaikh.Riyaz.a at 2007-11-9 19:39:07 >
# 9 Re: VB6 Insert query
Works well now, I have rst defined as "Public rst As ADODB.Recordset", I am having a problem with repeated queries, do I need to do somthing like rst.Close after each query in order to allow for the next query?
atuvy at 2007-11-9 19:40:09 >
# 10 Re: VB6 Insert query
Read post #5 again
dglienna at 2007-11-9 19:41:08 >
# 11 Re: VB6 Insert query
Yes, it works now

I need to assign a value to one of the fields in the record but I am getting an error message. I assume it is because of adOpenStatic, is that correct, how do I enter the data?

rst.Open strSQL, cnn, adOpenStatic
If rst.RecordCount > 0 Then
rst.MoveFirst
rst.Fields("XXXXX") = 2
End If
atuvy at 2007-11-9 19:42:07 >
# 12 Re: VB6 Insert query
adLockOptimistic solved the problem
atuvy at 2007-11-9 19:43:10 >