Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

test for object = nothing 1

Status
Not open for further replies.

Eupher

MIS
Jul 18, 2002
1,724
US
I'm trying to use an optional textbox object as an argument in a function but had some problems:
Code:
Public Sub OpenCalendarDateOnly(FormObj As Form, DateBox As TextBox, [blue]Optional DateBox2 As TextBox[/blue])
I got around them by declaring the argument as a Variant instead:
Code:
Public Sub OpenCalendarDateOnly(FormObj As Form, DateBox As TextBox, [blue]Optional DateBox2 As Variant[/blue])
Then test for IsMissing(DateBox2). I'm curious, about one thing, though... If I use this code:
Code:
If Not IsNull(p_dtSelected) Then
    DateBox = Format(p_dtSelected, "m/d/yyyy")
    If Not IsMissing(DateBox2) Then     [green]'if DateBox2 argument has been passed[/green]
        Set ctrl2 = DateBox2            [green]'assign DateBox2 variant to control variable[/green]
        If Not IsDate(ctrl2) Then       [green]'if ctrl2 does not hold a valid date[/green]
            ctrl2 = Null                [green]'set value to Null[/green]
            ctrl2 = Format(p_dtSelected, "m/d/yyyy")    [green]'write date value[/green]
            DateBox2 = ctrl2            [green]'assign ctrl2 value to DateBox2 variant[/green]
        End If
    End If
    DateBox.SetFocus
End If
...then everything works perfectly. However, if I try to write the value directly to the DateBox2 variant, like this:
Code:
If Not IsNull(p_dtSelected) Then
    DateBox = Format(p_dtSelected, "m/d/yyyy")
    If Not IsMissing(DateBox2) Then     [green]'if DateBox2 argument has been passed[/green]
        If Not IsDate(DateBox2) Then       [green]'if DateBox2 does not hold a valid date[/green]
            [blue]DateBox2 = Null[/blue]                [green]'set value to Null[/green]
            [blue]DateBox2 = Format(p_dtSelected, "m/d/yyyy")[/blue]    [green]'write date value[/green]
        End If
    End If
    DateBox.SetFocus
End If
...the value of DateBox2 never gets back to the calling form. Can someone 'splain that to me? Thanks!

Ken S.
 
I think it is due to when objects are passed to functions or subs, they are passed By Reference, any changes to the object variables, will be reflected on the objects.

When passing a variant, you are passing a variable -> a value here probably the value from the form control - it may look the same, but when passing me!txtSomeControl, it will mean different things depending on the sub/function declaration.

If declared as object you'll pass the form control, if variant or other datatype, you'll pass the value of the control - not an object.

In the latter case, you'll need to assign the value of the variable after the sub or function has executed.

Roy-Vidar
 
Thanks, Roy, that helps clear it up. But...
In the latter case, you'll need to assign the value of the variable after the sub or function has executed.
I was thinking along the same lines, but, how's come my first example works, then?

Ken S.
 
In the first case you're using/passing a form control - an object...

In all this, I didn't quite get what the problem was, so I just tried to answer one of the questions - but I'm thinking it might have to do with testing for a passed object?

Not sure how the IsMissing thingie is for objects, did you try

[tt]if (DateBox2 is nothing) then
' probably not passed
end if[/tt]

Roy-Vidar
 
p.s. In fact, this also works:
Code:
If Not IsNull(p_dtSelected) Then
    DateBox = Format(p_dtSelected, "m/d/yyyy")
    If Not IsMissing(DateBox2) Then     [green]'if DateBox2 argument has been passed[/green]
        Set ctrl2 = DateBox2            [green]'assign DateBox2 variant to control variable[/green]
        If Not IsDate(ctrl2) Then       [green]'if ctrl2 does not hold a valid date[/green]
            [s]ctrl2 = Null                [green]'set value to Null[/green][/s]
            ctrl2 = Format(p_dtSelected, "m/d/yyyy")    [green]'write date value[/green]
            [s]DateBox2 = ctrl2            [green]'assign ctrl2 value to DateBox2 variant[/green][/s]
        End If
    End If
    DateBox.SetFocus
End If
I guess because I've assigned the variant to a control object?

Ken S.
 
Roy, that's it!
if (DateBox2 is nothing) then
...is what I was looking for.
I had the wrong operator - [blue]Is[/blue] is the correct one, not [blue]=[/blue]

Thanks!

Ken S.
 
Here's the final version (until I tinker with it again):
Code:
If Not IsNull(p_dtSelected) Then
    DateBox = Format(p_dtSelected, "m/d/yyyy")
    If Not (DateBox2 Is Nothing) Then       [green]'if DateBox2 argument has been passed[/green]
        If Not IsDate(DateBox2) Then        [green]'if ctrl2 does not hold a valid date[/green]
            DateBox2 = Format(p_dtSelected, "m/d/yyyy")    [green]'write date value[/green]
        End If
    End If
    DateBox.SetFocus
End If
Thanks again.

Ken S.
 
p.s. That's with DateBox2 declared as an optional TextBox... oh, yeah, and the second line of comment should be DateBox2 instead of ctrl2.

Ken S.
 
Eupher (and Roy), Optional parameters can have a default value, which can make them easier to work with - as in this very crude example. (The second parameter is still a text box not a variant, so if used can be treated without casting as such.

Code:
Private Function CompareBoxes(b1 As TextBox, Optional b2 As TextBox = Nothing) As Boolean
  
  CompareBoxes = False
  If Not b2 Is Nothing Then
    CompareBoxes = Nz(b1, "") = Nz(b2, "")
    b2.SetFocus
  Else
    b1.SetFocus
  End If

End Function

Private Sub Command4_Click()

  If Not Check7 Then
    MsgBox CompareBoxes(Text0)
  Else
    MsgBox CompareBoxes(Text0, Text2)
  End If

End Sub

The form consists of:

Text0 and Text2 (TextBoxes)
Button4 (CommandButton)
Check7 (CheckBox)

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top