PDA

View Full Version : UserForm Initialize or Activate Conundrum



brorick
04-04-2008, 07:17 AM
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.

Dim x As Integer
x = x + 1
If x > 1 Then


:mkay 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


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

rory
04-04-2008, 07:51 AM
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

brorick
04-04-2008, 07:59 AM
Rory thank you for your response. :friends: Sounds like an excellent idea. I will give it a try.