Huynh Quang Cuong
May 24th, 1999, 08:06 PM
Hello,
I have a problem when I work with Disconnected Recordset and constant field in SQLServer.
This is my problem.
<<<<<<<<>>>>>>>>>
- I create a Database in SQLServer 7.0 named TestData, it contains a table named table1. The server name is MySQLServer
- I create a Database in Access 97 in C:\Documents\TestData.mdb, it also contains a table named table1
The table1 contains two fields named field1 and field2 (both are text 50). I put some data to test.
<<<<<<<<>>>>>>>>>
In VB6, I create a new Standard EXE project. The project reference to ADO 2.0.
I put a command button on Form1
'=====================Code in Command1_Click============
Private Sub Command1_Click()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strCN_SQL As String 'ConnectionString to SQLServer 7.0 Database
Dim strCN_Access As String 'ConnectionString to Access Database
strCN_SQL = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;"+ _
"Initial Catalog=TestData;Data Source=MySQLServer"
strCN_Access = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;"+ _
"Data Source=C:\Documents\TestData.mdb"
cn.ConnectionString = strCN_SQL 'Note!
cn.CursorLocation = adUseClient
cn.Open
Dim strSQL As String
strSQL = "SELECT table1.field1 , 1 as idx FROM table1" 'Contanst select
rs.Open strSQL, cn, adOpenStatic, adLockOptimistic
With rs
Set .ActiveConnection = Nothing 'Disconnect from data source
Do While Not .EOF
.Fields("field1").Value = "This is new value" 'This line work OK in both case.
.Fields("idx").Value = 100 'This line will be failed when use strCN_SQL connection string!
.MoveNext
Loop
End With
End Sub
'========================================================
This code works OK when I use Access database as data source (cn.ConnectionString = strCN_Access),
but when i use SQLServer as data source ( cn.ConnectionString = strCN_SQL), the assigment at line with code
.Fields("idx").Value = 100 will raise an error:
Run-time error '-214721788(80040e21)': Error occured
What is the problem? It seems that OLE DB Provider for SQL Server does not allow to change constant
field value in result set or I make any mistake?
Is there any way to change the constant field value from SQLServer result set (Disconnected Recordset)
Thanks in advance.
H.Q. Cuong
I have a problem when I work with Disconnected Recordset and constant field in SQLServer.
This is my problem.
<<<<<<<<>>>>>>>>>
- I create a Database in SQLServer 7.0 named TestData, it contains a table named table1. The server name is MySQLServer
- I create a Database in Access 97 in C:\Documents\TestData.mdb, it also contains a table named table1
The table1 contains two fields named field1 and field2 (both are text 50). I put some data to test.
<<<<<<<<>>>>>>>>>
In VB6, I create a new Standard EXE project. The project reference to ADO 2.0.
I put a command button on Form1
'=====================Code in Command1_Click============
Private Sub Command1_Click()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strCN_SQL As String 'ConnectionString to SQLServer 7.0 Database
Dim strCN_Access As String 'ConnectionString to Access Database
strCN_SQL = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;"+ _
"Initial Catalog=TestData;Data Source=MySQLServer"
strCN_Access = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;"+ _
"Data Source=C:\Documents\TestData.mdb"
cn.ConnectionString = strCN_SQL 'Note!
cn.CursorLocation = adUseClient
cn.Open
Dim strSQL As String
strSQL = "SELECT table1.field1 , 1 as idx FROM table1" 'Contanst select
rs.Open strSQL, cn, adOpenStatic, adLockOptimistic
With rs
Set .ActiveConnection = Nothing 'Disconnect from data source
Do While Not .EOF
.Fields("field1").Value = "This is new value" 'This line work OK in both case.
.Fields("idx").Value = 100 'This line will be failed when use strCN_SQL connection string!
.MoveNext
Loop
End With
End Sub
'========================================================
This code works OK when I use Access database as data source (cn.ConnectionString = strCN_Access),
but when i use SQLServer as data source ( cn.ConnectionString = strCN_SQL), the assigment at line with code
.Fields("idx").Value = 100 will raise an error:
Run-time error '-214721788(80040e21)': Error occured
What is the problem? It seems that OLE DB Provider for SQL Server does not allow to change constant
field value in result set or I make any mistake?
Is there any way to change the constant field value from SQLServer result set (Disconnected Recordset)
Thanks in advance.
H.Q. Cuong