Listindex @ userform initialize

Last Edited By Krjb Donovan
Last Updated: Mar 05, 2014 09:44 PM GMT

QuestionEdit

Is there a way to have the listindex of listboxes to have a preset value of zero when the userform initializes or opens?

Thank you and Denny


Sub Button1_Click()

TrapUF.Show

End Sub I would like these to be preset to these values when the userform is opened.

 Locationlb.ListIndex = 0
      SetsLB.ListIndex = 0
      Lure1LB.ListIndex = 0
      Lure2lb.ListIndex = 0
      Lure3LB.ListIndex = 0
      BaitLB.ListIndex = 0
      UrineLB.ListIndex = 0
      TrapLB.ListIndex = 0
      TargetLB.ListIndex = 0
      SNotesTB.Text = ""
      CBRem.Value = False


AnswerEdit

Denny,

there are two ways.

One is to load the form first and make those settings, then show the form

Sub Button1_Click() Load TrapUF With TrapUF

      .Locationlb.ListIndex = 0
     .SetsLB.ListIndex = 0
     .Lure1LB.ListIndex = 0
     .Lure2lb.ListIndex = 0
     .Lure3LB.ListIndex = 0
     .BaitLB.ListIndex = 0
     .UrineLB.ListIndex = 0
     .TrapLB.ListIndex = 0
     .TargetLB.ListIndex = 0
     .SNotesTB.Text = ""
     .CBRem.Value = False

End With TrapUF.Show End Sub

since this code it not in the userform module, you must qualify the names with their parent, the userform. This is easily done using the With; End With construct as shown above.

the other way is to use the Initialize Event.

Private Sub Userform_Initialize()

     Locationlb.ListIndex = 0
     SetsLB.ListIndex = 0
     Lure1LB.ListIndex = 0
     Lure2lb.ListIndex = 0
     Lure3LB.ListIndex = 0
     BaitLB.ListIndex = 0
     UrineLB.ListIndex = 0
     TrapLB.ListIndex = 0
     TargetLB.ListIndex = 0
     SNotesTB.Text = ""
     CBRem.Value = False

End Sub

This always has the name Userform_Initialize no matter what the name of the userform it. You can go into the userform module and at the top in the dropdowns, select Userform on the left and Initialize on the right. That will put in the proper declaration. Then add your code to that.

This event fires everytime the form is loaded.

Advertisement

©2024 eLuminary LLC. All rights reserved.