strDocName Error (1 Viewer)

kitty77

Registered User.
Local time
Today, 14:13
Joined
May 27, 2019
Messages
710
I'm using the following code in a command button.

Dim strDocName As String
Dim strWhere As String
If ([Mid] = "111") Then strDocName = "Report1"
If ([Mid] = "222") Then strDocName = "Report2"
If ([Mid] = "222") Then strDocName = "Report1"
strWhere = "[Mrecordid]=" & Me!Mrecordid
DoCmd.OpenReport strDocName, acViewReport, , strWhere

I'm getting the following error: The action or method requires a report name argument.

What am I doing wrong?

Thanks...
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:13
Joined
Oct 29, 2018
Messages
21,447
Hi. Perhaps none of the checks ([Mid]="111" or "222") was valid. For example, what if [Mid] was "333?"


Maybe you should assign a default report name or check first if there's a report name assigned before trying to open it.
 

isladogs

MVP / VIP
Local time
Today, 19:13
Joined
Jan 14, 2017
Messages
18,208
This may be a typo but both lines can't be correct:
Code:
If ([Mid] = "222") Then strDocName = "Report2"
If ([Mid] = "222") Then strDocName = "Report1"

Try adding the line Debug.Print strWhere before DoCmd.OpenReport ...
Does the output seem correct?
 

kitty77

Registered User.
Local time
Today, 14:13
Joined
May 27, 2019
Messages
710
Sorry, yes was a typo...

It looks like it does not like or can't handle multiple questions. When I break it done, it works. Here is what I really want.

If ([Mid] <> "aaa") And ([Mst] = "bbb") And ([M1] <> [M2]) Then strDocName = "Report"

Hope this makes sense...
 

isladogs

MVP / VIP
Local time
Today, 19:13
Joined
Jan 14, 2017
Messages
18,208
Try
If...ElseIf...ElseIf...Else...End If
OR use Select Case notation

Debug.print strDocName as well as strWhere as a check
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:13
Joined
Oct 29, 2018
Messages
21,447
Sorry, yes was a typo...

It looks like it does not like or can't handle multiple questions. When I break it done, it works. Here is what I really want.

If ([Mid] <> "aaa") And ([Mst] = "bbb") And ([M1] <> [M2]) Then strDocName = "Report"

Hope this makes sense...
Hi. How about telling us what you want to check for using plain words, and we'll try to convert it into code for you?
 

kitty77

Registered User.
Local time
Today, 14:13
Joined
May 27, 2019
Messages
710
I think basically, how do put several arguments together?
If something = something, If something <> something, etc...

Say four or five arguments. All these arguments helps me select the proper report to print.

Maybe an example of several arguments together would be helpful.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:13
Joined
Oct 29, 2018
Messages
21,447
I think basically, how do put several arguments together?
If something = something, If something <> something, etc...

Say four or five arguments. All these arguments helps me select the proper report to print.

Maybe an example of several arguments together would be helpful.

Hi. If I try to explain to you how to combine multiple conditions together, you will still be left interpreting it to apply to your particular situation. Sometimes, it might be easier to actually give an example that applies to your situation because it would make it easier for you to understand it and then apply what you learned to your other scenarios. We just need an English translation of what you want the code to do. We'll convert it to code speak for you.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:13
Joined
Oct 29, 2018
Messages
21,447
I think basically, how do put several arguments together?
If something = something, If something <> something, etc...

Say four or five arguments. All these arguments helps me select the proper report to print.

Maybe an example of several arguments together would be helpful.

Okay, see if this helps any. There are several ways to check for multiple conditions. For example.

Code:
If condition1 Then
  'do stuff
ElseIf condition2 Then
  'do stuff
...
End If

Or, if you need multiple conditions met at the same time, it might look like.

Code:
If condition1 AND conditions2 AND condition2 Then
...
End If
 

Micron

AWF VIP
Local time
Today, 14:13
Joined
Oct 20, 2018
Messages
3,478
It looks like it does not like or can't handle multiple questions
"It" certainly can. In my experience the issue is almost always not constructing an expression in a manner that is conducive to how the logic is processed by computer. Not so much when dealing with multiple AND operators as much as when the OR operator comes in to play.

Guaranteed that this will evaluate to True
6<>5 and 3=3 and 7<>8

EDIT - I let the posts about expressions divert me from the message you got. I interpret it that your value for the report name is invalid. It doesn't exist as named. Check spelling. Colin alluded to that in his post.
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:13
Joined
May 7, 2009
Messages
19,227
on the expression:

If ([Mid] <> "aaa") And ([Mst] = "bbb") And ([M1] <> [M2]) Then strDocName = "Report"

if any variables are empty, it would result to Null.
and you can't compare a Null to any value, hence resulting to False.
better add a null string, then compare:

If ([Mid] & "" <> "aaa") And ([Mst] & "" = "bbb") And ([M1] & "" <> [M2] & "") Then strDocName = "Report"
 

Micron

AWF VIP
Local time
Today, 14:13
Joined
Oct 20, 2018
Messages
3,478
If ([Mid] <> "aaa") And ([Mst] = "bbb") And ([M1] <> [M2]) Then strDocName = "Report"
if any variables are empty, it would result to Null.
and you can't compare a Null to any value, hence resulting to False.
better add a null string, then compare:
Only half true. Since you can't compare anything to Null, the result of the expression can only be Null, not false. Your empty string can work for <> comparisons, but never for = and be true, thus that expression will always evaluate to false anyway because "" will never equal bbb. Besides, the error message suggests that in all (or at least some) cases, the report variable is an empty string and that is what is being passed to the open report command. That means that none of the expressions are true, but they may all be Null too.
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:13
Joined
Feb 19, 2002
Messages
43,204
@Micron,
If a <> b will result in FALSE if either a or b is null. No test of a null value will ever return true except IsNull()

@Kitty,
Do not use parentheses unless they are needed. Having them in an expression just causes confusion when they are serving no purpose.

If ([Mid] <> "aaa") And ([Mst] = "bbb") And ([M1] <> [M2]) Then strDocName = "Report"

should be

If [Mid] <> "aaa" And [Mst] = "bbb" And [M1] <> [M2] Then strDocName = "Report"

Also, don't use square brackets for the same reason. They are simply eye clutter and distracting.

Now we get to the rules of precedence which is what you need to understand to create compound conditions. In logical operations the order is NOT, AND, OR

If all the conditions use the same relational operator
either:
a and b and c and d
OR:
a or b or c or d

Then the order is irrelevant. The result will be the same regardless of the order in which the conditions are evaluated. a and b is exactly the same result as b and a.

The tricky part is when you need to mix operators.
a or b and c or d
Now we have to perform the conditions based on the order of precedence so AND happens first. To explain this, I will add parentheses to show how the statement is evaluated
a or (b and c) or d
Once the AND is handled, we now have three values instead of four and they are related by OR since we evaluated the AND away.

Rather than depend on you or your readers understanding how the compiler will evaluate complex expressions, it is best to always use parentheses when you use different operators. Perhaps you wanted
a or b and c or d
to be evaluated as
(a or b) and (c or d)
That would most likely give a different result than the default interpretation. Or how about:
((a or b) and c) or d
As you can see, there are several different results and only YOU know what you want.
 
Last edited:

Micron

AWF VIP
Local time
Today, 14:13
Joined
Oct 20, 2018
Messages
3,478
If a <> b will result in FALSE if either a or b is null. No text of a null value will ever return true except IsNull()
Not that I ever said "no text of a Null will ever return true" because I have no idea what that means. I've never heard of "a text of a Null" but as for the rest, did you test?

?6<>5 and 3=null and 7<>9
Null
?6<>Null and 3=3 and 7<>9
Null
?6<>5 and 3=3 and 7<>Null
Null
 

kitty77

Registered User.
Local time
Today, 14:13
Joined
May 27, 2019
Messages
710
Thanks for all the good info on this subject. Got it working!!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:13
Joined
Feb 19, 2002
Messages
43,204
@Micron,
Null is equivalent to "Not True" so at it's simplest,
a<> b
when either a or b is null, the result is null which is treated as false when most people would want the result to be true because it is "true" that the two fields are not equal.

The upshot of this is --- be careful in how you construct your conditionals so you take into consideration the possibility that one or more of the arguments might be null
 

Attachments

  • testNull.JPG
    testNull.JPG
    32.3 KB · Views: 278

Micron

AWF VIP
Local time
Today, 14:13
Joined
Oct 20, 2018
Messages
3,478
You're not telling me anything that I don't already know.
Maybe this is all just semantics, but I'm not going to agree that "Null is equivalent to "Not True"" because Null is not equal to anything. Equivalent means equal or corresponding to. Null is neither equivalent, corresponding or equal to anything. I'm sure I don't have to tell you that, so I mention it for the benefit of others. Your example is obvious as I've just alluded to: b is not equal to null, thus the expression evaluates to null. I think I already showed that with my immediate window post results so I don't get why you posted that.

Still would like to know what a "text of a null" is. Or let's just move on?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:13
Joined
Feb 19, 2002
Messages
43,204
People who are new to development don't understand the implication of null. I'm sure you do understand it but I'm trying to get to a basic clarification of exactly what the impact of a null operand is. arnel suggested that a<>b would return false if one of the operands was null. I agree that statement is technically incorrect because it will in fact return null (which was the objective of the debug.print exercise) but in practice, the result is treated as false when used in a where clause or an If statement because null can never be true. So in a binary world where a result is true or not true, null ends up as false.

Select * from MyTable Where a <> b;

What happens when a = 10 and b is null? Is the record selected (true) or not (false)?
 

Micron

AWF VIP
Local time
Today, 14:13
Joined
Oct 20, 2018
Messages
3,478
the result is treated as false when used in a where clause
Do you have an example of that; specifically how you would use Null in a Where clause? In my experience, if you put =Null in a query design you will get nothing because as we agree, null is not equal to anything. If you put Null in the query, Access will convert that to Is Null, which is a function. If you try to do this in vba, you'll get an error.

So how would you use null in a where clause as you seem to be saying?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:13
Joined
Oct 29, 2018
Messages
21,447
Do you have an example of that; specifically how you would use Null in a Where clause? In my experience, if you put =Null in a query design you will get nothing because as we agree, null is not equal to anything. If you put Null in the query, Access will convert that to Is Null, which is a function. If you try to do this in vba, you'll get an error.

So how would you use null in a where clause as you seem to be saying?
Hi. Pardon me for jumping in, but here's what I just tried...

Code:
SELECT * FROM TableName WHERE 1 = TempVars!Test
 

Users who are viewing this thread

Top Bottom