How to deal with DBGrid control? - oracle

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)'.

Related

DataGrid in VB6

I am working on a VB6 project that uses a MS Access database. I want to display the value of a column in DataGrid using the following logic:
1 for a value of 1
2 for a value of 2
0 for other values
Can anyone help me?
You can add the logic directly into your SQL statement using IIF statement:
Select IIF(YourValue < 2, YourValue, 0) as NewValue, * FROM YourTable
This code loads records from a YourTable table (please update with your table name) and applies the logic your described to the YourValue field (if value is smaller than 2, return 0):
Dim objConnection As ADODB.Connection
Dim objRecordset As New ADODB.Recordset
Dim sSQLStatement As String
Set objConnection = New ADODB.Connection
Set objRecordset = New ADODB.Recordset
sSQLStatement = "Select IIF(YourValue < 2, YourValue, 0) as NewValue, * FROM YourTable"
objConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\YourDatabase.mdb"
objConnection.Open
objRecordset.CursorLocation = adUseClient
objRecordset.Open sSQLStatement, objConnection, adOpenStatic, adLockOptimistic
Set DataGrid1.DataSource = objRecordset
You can add this logic to more columns, you simply need to add extra IIF statments to your sSQLStatement string:
Select IIF(Value1 < 2, Value1, 0) as NewValue1, IIF(Value2 < 2, Value2, 0) as NewValue2, IIF(Value3 < 2, Value3, 0) as NewValue3 FROM YourTable

Error '80004005' only when SELECT DISTINCT with ASP Classic

I am developing in ASP VBScript at work and need to run a SELECT DISTINCT query but I am having some troubles.
I have other queries in my code that work perfectly fine, that do not use SELECT DISTINCT.
Here is what I am using:
Dim sections()
c = 1
set conn=Server.CreateObject("ADODB.Connection")
set rs=Server.CreateObject("ADODB.Recordset")
conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=******;User ID=Admin;" & _
"DATA SOURCE=" & Server.MapPath("modules.mdb")
rs.open "SELECT DISTINCT section FROM modules WHERE area='First' ORDER BY lvl ASC",conn
ReDim sections(10)
do while not rs.EOF
sections(c) = rs("section")
c = c + 1
rs.MoveNext
loop
rs.Close
conn.Close
set rs = nothing
set conn = nothing
Which gives me this error:
error '80004005'
on the line of the SQL query
The only way to fix this is to use "GROUP BY" instead of "DISTINCT"
SELECT DISTINCT section FROM modules WHERE area='First' ORDER BY lvl ASC
SELECT section FROM modules WHERE area='First' GROUP BY section ORDER BY lvl ASC

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

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