Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

How do DataGrid and ComboBox Interact?

I am using VB 6 and MS DataGrid control 6.0 with an Oracle database.

I have not worked with a datagrid before and I inherited this program from a person who is no longer with the company.

On the field I am having a problem with, the datagrid (dgParams) is working with a combobox (cmbSeismicCategory).

PROBLEM:
User simply clicks on the dgParams datagrid column that has the cmbSeismicCategory combobox when the value is null.  Without the user even selecting a value in the combobox, the value changes in the database to last
value on the combobox selection list.

I have isolated where the problem is occurring but do not understand what is happening and why.

OBSERVATIONS:
There is not a default in the Oracle database so I focused on the program.

I am following dgParams.Text and cmbSeismicCategory.Text through the program.  Reference the code below.  Through LINE 3, dgParams.Text and cmbSeismicCategory.Text both equal null.

After LINE 3, the program moves to LINE 11 then dgParams.Text equals null and cmbSeismicCategory.Text equals the last value on the combobox selection list which happens to be "N/A".

Of course, LINE 12 changes dgParams.Text to "N/A" and all this has occurred with the user simply clicking on the datagrid column.

LINE 1  Private Sub FillCombo(Temp As ComboBox, StrValue As String)
LINE 2      Dim i As Integer
LINE 3      Temp.ListIndex = Temp.ListCount - 1
LINE 4      For i = 0 To Temp.ListCount - 1
LINE 5          If Temp.List(i) = StrValue Then
LINE 6              Temp.ListIndex = i
LINE 7              Exit For
LINE 8          End If
LINE 9      Next i
LINE 10 End Sub

LINE 11 Private Sub cmbSeismicCategory_Click()
LINE 12     dgParams.Text = cmbSeismicCategory.Text
LINE 13 End Sub

I appreciate your suggestions.
0
Kevin_Christensen
Asked:
Kevin_Christensen
  • 5
  • 4
1 Solution
 
wehoitCommented:
You'll have to post more of the code for more help...

You write:
"Of course, LINE 12 changes dgParams.Text to "N/A" and all this has occurred with the user simply clicking on the datagrid column."

This tells me that something is going on prior to getting to the Sub you posted here.

The last value in the box is being selected in this Sub with this line (3):
Temp.ListIndex = Temp.ListCount - 1

If this combo only has "N/A" in it, then that is what is being selected.

Then, when you say the code moves from Line 3 to Line 11,
I'm not sure how this could happen. It should evaluate Line 4 first - it must be that the value for Temp.ListCount-1 is less than zero - meaning there's nothing in the "temp" list (the one sent via params) so the For loop is skipped.

What calls the "FillCombo" Sub?
My guess is the user clicking on the datagrid does that.
Look for the data grid's "click" or "selchange" event.
Or, just search for "FillCombo" throughout the code to determine where the call comes from...

0
 
Kevin_ChristensenAuthor Commented:
You are correct, FillCombo() is called by dgParams_RowColChange() -- see code below...

The combobox list has 5 values in this order; SC-I, SC-II, SC-III, SC-IV, N/A.

After FillCombo LINE 3 Temp.ListIndex = -1 abd Temp.ListCount = 5

It seems that FillCombo LINE 3 triggers the combobox Click event and chsnges cmbSeismicCategory.Text to N/A.

BEGIN CODE:
Private Sub dgParams_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
If dgParams.Col = 6 Then
    Call FillCombo(cmbSeismicCategory, dgParams.Text)
    Call ShiftCombo(cmbSeismicCategory, 6)
Else
    cmbSeismicCategory.Visible = False
End If
If dgParams.Col = 5 Then
    Call FillCombo(cmbSafetyClass, dgParams.Text)
    Call ShiftCombo(cmbSafetyClass, 5)
Else
    cmbSafetyClass.Visible = False
End If


If Not IsInsert Then
    If Not IsEmpty(LastRow) Then
       If IsLocked Then Call Unlock_Record
         
       If adoRs.EOF Or adoRs.BOF Then
       Else
           If Not adoRs.EditMode = adEditAdd Then SetRefs
       End If
    End If
End If

End Sub
0
 
wehoitCommented:
I don't think I see the need for Line 3 at all.

Have you commented it out and run the project?

It looks like it's selecting the last value ("N/A") by default.

I would also consider changing the code to this:
LINE 1  Private Sub FillCombo(Temp As ComboBox, StrValue As String)
LINE 2      Dim i As Integer
..........<moved below>.............................
LINE 4      For i = 0 To Temp.ListCount - 1
LINE 5          If Temp.List(i) = StrValue Then
LINE 6              Temp.ListIndex = i
LINE 7              Exit For
LINE 8          End If
LINE 3      Temp.ListIndex = Temp.ListCount - 1
LINE 9      Next i
LINE 10 End Sub

This way, "N/A" is selected if nothing else is first.
0
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!

 
Kevin_ChristensenAuthor Commented:
THANK YOU!  That worked.  I cannot find any negative impact to removing that line of code.

Though not a problem, I have another observation with this same combobox and if something quick comes to mind I would appreciate a suggestion.  When there are multiple rows displayed in the datagrid and if one row's combobox field has a value and another row is null, the combobox behaves this way.  Only when the combobox with a null value has the focus, the value from the other row is displayed, but does not change the data in the database.

Kevin

0
 
wehoitCommented:
You kind of lost me on the last one.

Can you be a little more descriptive, and perhaps attach a screenshot?
0
 
Kevin_ChristensenAuthor Commented:
How do you add an attachment?  I will try and expalain using text...  Otherwise for screenshots, you can contact me at rockserf@hotmail.com and I will e-mail them to you.

This is the same datagrid and combobox that you helped me with before, therefore the same code applies.

These values display in the datagrid when first viewing 3 records.

Column - Seismic Category
Row 1  - null
Row 2  - null
Row 3  - N/A

After clicking on row 3 then clicking on row 1, for example, these are the values that I see.

Column - Seismic Category
Row 1  - N/A
Row 2  - null
Row 3  - N/A

Then if I click on row 2...

Column - Seismic Category
Row 1  - null
Row 2  - N/A
Row 3  - N/A
 
Fortunately the values for rows 1 and 2 do not change in the database but the fact that N/A is dispalyed is confusing and misleading.

Thanks.
0
 
wehoitCommented:
You list the display for some rows as "null".
Do you mean the combo boxes show nothing?
While the "N/A" reference is actual text, right?

And you're saying that just clicking on a 'row' changes the displayed text in the combos to 'N/A'?

Last, the problem is that clicking Row 2 swaps the displayed text between Rows 1 and 2?

OK - first, do this: In this Sub
Private Sub dgParams_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
If dgParams.Col = 6 Then
   Call FillCombo(cmbSeismicCategory, dgParams.Text)
...

display these vars (use a msgbox, or a text box or something to dump the values) We need to see what is being sent to the FillCombo Sub.
LastRow
LastCol
cmbSeismicCategory
dgParams.Text
0
 
Kevin_ChristensenAuthor Commented:
The answer is YES to all of your questions.

Looking at LastRow, LastCol, cmbSeismicCategory.Text, and dgParams.Text where you suggested, yields the following:

Clicking Row 3 shows
  LastRow = 1
  LastCol = 0
  cmbSeismicCategory.Text = null
  dgParams.Text = N/A

Then clicking Row 1 shows
  LastRow = 3
  LastCol = -1
  cmbSeismicCategory.Text = N/A
  dgParams.Text = null

Then clicking Row 2 shows
  LastRow = 1
  LastCol = -1
  cmbSeismicCategory.Text = N/A
  dgParams.Text = null

I will be posting a new question for another problem I am having with this same datagrid, probably on Wednesday.  An opportunity for more points!
0
 
wehoitCommented:
It's getting difficult to track now.

The Sub "dgParams_RowColChange" never uses the input val "LastCol", so I don't know why it's there.

Also, what does the sub "ShiftCombo" do?

Just from a debugging standpoint, I'd comment out the calls to "ShiftCombo" within "dgParams_RowColChange" and see if the values shown in the combos still get weird.

Also, when the value (ListIndex) in the combo changes, it should fire off the "change" or "click" event for that combo.
Perhaps that event is where the text changes again?
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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