View Full Version : Please help with IF statement


Bigmo2u
07-09-2008, 07:12 AM
I have tried several ways of doing this and can not get it to work. Here is the If statement:

If (Me.ComponentCode = "KM") And (CurrentUser() <> "user1") Or (CurrentUser() <> "user2") Or (CurrentUser() <> "user3") Or (CurrentUser() <> "user4") Then _
MsgBox "You are not authorized to enter this claim. Please take it to MCRATS."
Me.tblClaims_SSN.SetFocus
End If

I originally had it with out the End If and got an error of saying I need the end if. So I added it now the if statement will not work. It throws the message box everytime.

Do I need to do this as SELECT CASE????

Can someone please help me.

KenHigg
07-09-2008, 07:17 AM
This all looks rather dynamic so I would recommend you put it in a table and do a dlookup(). What you are doing is called hard-coding. If any of these people change you'll have to go back into your code and make changes...

(Which may not be bad if you're getting paid to come back out and make these type changes ;p)

Bigmo2u
07-09-2008, 07:21 AM
Thanks KenHigg, but I am not sure how to do that.

KenHigg
07-09-2008, 07:23 AM
So are you going that route or do you still want help ironing out the if thing?

MSAccessRookie
07-09-2008, 07:29 AM
I have tried several ways of doing this and can not get it to work. Here is the If statement:

If (Me.ComponentCode = "KM") And (CurrentUser() <> "cmoratz") Or (CurrentUser() <> "bhoward") Or (CurrentUser() <> "kwilliams") Or (CurrentUser() <> "bwilliams") Then _
MsgBox "You are not authorized to enter this claim. Please take it to MCRATS."
Me.tblClaims_SSN.SetFocus
End IfI originally had it with out the End If and got an error of saying I need the end if. So I added it now the if statement will not work. It throws the message box everytime.

Do I need to do this as SELECT CASE????

Can someone please help me.

The following code works for me and is very similar to yours (The names have been changed to protect the innocent clients). They are very similar, except that I see an extra "_" that you have that I do not have.

-----------------------------------------------------------------------

If ((Me.UserName = "SQLDevelopment") Or (Me.UserName = "User1") Or (Me.UserName = "User2")) Then
Me.TransactionsButton.Visible = True
Me.MerckOrdersReport.Visible = True
End If

Bigmo2u
07-09-2008, 07:33 AM
Thanks MSAccessRookie, I will try it.

KenHiggs, I would like to learn your way as well. Since I have several of these per form.

KenHigg
07-09-2008, 07:34 AM
I think he simply needs to put parens around all of his 'or' stuff so at least one of the 'or's are true (If that's the logic).

???

MSAccessRookie
07-09-2008, 07:35 AM
I think it might have been the extra "_":eek:

Bigmo2u
07-09-2008, 07:38 AM
Compile Error: End If without If Block

If (Me.CompCode = "KM") And ((CurrentUser() <> "cmoratz") Or (CurrentUser() <> "bhoward") Or (CurrentUser() <> "kwilliams") Or (CurrentUser() <> "bwilliams")) Then _
MsgBox "You are not authorized to enter this claim. Please take it to MCRATS."
Me.tblClaims_SSN.SetFocus
End If

georgedwilkinson
07-09-2008, 07:40 AM
Remove the "_" after the word "Then".

Bigmo2u
07-09-2008, 07:41 AM
I did that also, but now it gives me the MsgBox and that shouldn't happen.

MSAccessRookie
07-09-2008, 07:41 AM
Based on the timing of th post, I am guessing you did not see my last reply. try getting rid of the "_" at the end of the first line

MSAccessRookie
07-09-2008, 07:43 AM
Break the sentence down and answer two questions please.

1. What is the value of Me.CompCode?
2. What is your active CurrentUser()?

KenHigg
07-09-2008, 07:46 AM
I think you have a rouge 'if' else where in your code - ?

Bigmo2u
07-09-2008, 07:46 AM
I have removed the "_" and now it gives me the MsgBox and It shouldn't.

If (Me.CompCode = "KM") And ((CurrentUser() <> "User1") Or (CurrentUser() <> "User2") Or (CurrentUser() <> "User3") Or (CurrentUser() <> "User4")) Then
MsgBox "You are not authorized to enter this claim. Please take it to MCRATS."
End If



1. CompCode is text and can either be null or KM
2. CurrentUser() = cmoratz

KenHigg - what do u mean by a Rogue 'if' Else in my code?

MSAccessRookie
07-09-2008, 07:53 AM
I have removed the "_" and now it gives me the MsgBox and It shouldn't.

If (Me.CompCode = "KM") And ((CurrentUser() <> "User1") Or (CurrentUser() <> "User2") Or (CurrentUser() <> "User3") Or (CurrentUser() <> "User4")) Then
MsgBox "You are not authorized to enter this claim. Please take it to MCRATS."
End If1. CompCode is text and can either be null or KM
2. CurrentUser() = cmoratz

I think I see at least one point. I think that you want And instead of Or in all of the Red marked locations. Also:

1. I need to know what it IS, not what it COULD BE (to determine Pass/Fail for this part).
2. User cmoratz is not in the list of allowed users { User1; User2; User3; and User4 }.

Bigmo2u
07-09-2008, 07:58 AM
KenHigg - how do you do the Dlookup that you were talking about?

Bigmo2u
07-09-2008, 08:03 AM
The code really looks like this:

If (Me.CompCode = "KM") And ((CurrentUser() <> "cmoratz") Or (CurrentUser() <> "bhoward") Or (CurrentUser() <> "kwilliams") Or (CurrentUser() <> "bwilliams")) Then
MsgBox "You are not authorized to enter this claim. Please take it to MCRATS."
Me.tblClaims_SSN.SetFocus

End If

CompCode = KM
CurrentUser = cmoratz

Sorry for the confusion. Not sure on the "And" part you pointed out. If the compcode is is KM and it is right now and not equal to any of these user then throw the msgbox and setfocus on the SSN field.

If the compcode = KM and it is one of the users listed to do nothing.

KenHigg
07-09-2008, 08:22 AM
I think you simply need to replace all of you 'or's with 'and's...

KenHigg
07-09-2008, 08:26 AM
My bad, I see Rookie already suggested that - :)

Bigmo2u
07-09-2008, 08:28 AM
KenHigg - it worked.... MSAccessRookie, now I get what you were saying... Thanks guys I have learn several new things. Don't "_" after Then and then you can you put an End If at the end of the statement.....

As always this forum has great people and they are always willing to help....

KenHigg
07-09-2008, 08:30 AM
cool - That 'don't equal or' stuff is a mind twister - :p

MSAccessRookie
07-09-2008, 08:40 AM
Don't feel too bad about it, about half the time I get the And and the Or mixed up myself. My example was poor because it was inclusive while yours was exclusive. We can all Learn from each other out here. Just glad to be a part of the solution.

Bigmo2u
07-09-2008, 08:44 AM
Now I have the issue when the user is not in the list that it throws the MsgBox but will not undo the record and setfocus on the specific field.

If (Me.CompCode = "KM") And ((CurrentUser() <> "cmoratz1") And (CurrentUser() <> "bhoward") And (CurrentUser() <> "kwilliams") And (CurrentUser() <> "bwilliams")) Then
MsgBox "You are not authorized to enter this claim. Please take it to MCRATS."
UndoRecord_Click
Me.tblClaims_SSN.SetFocus
End If

KenHigg
07-09-2008, 08:51 AM
Back to the dlookup thing:
( I actually use a dcount() )

Bigmo2u
07-10-2008, 06:34 AM
I am still having problems with the last statement.

[code]If (Me.CompCode = "KM") And ((CurrentUser() <> "cmoratz1") And (CurrentUser() <> "bhoward") And (CurrentUser() <> "kwilliams") And (CurrentUser() <> "bwilliams")) Then
MsgBox "You are not authorized to enter this claim. Please take it to MCRATS."
UndoRecord_Click
Me.tblClaims_SSN.SetFocus
End If

Can Someone please tell me why it will not set the cursor back into this field?

Bigmo2u
07-10-2008, 07:24 AM
FIgured it out. Just had to use the GotFocus on the next field.

Private Sub ClaimType_GotFocus()

If IsNull(Me.tblClaims_SSN) Then
Me.tblClaims_SSN.SetFocus
End If

End Sub