combobox.change event fires when form opens. [Resolved]
I know that I can progam around this but still not quite sure why it happens. I have a project where a form has a combobox.change event that has code in it that gives the user a message that they are about to cause something to happen by changing the selection in the combo box. However, when the form opens the change event fires and the message box appears. The code is simple
Private Sub cboBox1_change()
msgbox "You just changed your selection"
End Sub
As the form opens the Message appears "You just changed your selection" and then after the user clicks ok the form opens. Why does the event Change fire as the form is opening? Is there a better way of handling this? Or do I just write come code to check to see if the form is open before the change event should occur? Thanks again for everyones help in the past. I continue to learn from this site.
Mike
Re: combobox.change event fires when form opens.
Quote:
Originally posted by MIKEADKINS
I know that I can progam around this but still not quite sure why it happens. I have a project where a form has a combobox.change event that has code in it that gives the user a message that they are about to cause something to happen by changing the selection in the combo box. However, when the form opens the change event fires and the message box appears. The code is simple
Private Sub cboBox1_change()
msgbox "You just changed your selection"
End Sub
As the form opens the Message appears "You just changed your selection" and then after the user clicks ok the form opens. Why does the event Change fire as the form is opening? Is there a better way of handling this? Or do I just write come code to check to see if the form is open before the change event should occur? Thanks again for everyones help in the past. I continue to learn from this site.
Mike
The Change event does not normally fire when the form loads. Put a breakpoint on the first line of code in the Change event and when the code gets there look at the Call Stack to see where you came from.
Discovered even more about combo boxes
The information that you provided did work. When the form opens the change event does not fire the code if it is placed within the If condition. However, I also found that clicking on the Combo List and selecting another choice does not kick off the change event code. What I did find was cboBox_Click() event does not fire until you make a new selection in a combo box. So the result was the following code that worked for me. But the other information is great to know also.
Private Sub Combo1_Click()
msgBox "You just changed something!"
End Sub
cboBox_change event update
RhinoBull wanted to see my form_load code. So here it is along with the sub procedure. None of these should be affecting the cboBox_change event but I wanted to provide it anyway. What was suggested above did work. When the cboBox_Change() procedure checked to see if the form was visible it did not fire because the form had not loaded as stated by RobDog888. But as I said, what I was going after resulted in putting my msgbox code in the cboBox_Click() event. Because when I clicked on the cboBox and changed the selection the cboBox_Change() code did not fire. It just acts differently that what you would expect from the events. But as most of us know, if one thing does not work try something else. Thanks again for the updates. I learn so much from you all that thanks just never seems enough. Take care. Mike
VB Code:
Private Sub Form_Load()
Dim rsCDs As ADODB.Recordset
strPath = App.Path & "\datastore.mdb"
With Adodc2
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Persist Security Info=False;Data Source=" & strPath & _
"; Mode=Read|Write"
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.CommandType = adCmdText
.RecordSource = "SELECT * FROM CD_Table"
.Refresh
End With
Adodc2.Recordset.MoveFirst
'Find the selected record and move the record pointer to that record.
Adodc2.Recordset.Find "RecordID = '" & gsRecordID & "'", 1, adSearchForward, 1
If Adodc2.Recordset.EOF Then
Adodc2.Recordset.MoveFirst
Else
End If
HideLoanInfo
cmdUpdate.Enabled = False
End Sub
Private Sub HideLoanInfo()
If chkOnLoan.Value = 1 Then
lblLoaned_to.Visible = True
txtLoaned_To.Visible = True
lblLoanDate.Visible = True
txtLoan_date.Visible = True
lbl_Loan_Due_Date.Visible = True
txtLoan_Date_Due.Visible = True
cmdReturnItem.Visible = True
Else
lblLoaned_to.Visible = False
txtLoaned_To.Visible = False
lblLoanDate.Visible = False
txtLoan_date.Visible = False
lbl_Loan_Due_Date.Visible = False
txtLoan_Date_Due.Visible = False
cmdReturnItem.Visible = False
End If
End Sub