Hi everyone...,
I am kind of new to VBA. I am getting this strange error "can't move focus to the control combo2" when i try to setfocus to the combobox.
What i am doing in my code is i have 2 combobox, let's say combo1 and combo2, i have set the Row source property of both of them to fields of my table, what exactly i want is if i select a value from combo1 i want the corresponding value of the field in combo2 and if i select a value from combo2 i want the corresponding value of the field in combo1. Here is my code, and command4 is the save button where i am trying to save the both combo values with some other data back to table. I would appreciate if somebody could help me.
Private Sub Combo0_Exit(Cancel As Integer)
Dim rst As New ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
rst.Open "Test", cnn, adOpenDynamic, adLockOptimistic, adCmdTable
Do Until rst.EOF
If rst![Value1] = Combo0.Text Then
Combo2.SetFocus
Combo2.Text = rst![Value2]
GoTo Last
Else
rst.MoveNext
End If
Loop
Last:
rst.Close
End Sub
Private Sub Combo2_Exit(Cancel As Integer)
Dim rst As New ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
rst.Open "Test", cnn, adOpenDynamic, adLockOptimistic, adCmdTable
Do Until rst.EOF
If rst![Value2] = Combo2.Text Then
Combo0.SetFocus
Combo0.Text = rst![Value1]
GoTo Last
Else
rst.MoveNext
End If
Loop
Last:
rst.Close
End Sub
Private Sub Command4_Click()
Dim rst As New ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
rst.Open "Test", cnn, adOpenDynamic, adLockOptimistic, adCmdTable
rst.AddNew
Combo0.SetFocus
rst![Value1] = Combo0.Text
MsgBox "Value Set for Value1 field"
Command4.SetFocus
Combo2.SetFocus
rst![Value2] = Combo2.Text
rst.Update
End Sub
I am kind of new to VBA. I am getting this strange error "can't move focus to the control combo2" when i try to setfocus to the combobox.
What i am doing in my code is i have 2 combobox, let's say combo1 and combo2, i have set the Row source property of both of them to fields of my table, what exactly i want is if i select a value from combo1 i want the corresponding value of the field in combo2 and if i select a value from combo2 i want the corresponding value of the field in combo1. Here is my code, and command4 is the save button where i am trying to save the both combo values with some other data back to table. I would appreciate if somebody could help me.
Private Sub Combo0_Exit(Cancel As Integer)
Dim rst As New ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
rst.Open "Test", cnn, adOpenDynamic, adLockOptimistic, adCmdTable
Do Until rst.EOF
If rst![Value1] = Combo0.Text Then
Combo2.SetFocus
Combo2.Text = rst![Value2]
GoTo Last
Else
rst.MoveNext
End If
Loop
Last:
rst.Close
End Sub
Private Sub Combo2_Exit(Cancel As Integer)
Dim rst As New ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
rst.Open "Test", cnn, adOpenDynamic, adLockOptimistic, adCmdTable
Do Until rst.EOF
If rst![Value2] = Combo2.Text Then
Combo0.SetFocus
Combo0.Text = rst![Value1]
GoTo Last
Else
rst.MoveNext
End If
Loop
Last:
rst.Close
End Sub
Private Sub Command4_Click()
Dim rst As New ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
rst.Open "Test", cnn, adOpenDynamic, adLockOptimistic, adCmdTable
rst.AddNew
Combo0.SetFocus
rst![Value1] = Combo0.Text
MsgBox "Value Set for Value1 field"
Command4.SetFocus
Combo2.SetFocus
rst![Value2] = Combo2.Text
rst.Update
End Sub