Consulting

Results 1 to 3 of 3

Thread: UserForm Initialize or Activate Conundrum

  1. #1

    UserForm Initialize or Activate Conundrum

    The referenced code below (Example 1) is on UserForm1. The form when opened defaults to a specific record. Then the user has the option to click on a button to view all the records on UserForm2. When the user double clicks on an item in the listbox the UserForm1 will reopen and the record details will display.

    The problem I have is when UserForm1 opens the Initialize code conflicts with the Activate code and therefore neither works.

    I considered using something like this on the UserForm_Activate code.

    [vba]Dim x As Integer
    x = x + 1
    If x > 1 Then
    [/vba]

    The problem with the above code occurs because UserForm1 utilizes Me.Hide therefore the counter doesn't work the way I need it to. I want the Userform_Activate() code to be ignored the first time the form is run. Any help is always greatly appreciated. Thank you.


    Example 1
    [vba]

    Dim rgData as Range
    Public myrow As String
    Public mynewrow As String

    Private Sub UserForm_Initialize()
    LoadData 2
    End Sub
    Private Sub UserForm_Activate()
    If UserForm1.txtMyRow.Value = "" Then

    mynewrow = UserForm1.txtNewRow.Value
    With Range("NewData")
    Set rgData = Range("NewData").Rows(mynewrow)
    Call GetRecords
    Me.txtRow = mynewrow
    End With

    ElseIf UserForm1.txtNewRow = "" Then
    myrow = UserForm1.txtMyRow.Value
    With Range("Database")
    Set rgData = Range("Database").Rows(myrow)
    Call GetRecords
    Me.txtRow = myrow
    End With
    End If


    End Sub[/vba]
    Last edited by brorick; 04-04-2008 at 07:53 AM.

  2. #2
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You could add a Boolean variable to the userform (say blnFirstRun) and set it to True in the Initialize event. Then in the Activate event, check if it's True, if so, set it to False and Exit Sub.
    HTH
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    Rory thank you for your response. Sounds like an excellent idea. I will give it a try.

Posting Permissions

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