473,466 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to add data from DataGrid into ComboBox list?

Mas Juliza Alias
67 New Member
Hi,
How to add the list of a combo box with a data in a table from data grid?
I have a data grid connected to a .mdb database in one form, and two combo boxes connected to the same database in another form. I am using Data Environment to connect the objects to the database. I've set the combo boxes' properties, DataSource, DataMember and DataField to the Data Environment, data Table and data Field respectively. The problem is, only the first data in the field is shown on both combo boxes' lists. How to add the rest of data into the drop down list?
(see attachment)
Thank you in advance.
Attached Files
File Type: zip ComboBox & DataEnvironment.zip (18.5 KB, 517 views)
Apr 24 '12 #1

✓ answered by Guido Geurs

I hope this code will help you:
It runs through the datagrid and collects first the unique dates in an array.
Once all the unique dates found, it add s them to the comboboxes.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. Dim ARRREC() As String
  3. Dim ARRRECidx As Integer
  4. Dim RECSET As ADODB.Recordset
  5. Dim RECSETdate As String
  6. Dim PRESENT As Boolean
  7.    Set RECSET = DataEnvironment1.rsCommand1 '§ connect the recordset to the database
  8.    ReDim ARRREC(0)
  9.    '§ put unique dates in the array
  10.    With RECSET
  11.       Do Until .EOF
  12.          RECSETdate = .Fields(0)
  13.          PRESENT = False
  14.          '§ check array if retreved date exist in array
  15.          For ARRRECidx = 0 To UBound(ARRREC)
  16.             If RECSETdate = ARRREC(ARRRECidx) Then PRESENT = True
  17.          Next
  18.          '§ if not present: add
  19.          If PRESENT = False Then
  20.             ReDim Preserve ARRREC(UBound(ARRREC) + 1)
  21.             ARRREC(UBound(ARRREC)) = RECSETdate
  22.          End If
  23.          .MoveNext '§ go to next row
  24.       Loop
  25.       .MoveFirst '§ go back to top of list
  26.    End With
  27.    With frmComboBox
  28.       '§ dump array in the comboboxes
  29.       For ARRRECidx = 1 To UBound(ARRREC)
  30.          .cboFromDate.AddItem ARRREC(ARRRECidx)
  31.          .cboToDate.AddItem ARRREC(ARRRECidx)
  32.       Next
  33.       .Show
  34.    End With
  35.    Set RECSET = Nothing '§ clean memory
  36.    Me.Hide
  37. End Sub
PS:
it's better to collect the unique dates then to connect to the database and put all the dates in the combobox because the dates are more than once in the records.

8 5384
Guido Geurs
767 Recognized Expert Contributor
I hope this code will help you:
It runs through the datagrid and collects first the unique dates in an array.
Once all the unique dates found, it add s them to the comboboxes.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. Dim ARRREC() As String
  3. Dim ARRRECidx As Integer
  4. Dim RECSET As ADODB.Recordset
  5. Dim RECSETdate As String
  6. Dim PRESENT As Boolean
  7.    Set RECSET = DataEnvironment1.rsCommand1 '§ connect the recordset to the database
  8.    ReDim ARRREC(0)
  9.    '§ put unique dates in the array
  10.    With RECSET
  11.       Do Until .EOF
  12.          RECSETdate = .Fields(0)
  13.          PRESENT = False
  14.          '§ check array if retreved date exist in array
  15.          For ARRRECidx = 0 To UBound(ARRREC)
  16.             If RECSETdate = ARRREC(ARRRECidx) Then PRESENT = True
  17.          Next
  18.          '§ if not present: add
  19.          If PRESENT = False Then
  20.             ReDim Preserve ARRREC(UBound(ARRREC) + 1)
  21.             ARRREC(UBound(ARRREC)) = RECSETdate
  22.          End If
  23.          .MoveNext '§ go to next row
  24.       Loop
  25.       .MoveFirst '§ go back to top of list
  26.    End With
  27.    With frmComboBox
  28.       '§ dump array in the comboboxes
  29.       For ARRRECidx = 1 To UBound(ARRREC)
  30.          .cboFromDate.AddItem ARRREC(ARRRECidx)
  31.          .cboToDate.AddItem ARRREC(ARRRECidx)
  32.       Next
  33.       .Show
  34.    End With
  35.    Set RECSET = Nothing '§ clean memory
  36.    Me.Hide
  37. End Sub
PS:
it's better to collect the unique dates then to connect to the database and put all the dates in the combobox because the dates are more than once in the records.
Apr 26 '12 #2
Mas Juliza Alias
67 New Member
You are such a HERO! It works really well! Thanks a lot! :)
Apr 27 '12 #3
Guido Geurs
767 Recognized Expert Contributor
If you want to work with large DBfiles, it's better to put the DB in an array (much faster , only load once) like :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. Dim RECSET As ADODB.Recordset '§ recordset from database
  3. Dim ARRrecsetRECORDSidx As Integer
  4. Dim ARRdate() As String
  5. Dim ARRdateIdx As Integer
  6. Dim PRESENT As Boolean
  7. '§ put database in array
  8.    Set RECSET = DataEnvironment1.rsCommand1 '§ connect the recordset to the database
  9.    ARRrecset = RECSET.GetRows
  10.       '§ !! the records in the array are HORIZONTAL !! the attributes are VERTICAL
  11.       '§ 1st counter (vertical) => attributes
  12.       '§ 2nd counter (horizontal) => records
  13. '§ put unique dates in the ARRdate
  14.    ReDim ARRdate(0)
  15.    '§ => run through records with 2nd counter
  16.    For ARRrecsetRECORDSidx = 0 To UBound(ARRrecset, 2)
  17.       PRESENT = False
  18.       For ARRdateIdx = 0 To UBound(ARRdate)
  19.          '§ attribute 0 = Date
  20.          If ARRdate(ARRdateIdx) = ARRrecset(0, ARRrecsetRECORDSidx) Then PRESENT = True
  21.       Next
  22.       If PRESENT = False Then
  23.          ReDim Preserve ARRdate(UBound(ARRdate) + 1)
  24.          ARRdate(UBound(ARRdate)) = ARRrecset(0, ARRrecsetRECORDSidx)
  25.       End If
  26.    Next
  27. '§ Fill the Comboboxes
  28.    With frmComboBox
  29.       '§ dump array in the comboboxes
  30.       For ARRdateIdx = 1 To UBound(ARRdate)
  31.          .cboFromDate.AddItem ARRdate(ARRdateIdx)
  32.          .cboToDate.AddItem ARRdate(ARRdateIdx)
  33.       Next
  34.       .Show
  35.    End With
  36.    Set RECSET = Nothing '§ clean memory : records are in array ARRrecset
  37.    Me.Hide
  38. End Sub
To filter the data "Volume" and "Area" (from date in Combobox): run through array and put in textbox:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdFindFromDate_Click()
  2. Dim ARRrecsetIdx As Integer
  3. '§ check combobox text
  4.    If cboFromDate.Text = "" Then
  5.       MsgBox "Select a date in the combobox"
  6.       Exit Sub
  7.    End If
  8. '§ find Volume and Area with combobox text
  9.    With TxtFromDate
  10.       .Text = ""
  11.       '§ !! the records in the array are HORIZONTAL !! the attributes are VERTICAL
  12.       '§ => run through records with 2nd counter
  13.       '§ 1st counter = vertical (0=Date, 1=Time, 2=Level, 3=Volume, 4=Area)
  14.       '§ 2nd counter = horizontal (records)
  15.       For ARRrecsetIdx = 0 To UBound(ARRrecset, 2)
  16.          '§ attribute 0 = Date
  17.          If ARRrecset(0, ARRrecsetIdx) = cboFromDate.Text Then
  18.             '§ attribute 3 = Volume, attribute 4 = Area
  19.             .Text = .Text & "Volume= " & ARRrecset(3, ARRrecsetIdx) _
  20.                      & "   Area= " & ARRrecset(4, ARRrecsetIdx) & vbNewLine
  21.          End If
  22.       Next
  23.    End With
  24. End Sub
Apr 28 '12 #4
Mas Juliza Alias
67 New Member
In the code, which line did you assign the first attribute of the ARRrescet into ARRdate?
Apr 30 '12 #5
Mas Juliza Alias
67 New Member
I got it. Brilliant use of 'For' and 'If'. I would never thought of that kind of solution. Thank You...
Apr 30 '12 #6
Guido Geurs
767 Recognized Expert Contributor
Do you know how to work with "Add Watch" and "Watch Window" ?
Apr 30 '12 #7
Mas Juliza Alias
67 New Member
Never use it... Why?
Apr 30 '12 #8
Guido Geurs
767 Recognized Expert Contributor
It's a very handy tool in solving problems and to have a view in the structure of the components properties, var's and array's.
I will write a document explaining how it works.
Apr 30 '12 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Susan Bricker | last post by:
The following error: "The current field must match the join key '?' in the table that seves as t the 'one' side of one-to-many relationship. Enter a record in the 'one' side table with the...
3
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found...
2
by: john sutor | last post by:
Does anyone know how to create a combobox in a standard datagrid? I can create check boxes , but not the combobox
0
by: AR | last post by:
I have a datagrid containing a single combobox column. It also contains an 'Add New Row' button at the bottom of the datagrid. The datagrid is in a form that contains 'Save' and 'Restore' buttons....
5
by: Keith G | last post by:
I am using Visual Studio 2003. In the standard combobox control it would appear that only 1 column of data can be displayed in the list (as stipulated in the DisplayMember property). In VBA it was...
2
by: Crazy Cat | last post by:
I am using a data-bound combobox with dropdownstyle set to dropdownlist. Teh combobox is bound to a bindingsource which is in turn bound to a table in my database. The table has only 4 rows and I...
2
by: mnms | last post by:
Hi, I'm wondering if it's possible "manually" add an extra value to a combobox list. At the moment I have two fields, one "transparent" is a checkbox that lets you define a colour as...
0
by: Kysonel | last post by:
Hello ! I have been trying to use the datagridComboColumnStyle that was decribed in this thread : ...
0
by: SteveArmstrong | last post by:
This code is used in conjunction with an excel spreadsheet, i need to be able to remove each item from the comboBox list as the item is selected this can be in any order and if and when the last item...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.