If Then ElseIf code not working.

lmcc007

Registered User.
Local time
Today, 16:55
Joined
Nov 10, 2007
Messages
635
I am trying to change from using three labels on my form to one label.

1. If this is a new record, the label will say “Add Attachment (y/n)?” and the text box will have a Default Value = Y.

2. If not a new record and Activity ID is not equaled to 1, 2, or 23 then the label will say “Add Attachment (y/n)?” and the text box will have a Default Value = Y.

3. Else if ActivityID = 2 then the label will say "Click icon to vie&w Attachment." And, an icon will display next to it.

4. Else if ActivityID = 1 or 23 then the label will "Click icon to view &Contract Listings." And, another kind of icon will display next to it.

Sometimes the label and icon changes and sometimes they don’t. Below is my code:

Code:
    If Me.NewRecord Then
        Me.lblImage.Caption = "Add Attachment (y/n)?  "
        Me.cmdImage.Visible = False
        Me.txtYesNo.Visible = True
        Me.txtYesNo.Left = 2438
        Me.txtYesNo.Value = "N"
    ElseIf Not Me.NewRecord And Me.ActivityID <> 2 Or Me.ActivityID <> 23 Or Me.ActivityID <> 1 Then
        Me.lblImage.Caption = "Add Attachment (y/n)?  "
        Me.cmdImage.Visible = False
        Me.txtYesNo.Visible = True
        Me.txtYesNo.Left = 2438
        Me.txtYesNo.Value = "N"
    ElseIf Me.ActivityID = 2 Then
        Me.lblImage.Caption = "Click icon to vie&w Attachment."
        Me.cmdImage.Visible = True
        Me.cmdImage.Picture = "C:\Users\LMCC\Pictures\Attachment--smaller.bmp"
        Me.txtYesNo.Visible = False
    Else
        If Me.ActivityID = 23 Or Me.ActivityID = 1 Then
            Me.lblImage.Caption = "Click icon to view &Contract Listings."
            Me.cmdImage.Visible = True
            Me.cmdImage.Picture = "C:\Users\LMCC\Pictures\paperclip -- tiny.bmp"
            Me.txtYesNo.Visible = False
        End If
    End If

Help!!!
 
A couple of things...
First, do you know how to set breakpoints and step through your code line by line while it executes? If not let me know 'cause it's a wicked tool to have if you aren't already using it.

Ok, your logic looks a bit muddled in the IF...ELSE block. See how these all test for Not something or <> something?
Code:
 ElseIf Not Me.NewRecord And Me.ActivityID <> 2 Or Me.ActivityID <> 23 Or Me.ActivityID <> 1 Then
I would handle all the positive cases first. Then everything that doesn't get handled can go in the final 'Else'...
Code:
if me.newrecord then
[COLOR="Green"]  'do stuff
  'also, from now on Not me.newrecord MUST be true[/COLOR]
elseif me.activityID = 1 or me.activityID = 23 then
[COLOR="Green"]  'do stuff
  'also, from now on <> 1 and <> 23 MUST be true[/COLOR]
elseif me.activityID = 2 then 
[COLOR="Green"]  'do stuff
  'also, form now on ... you guessed it[/COLOR]
else
[COLOR="Green"]  'now handle everything that wasn't previously handled[/COLOR]
end if
If you rearrange it like that does it work?

And generally, first compare for what you ARE looking for. Then anything that ISN'T that--typically a much larger set of things--is handled by 'Else'
Cheers,
 
A couple of things...
First, do you know how to set breakpoints and step through your code line by line while it executes? If not let me know 'cause it's a wicked tool to have if you aren't already using it.

No, I tried it in class and followed instructions but I don't understand it where I can use it.

I really would like to know how to finally get it--understand it.
 
Okay, I did it like this:

Code:
    If Me.NewRecord Then
        Me.lblImage.Caption = "NEW RECORD---Add Attachment (y/n)?  "
        Me.cmdImage.Visible = False
        Me.txtYesNo.Visible = True
        Me.txtYesNo.Left = 2438
        Me.txtYesNo.Value = "N"
    ElseIf Me.ActivityID = 1 Or Me.ActivityID = 23 Then
        Me.lblImage.Caption = "1 OR 23 --------Click icon to view &Job Posting."
        Me.cmdImage.Visible = True
        Me.cmdImage.Picture = "C:\Users\LMCC\Pictures\paperclip -- tiny.bmp"
        Me.txtYesNo.Visible = False
    ElseIf Me.ActivityID = 2 Then
        Me.lblImage.Caption = "ATTACHMENT---Click icon to vie&w Attachment."
        Me.cmdImage.Visible = True
        Me.cmdImage.Picture = "C:\Users\LMCC\Pictures\Attachment--smaller.bmp"
        Me.txtYesNo.Visible = False
    Else
        Me.lblImage.Caption = "I AM THE ELSE????????????????"
        Me.cmdImage.Visible = False
        Me.txtYesNo.Visible = True
        Me.txtYesNo.Value = "YYY"
    End If

If there a better way I should do this instead?
 
I am trying to check for Null, but I keep getting Error # 424 (Object Reqd.). Below is the code:

Code:
If (Me.NewRecord = True) Then
        Me.lblImage.Caption = "Add Attachment (y/n)?"
        Me.cmdImage.Visible = False
        Me.txtYesNo.Visible = True
        Me.txtYesNo.Left = 2438
        Me.txtYesNo.Value = "N"
    ElseIf Me.ActivityID = 1 Or Me.ActivityID = 23 Then
        If Me.EventContractPostingAttachment Is Not Null Then
            Me.lblImage.Caption = "Click icon to view Posting."
            Me.cmdImage.Visible = True
            Me.cmdImage.Picture = "C:\Users\LMCC\Pictures\paperclip -- tiny.bmp"
            Me.txtYesNo.Visible = False
    ElseIf Me.ActivityID = 2 Then
        If Me.EventAttachment Is Not Null Then
            Me.lblImage.Caption = "Click icon to vie&w Attachment."
            Me.cmdImage.Visible = True
            Me.cmdImage.Picture = "C:\Users\LMCC\Pictures\Attachment--smaller.bmp"
            Me.txtYesNo.Visible = False
        Else
            Me.lblImage.Caption = "Add Attachment (y/n)?"
            Me.cmdImage.Visible = False
            Me.txtYesNo.Visible = True
            Me.txtYesNo.Left = 2438
            Me.txtYesNo.Value = "N"
        End If
        End If
    End If
 
If there a better way I should do this instead?
No, that looks fine. Does it work as expected?

The 'IS' operator in VBA is for the comparison of objects and is the origin of your error. The following code ...
Code:
Object1 IS Object2
... evaluates to True if the two object variables Object1 and Object2 point to the same object, but the operands are required to be objects.
To check for nulls here ...
Code:
If Me.EventContractPostingAttachment Is Not Null Then
... you cuold use the VBA.IsNull() function like ...
Code:
If Not IsNull(Me.EventContractPostingAttachment) Then

You don't take weekends off?
Cheers,
 
No, that looks fine. Does it work as expected?

The 'IS' operator in VBA is for the comparison of objects and is the origin of your error. The following code ...
Code:
Object1 IS Object2
... evaluates to True if the two object variables Object1 and Object2 point to the same object, but the operands are required to be objects.
To check for nulls here ...
Code:
If Me.EventContractPostingAttachment Is Not Null Then
... you cuold use the VBA.IsNull() function like ...
Code:
If Not IsNull(Me.EventContractPostingAttachment) Then

You don't take weekends off?
Cheers,

Now that make sense. So, Is compare one object against another.

Thanks lagbolt!
 
You're welcome. Congrats to you for your perseverance!!!
Cheers,
 

Users who are viewing this thread

Back
Top Bottom