• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

Simple question - DataGrid and Combo

Hi everyone!
I have one ComboBox and one Datagrid and i want to show information from an Access database. I don't want to use ADODC , just an ADODB.Recordset

I have this code written but it didn't help me much

For the ComboBox

Dim rs_reg As ADODB.Recordset
Set rs_reg = New ADODB.Recordset
rs_reg.ActiveConnection = Conbase
rs_reg.Source = "SELECT MyTable.* FROM MyTable;"
rs_reg.Open , , 3, adLockOptimistic

Set Cmbgener.DataSource = rs_reg
Cmbgener.DataField = rs_reg!Field1
Cmbgener.Refresh

For the datagrid

Dim rs_data As ADODB.Recordset
Set rs_data = New ADODB.Recordset
rs_data.ActiveConnection = Conbase
rs_data.Source = "SELECT MyTable.* FROM MyTable;"
rs_data.Open , , 3, adLockOptimistic

Set Dg1.DataSource = rs_data
Dg1.Refresh


I have the datagrid and the combobox in different forms
Thanks
Schachmann
0
schachmann
Asked:
schachmann
2 Solutions
 
RiverGuyCommented:
How about this for the combo box

Dim intCount As Integer
    rst1.MoveFirst
    Do Until rst1.EOF
        With Combo1
            .AddItem rst1![descriptionField]
            .ItemData(intCount) = rst1![IDField]
        End With
        intCount = intCount + 1
        rst1.MoveNext
    Loop
0
 
schachmannAuthor Commented:
RiverGuy:
i need to use datasource. I know that code you typed.
And how about the datagrid? any suggestions?
0
 
RiverGuyCommented:
You need to use the datasource?  Why?  Are you wanting to bind this to lookup from one table and to change data in another table?  Have you thought about using a DataCombo?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
supunrCommented:
You can not bind a recordset to a combo box.  If you want to do that, then you have to use "Microsoft DataList Controls 6.0" and have a modular recordset open.  Otherwise, you have to manually add the records for the combo box.

To add records to combo box...

Dim rs_reg As ADODB.Recordset
Set rs_reg = New ADODB.Recordset
rs_reg.ActiveConnection = Conbase
rs_reg.Source = "SELECT MyTable.* FROM MyTable;"
rs_reg.Open , , 3, adLockOptimistic

combo1.Clear ' remove all items
if (re_reg.RecordCount > 0) then rs_reg.MoveFirst
Do While (NOT rs_reg.EOF)
     Cmbgener.AddItem rs_reg.Fields("FieldName").Value
     rs_reg.MoveNext
Loop

However if you use data combo, then you only have to say...
Set DataCombo1.RowSource = rs_reg
' but make sure the rs_reg is open at this point and it is open during the whole process.

For datagrid, I found that if the recordset contains no records, the grid behaves strangly after refreshing.  So make sure to check if the recordcount is > 0

Set Dg1.DataSource = Nothing
Dg1.ClearFields
if (rs_data.RecordCount > 0) then
     Set Dg1.DataSource = rs_data
End if
0
 
schachmannAuthor Commented:
supunr:
I tried your code. I tried to connect the database with ADO and it work fine. But when i try to do it with adodb y don't get any records. And i checked for recordcount and it is bigger than 0. Any ideas?
Thanks again
schachmann
0
 
schachmannAuthor Commented:
Sorry!
i solved the combo stuff. I did what you said RiverGuy and supunr and worked. I thought you could do it the same way with combobox and datacombo.
The problem is with the datagrid
0
 
RiverGuyCommented:
0
 
schachmannAuthor Commented:
HI!
i´ve splitted the points.
check the link for additional info.
http://www.experts-exchange.com/Community_Support/Q_20637996.html
Thanks to both of you
0
 
SpideyModCommented:
schachmann,
You can split the points yourself now.  Look just above the comment area, there is a link called "Spilt"  Click it and follow the prompts.

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now