473,383 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Two if isNull statement

22
I have two combox which i want to select before open command2
and I have it right but the problem I encounter is that, if B2 is selected it overide and open cammand2. Here is code below:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command2_Click()
  2.     If IsNull(B1) Then
  3.         MsgBox ("Please Select B1 To Proceed")
  4.     End If
  5.     If IsNull(B2) Then
  6.         MsgBox ("Please Select B2 To Proceed")
  7.     Else
  8.         DoCmd.RunMacro "NEW ASSETS Query2"
  9.     End If
  10. End Sub
Jul 4 '18 #1

✓ answered by NeoPa

Mafi:
I have it right but the problem I encounter is that
How can anyone write that. It can't possibly make any sense. If you had it right then there couldn't be a problem - by definition. Obviously you don't have it right.

I assume you want to run the macro if, and only if, both [B1] & [B2] have values set. To do that you need to make the code reflect that :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command2_Click()
  2.     With Me
  3.         If IsNull(.B1) Then
  4.             Call MsgBox("Please Select B1 To Proceed")
  5.             Call .B1.SetFocus
  6.         ElseIf IsNull(.B2) Then
  7.             Call MsgBox("Please Select B2 To Proceed")
  8.             Call .B2.SetFocus
  9.         Else
  10.             Call DoCmd.RunMacro("NEW ASSETS Query2")
  11.         End If
  12.     End With
  13. End Sub

8 1204
NeoPa
32,556 Expert Mod 16PB
Mafi:
I have it right but the problem I encounter is that
How can anyone write that. It can't possibly make any sense. If you had it right then there couldn't be a problem - by definition. Obviously you don't have it right.

I assume you want to run the macro if, and only if, both [B1] & [B2] have values set. To do that you need to make the code reflect that :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command2_Click()
  2.     With Me
  3.         If IsNull(.B1) Then
  4.             Call MsgBox("Please Select B1 To Proceed")
  5.             Call .B1.SetFocus
  6.         ElseIf IsNull(.B2) Then
  7.             Call MsgBox("Please Select B2 To Proceed")
  8.             Call .B2.SetFocus
  9.         Else
  10.             Call DoCmd.RunMacro("NEW ASSETS Query2")
  11.         End If
  12.     End With
  13. End Sub
Jul 4 '18 #2
Mafi
22
I tried but pop up Compile error: end with without with
Jul 5 '18 #3
PhilOfWalton
1,430 Expert 1GB
Two points.

In the above code, remove all instances of the word "Call".

I would strongly advise against Macros (Except 'AotoExec'). Firstly they are far less powerful than VBA Code, and equally as important, if everything is in code, it can all be seen in one place, and if there is a problem with the code running, it can simply be stepped through, line by line to see where unexpected values appear.

Using Macros, one has to change from the VBA window to the Macro Window.

Phil
Jul 5 '18 #4
Mafi
22
Oh! I miss something from the code suggested by Neopa that is why am getting the Error. Now works perfectly. Thanks
Jul 5 '18 #5
NeoPa
32,556 Expert Mod 16PB
Phil:
In the above code, remove all instances of the word "Call".
That wouldn't be a good idea Phil.

If, for reasons that are beyond me, you should ever feel the need to make the calls the less clear way, then you'd need to remove the parentheses from around the parameters too. Not sure why anyone would choose the more clumsy approach over the less, mind.

You may notice that the original code suffered from having the parentheses without the Call.
Jul 5 '18 #6
twinnyfo
3,653 Expert Mod 2GB
Phil:
In the above code, remove all instances of the word "Call".
That wouldn't be a good idea Phil.
@NeoPa,
To help me understand, is there an advantage using
Expand|Select|Wrap|Line Numbers
  1. Call MsgBox("Please Select B1 To Proceed")
over using
Expand|Select|Wrap|Line Numbers
  1. MsgBox "Please Select B1 To Proceed"
I have always used the latter, and have seen some folks using the Call Statement frequently, but found that method superfluous.

Again, I like to understand why Access Jedis use the methods they use, as sometimes the invisible things help me understand the bigger picture (such as Boolean values "<> False").
Jul 5 '18 #7
PhilOfWalton
1,430 Expert 1GB
I always use
Expand|Select|Wrap|Line Numbers
  1. MsgBox "Please Select B1 To Proceed"
  2.  
Equally I would use
Expand|Select|Wrap|Line Numbers
  1.     With Me
  2.         .B1.SetFocus
  3.     End With
  4.  
I don't see what the "Call" achieves

In fact, in one of my programs, I have to use a user defined MsgBox function (for messages in non Latin scrips, like Russian) and still use the same syntax.

Phil
Jul 5 '18 #8
NeoPa
32,556 Expert Mod 16PB
Phil:
I don't see what the "Call" achieves
Let me try to explain.

The reason I recommend using the Call syntax is that it tells the reader immediately that the line branches to a procedure and is not a statement of direct VBA action, nor is it an array reference, which also uses parentheses for the element indicator. It's often helpful to see where, and how often, any code branches off elsewhere to get things done. Using Call is explicit and makes that obvious.

So, while it may not be very common practice, it's certainly a better way to work - particularly if you have to work with others at any stage.

Another recommendation, based on seeing it confuse people a great deal, is that where there is any complication to a procedure call, and of course where it's even possible, named parameters should be used instead of positional parameters. I've lost count of how many questions have been seen here where members have simply lost count of the commas between parameters and one or more has ended up in the wrong place.
Jul 5 '18 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Stephen Patten | last post by:
Hi All, While in the process of building my table (40 or so Insert statments) can I then query ("select * from @Table_variable") and use the results up to theat point for another insert into...
5
by: Rachel Weeden | last post by:
I'm working on an ASP Web application, and am having syntax issues in a WHERE statement I'm trying to write that uses the CInt Function on a field. Basically, I want to select records using...
1
by: Joerg Trumpfheller | last post by:
Hey freakz, I have a sql staement like this (for MS-SQLServer): Dim command As OleDb.OleDbCommand command.CommandText = "SELECT Table1.ID, ISNULL(Table1.STATUS, '') AS STATUS, " & _ "...
2
by: Raoul Watson | last post by:
I have used isNull statement for as long as I have used VB.. Recently I am devugging a program and it is very clear that the "IsNull" function sometimes would return a true even when the value is...
16
by: madeleine | last post by:
Please can someone help me, I think I may go mad with this one: Do While Not IsNull(CDate(FormatDateTime(rst!F1.Value, vbShortDate))) If IsNull(CDate(FormatDateTime(rst!F1.Value, vbShortDate)))...
1
by: josh2007 | last post by:
hi all, I'm having a hard time producing the correct sql statement for my tables. below is my sql statement but i'm getting incorrect result. This particular query returns the sum of all the...
0
by: nasirpinnacle | last post by:
I wrote a Function who returns around 800000 records in 17-20 Seconds but if i use 7 differernt simple case Statements its time increases to 03 min and 10 seconds.. Can somebody help me. Table...
1
by: Dani | last post by:
Hi, I'm looking for some guidance on how to approach a problem. We are continuously pulling recurring work order tasks from one database into a new one that I've built in Access (one with more...
9
by: Promise007 | last post by:
I am in the process of trying to build an expression in Access 2016. I have three tables that are joined by a common field in a query. The first table is called No Math and it has a field named No...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.