Results 1 to 9 of 9

Thread: combobox.change event fires when form opens. [Resolved]

  1. #1

    Thread Starter
    Member
    Join Date
    May 2004
    Location
    Huntington WV
    Posts
    49

    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
    Last edited by MIKEADKINS; Sep 17th, 2004 at 01:00 PM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    You may set some boolean flag while form is loading and check it within any procedure, also you will reset it at the end of your Form_Load event.
    But I would like to see your Form_Load event procedure if you don't mind.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Easiest is just to check if the form is visible or not. Its not visible
    until its finished loading.

    VB Code:
    1. Private Sub Combo1_Change()
    2.     If Form1.Visible = True Then
    3.         'Do stuf like validation of data entry or ??
    4.     Else
    5.         'Skips because the form is still loading.
    6.     End If
    7. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: combobox.change event fires when form opens.

    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.

  5. #5

    Thread Starter
    Member
    Join Date
    May 2004
    Location
    Huntington WV
    Posts
    49

    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

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    The combo1_change event is for when a user types something
    into the combo box for style 0. The click event is when the user
    actually clicks on an item in the dropdown list.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Member
    Join Date
    May 2004
    Location
    Huntington WV
    Posts
    49

    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:
    1. Private Sub Form_Load()
    2.    
    3.    Dim rsCDs As ADODB.Recordset
    4.    strPath = App.Path & "\datastore.mdb"
    5.    
    6.  With Adodc2
    7.       .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    8.       "Persist Security Info=False;Data Source=" & strPath & _
    9.       "; Mode=Read|Write"
    10.       .CursorLocation = adUseClient
    11.       .CursorType = adOpenStatic
    12.       .CommandType = adCmdText
    13.       .RecordSource = "SELECT * FROM CD_Table"
    14.       .Refresh
    15.    End With
    16.    
    17.    Adodc2.Recordset.MoveFirst
    18.    'Find the selected record and move the record pointer to that record.
    19.    Adodc2.Recordset.Find "RecordID = '" & gsRecordID & "'", 1, adSearchForward, 1
    20.    If Adodc2.Recordset.EOF Then
    21.     Adodc2.Recordset.MoveFirst
    22.     Else
    23.    End If
    24.    
    25.    
    26.    HideLoanInfo
    27.    
    28.    cmdUpdate.Enabled = False
    29. End Sub
    30.  
    31. Private Sub HideLoanInfo()
    32.    
    33.     If chkOnLoan.Value = 1 Then
    34.         lblLoaned_to.Visible = True
    35.         txtLoaned_To.Visible = True
    36.         lblLoanDate.Visible = True
    37.         txtLoan_date.Visible = True
    38.         lbl_Loan_Due_Date.Visible = True
    39.         txtLoan_Date_Due.Visible = True
    40.         cmdReturnItem.Visible = True
    41.     Else
    42.         lblLoaned_to.Visible = False
    43.         txtLoaned_To.Visible = False
    44.         lblLoanDate.Visible = False
    45.         txtLoan_date.Visible = False
    46.         lbl_Loan_Due_Date.Visible = False
    47.         txtLoan_Date_Due.Visible = False
    48.         cmdReturnItem.Visible = False
    49.     End If
    50. End Sub
    Last edited by MIKEADKINS; Sep 17th, 2004 at 01:48 PM.

  8. #8

  9. #9

    Thread Starter
    Member
    Join Date
    May 2004
    Location
    Huntington WV
    Posts
    49

    Still learning. Thanks

    Still learning. I did not know about the [Highlight=VB] tag. Wow, thats neat! My program is progressing nicely now and hope to have it completed this weekend. Thanks for everyones help again. Great Web Site. This is what the internet is all about. Sharing information and knowlege. Everyone take care and have a great weekend. If you are in West Virginia and you see someone at Starbucks with there laptop, coding away, its ME.

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width