Originally Posted by
mikerickson
...NOTE: when treating the uf as a class, after the .Show command DO NOT use the Me keyword. The user might corner click the uf closed and Me may not exist. Using the full name of the userform (UserForm1) rather than Me gets the default data from a new instance of the uf, (this was used above to test if the user OKed or Cancled)
Greetings All,
@mikerickson:
Hi Mike :-) Reference post 12, I heartily agree with Paul; that is just "too cool!".
A lazy Sunday in effect, I tried a couple of things. In gist, I was thinking that it would be neat if we could create a default property (okay, not spectacularly neat, but just thought it might be a little "cool"). Alas, it appears one cannot specify a default member of a userform. I tried a little Class with a default (that calls the form), but of course the extra step makes this to no advantage at all.
Anyways, in calling the form directly, unless I'm glossing over something, how about using QueryClose as a final check on what we want the Function to return? This way Me is never an issue.
Form named frmInput:
One textbox, one label, two buttons, named as shown.
Option Explicit
Private bOK As Boolean
Public Function Value_Ret(Optional strTitle As String = "My Custom Input", _
Optional strPrompt As String = "Enter Something", _
Optional vntDefault As Variant = vbNullString _
) As String
With Me
.Caption = strTitle
.lblPrompt.Caption = strPrompt
With .txtInput
.Value = vntDefault
.SelStart = 0
.SelLength = Len(vntDefault)
End With
.Show
If Len(.txtInput.Value) > 0 And Not .txtInput.Value = vntDefault Then
Value_Ret = .txtInput.Value
Else
Value_Ret = False
End If
End With
'Unload frmInput
End Function
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdOK_Click()
bOK = True
Unload Me
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If Not bOK Then Me.txtInput.Value = vbNullString
End Sub
In a Standard Module
[vba]Sub example02()
Dim uiValue
uiValue = frmInput.Value_Ret(, , "old text")
If uiValue = "False" Then
MsgBox "cancelled"
Else
MsgBox uiValue & " was entered."
End If
End Sub[/vba]
Well, a pleasant evening to all,
Mark