I'm using the following code behind a combo box name cboCompy. For some reason when I make a selection from the pulldown in the combo box this code won't fire. No errors appear but the code won't execute. I put stop in the code so I can view but it never does.
I also tried _Change instead of _OnChange. I'm new to VB6 so pardon a dumb question.
Is there something in the properties need to change?

Thanks,


Private Sub cboCompy_OnChange()
Dim name As String, str As String

Set rs = Nothing

If Me.cboComp <> "Combo1" Then
name = Me.cboComp
str = "select * from tblToDo where NameofComp = '" & name & "'"
' str = "select * from tblToDo where NameofComp = " & name
rs.Open str, adoconn, adOpenForwardOnly, adLockPessimistic

If rs.EOF Then
rs.AddNew
rs(0) = name
rs.Update
Else
FillFields
End If
End If

Set rs = Nothing
str = "select * from tblToDo"
rs.Open str, adoconn, adOpenDynamic, adLockPessimistic


End Sub

Hi,
ComboBoxes in vb6 dont have OnChange() Event, it has Change () event occurs only when typing on the Text of Combobox. Whenever change the items in Combobox it triggers Click () Event. So u can place your code insite Click() event.

Private Sub Combo1_Click()
   ' YourCode
   'Dim name As String, str As String
   'Set rs = Nothing
    'If Me.cboComp <> "Combo1" Then
   .......
End Sub

Or if you want Change and Click event call the one Event(Change()) inside another one (Click())
Ex.

Private Sub Combo1_Change()
   ' YourCode
   'Dim name As String, str As String
   'Set rs = Nothing
    'If Me.cboComp <> "Combo1" Then
   .......
End Sub

Private Sub Combo1_Click()
   Call Combo1_Change
End Sub

The click worked great! Thanks. I'm having a tough time moving from VBA and catching on to VB. I use the following code to add a new record in a Access table. The record is added but when I try to put something into the txtToDo1 field for the new record it never adds it to the newly created record. I assume the new record is not the current record. Can anybody see what I'm doing wrong?


Private Sub cboCompy_Click()
Dim name As String, str As String

Set rs = Nothing

If Me.cboCompy <> "Combo1" Then
name = Me.cboCompy
str = "select * from tblToDo where NameofComp = '" & name & "'"
rs.Open str, adoconn, adOpenForwardOnly, adLockOptimistic

If rs.EOF Then
rs.AddNew
rs(0) = name
rs.Update
txtToDoCompany.Text = rs(0)
txtToDo1.Text = ""
If rs(1) <> "" Then
txtToDo1.Text = rs(1)
End If

Else
FillFields
End If
End If

rs.Close
Set rs = Nothing
adoconn.Close
Set adconn = Nothing
End Sub

Private Sub cboCompy_Click()
Dim name As String, str As String

Set rs = Nothing

If Me.cboCompy <> "Combo1" Then
name = Me.cboCompy
str = "select * from tblToDo where NameofComp = '" & name & "'"
[B]set rs = new RecordSet[/B]
rs.Open str, adoconn, adOpenForwardOnly, adLockOptimistic

If rs.EOF Then
rs.AddNew
rs(0) = name
[B]rs(1) = txtToDo1.Text[/B]
rs.Update

Green indicating the new portion i added

This article has been dead for over six months. Start a new discussion instead.