VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Combining DBGrid & Combo

by AR. Sithanandam (1 Submission)
Category: Custom Controls/Forms/Menus
Compatability: VB Script
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (3 Votes)

Often I use to thought of using a particular cell of DBGrid control as a Combo box, so that user can select using the combo box.This facility is available in MS Access. This code shows how to combine DBGrid control and a combo box in VB, so that whenever the user clicks a particular cell if DBGrid control this combo box is visible and user can select the value from the combo box.

Rate Combining DBGrid & Combo

STEP1 - Put a DBGrid Control on a Form and connect it to a Data Control
STEP2 - put a combo box over the DBGrid and set the visible property to False and set the
    width of the Combo equal to width of a particular Column of DbGrid control, 
    which you want to populate the data.
STEP3 - Add the following code in Form_Load, DbGrid1_Rowcolchange and Combo1_Click() Events

Private Sub Form_Load()
 
 'POPULATE THE COMBO BOX WITH THE DESIRED VALUES
 Combo1.AddItem "MYTEXT1"
 Combo1.AddItem "MYTEXT2"
 Combo1.AddItem "MYTEXT3"
'ALTERNATIVELY, COMBO BOX CAN ALSO BE POPULATED WITH DATA FROM A TABLE.
End Sub

Private Sub DBGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
 
 Dim Currentrow, CurrentCol
 Currentrow = DBGrid1.Row
 CurrentCol = DBGrid1.Col
 
 ' HERE THE VALUE 2 MEANS, THE PARTICULAR
'  COLUMN WHICH REQUIRES THE LIST OF DATA 
 ' FROM COMBO BOX. 
 If CurrentCol = 2 Then
  Combo1.Visible = True
  Combo1.Top = Me.DBGrid1.RowTop(Currentrow) + DBGrid1.RowHeight 
  Combo1.Left = 1950
  Combo1.Width = 980
  Combo1.Text = DBGrid1.Text ' ASSIGNING
  ' PARTICULAR CELL VALUE OF DBGRID TO COMBO
  Else
  Combo1.Visible = False
 End If
End Sub

Private Sub Combo1_Click()
 DBGrid1.Col = 2
 DBGrid1.Text = Me.Combo1.Text ' ASSIGNING THE
  ' COMBO VALUE TO PARTICULAR CELL OF DBGRID
End Sub

Download this snippet    Add to My Saved Code

Combining DBGrid & Combo Comments

No comments have been posted about Combining DBGrid & Combo. Why not be the first to post a comment about Combining DBGrid & Combo.

Post your comment

Subject:
Message:
0/1000 characters