You are viewing limited content. For full access, please sign in.

Question

Question

Can my lookup be set to ignore NULL VALUES?

asked on April 5, 2022

I have two tables side-by-side, filling lookup values from the same SQL table, and I'm getting extra rows because of null values.  How can I get my tables to fill without the null values?

 

0 0

Replies

replied on April 5, 2022 Show version history

Set up a view

CREATE VIEW vw_PPE AS 

SELECT ppe FROM generalTable

WHERE ppe IS NOT NULL;

and then use the view instead of the table in the lookup.

0 0
replied on April 5, 2022

Thanks, James.  Unfortunately, although I was learning about using views in lookup rules last week, I discovered that this particular form needs more flexibility than the views can give me.  Each table is filled based on which task was picked.  ie, if Task #324 was picked, then load all of the #324 rows.  

This is the same form today, but a different table, however, there is still the need to isolate the rows based on which task was picked.  For each task, the hidden SQL tables have different hazards, different PPE and different ... well ... a lot of different tables with collections of things.

I guess I could create a different SQL table for each different thing, but I was combining things where possible.  In this case the SQL table holds two categories of things (PPE, and IN PLACE) and the tables load based on which category was picked.  But it needs to pull in only the PPE or IN PLACE items for the right task.

0 0
replied on April 5, 2022

Oh!  I figured it out!  I had to have another hidden field to isolate only PPE:

It works!

0 0
replied on April 5, 2022

James!  I'm working on another problem, maybe you can help me?  

Another table in this form is trying to pull emergency plan rows out of the EP SQL table, but with some IF/THEN options.  I was wondering if views would work, but... I can't get the Look Up Rules to fill a different view if a different thing is happening.

Here is my tiny SQL table, where I want ALL the rows except the Grassfire Awareness row... UNLESS, the Department is Ag Service Board OR!! or the Crew is Maintenance Graders:

0 0
replied on April 5, 2022

Hi Connie!

So, you're trying to get something like this?

If Department ≠ Ag Service Board AND Crew ≠ Maintenance Graders then SELECT ALL WHERE Plans ≠ "Grassfire Awareness"

If Department = Ag Service Board then SELECT ALL WHERE Department = Ag Service Board

If Crew = Maintenance Graders then SELECT ALL WHERE Crew = Maintenance Graders

Have I correctly understood the goal?

If I have correctly determined it, then 2nd and 3rd one are fairly easy since they are just evaluating equality, which Forms does easily with lookups.  But the 1st one is not so easy since it's evaluating inequality, which Forms doesn't really do.  You could probably make it work in Forms with check values for the Department and Crew fields, and ensuring they are blank if they are not those two expected values.

0 0
replied on April 6, 2022

Thanks, Matthew!!

So, it's:  Select Form Number "ALL", but if Department is "Ag Service Board" OR Crew is "Maintenance Graders", add "Grassfire Awareness".

Your first option would work, I think, for all the normal ones, but how do we get it to work for the exceptions?

But, you've given me an idea...  no, wait... still doesn't fit all possibilities.

Ultimately, I need three options, but how to get them... I thought I could have hidden fields collecting the department and crew fields, then have the if statement of If (this) or (that) then _____ and there would be another column in the SQL table to match up with.  But I haven't been able to get the if statement working and haven't been able to decide on what the extra column in the SQL would be yet.  Do you think something like this would work?

I've unhidden my collector fields for now and you can see the Part 2 isn't collecting anything yet (simply because I haven't found how to say "if Part 1 contains ______ instead of equals _____ [one of the two exceptions]).

0 0
replied on April 6, 2022 Show version history

For the way you are describing it, I think you're going to be best served by a Stored Procedure, which works a little differently than a View.
Where a View is basically just a saved query, a Stored Procedure has a lot more ability to manipulate input data to return different results.

If you want to pursue this, then in SQL Management Studio, you'd go to the "Programmability" folder instead of Tables or Views, then inside there, open the "Stored Procedures" folder, and right-click and select the option to add a new stored procedure.  You'll get something that looks like this:

-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName> 
	-- Add the parameters for the stored procedure here
	<@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, 
	<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END
GO

Notice that says to press Ctrl+Shift+M to populate Template Parameters.  You'll get a popup like this, which you can fill out similar to how I've done in the screenshot (obviously using your name instead of mine and a more appropriate description).

Now it should be looking something like this. 

-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Matthew Tingey
-- Create date: April 6, 2022
-- Description:	Demonostration for Connie.
-- =============================================
CREATE PROCEDURE DemoForConnie 
	-- Add the parameters for the stored procedure here
	@Department varchar(50) = , 
	@Crew varchar(50) = 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT @Department, @Crew
END
GO

Notice that it is likely showing an error on lines 23 and 24 because those parameters are showing equal to nothing (because I left them blank in that pop-up window).  You can just delete the equal signs here - like this: 

@Department varchar(50), 
@Crew varchar(50)

You may notice, this isn't doing much right now.  It takes in two parameters, the @Department and @Crew, and then returns those same parameters back in the SELECT statement.

From here's we're going to edit that SELECT statement to handle the data you need.

So delete the SELECT statement on line 32, and replace it with the following (tweaking it for the apporiate table name, as I didn't see that in your post before): 

SELECT
  [Form Number],
  [Category],
  [Plans],
  [Department],
  [Crew],
  [Line #]
FROM [TableNameGoesHere]
WHERE
  [Form Number] = 'ALL'
  OR [Department] = @Department
  OR [Crew] = @Crew

This allows the two values to be passed in and will return the rows they match, plus all rows where the [Form Number] is set to ALL.

When you executive this script, it isn't going to run the query, it's going to create the Stored Procedure.  In the folder selector, you can hit Refresh, and you should now see the new Stored Procedure.  You can right-click on it, and select the option to Execute it, which will allow you to test it, passing different values into the @Department and @Crew Parameters.

Once you've fully tested it and you are happy with it, you can go into Forms and refresh your data source so that it can see the new Stored Procedure.

On your form itself, on the lookups, you'll need to change the second field from "Table / View" to "Stored Procedure", but then your new procedure should be an option to select.  You'll be able to pick the two fields to pass in for the @Department and @Crew parameters.  And then the fields that will be populated by the returning values.  Unless it has changed with newer versions, there is a difference with Stored Procedures versus Tables or Views where Forms won't pre-populate the returning column names and you'll need to do it manually (Form Number, Category, Plans, Department, Crew, Line #).

0 0
replied on April 7, 2022

Thanks, Matthew!!  I don't seem to have that option.  When looking, I saw a few messages like "needs to be loaded".  I'm wondering if we purchased a basic pkg and don't have this.  I think I'm going to have to contact my IT to see if this is available.

0 0
replied on April 7, 2022

It could be something like, you are on Express and it isn't included, or it could be a situation where that functionality wasn't installed - there are a lot of parts in MSSMS that are optional when installed.

If you don't have it, and cannot get it, then you'll probably have to resort to a more complex set-up like mutliple tables in Forms that perform the same (but slightly different) function - or a View that includes all possible options for Department and Crew, repeating all the values for each one - or something else entirely.

0 0
replied on April 7, 2022

Okay, I have come up with a temporary solution that I'm excited about!

First:

  • I have the Part 1 hidden field filling whenever the Department is Ag Service Board or the Crew is Maintenance Graders
  • I have the Part 2 hidden field defaulted with just "ALL".

Then,

  • I have duplicated the Emergency Plan table and used field rules to hide/show based on what is in the Part 1 hidden field (or NOT in this field).
  • I have the lookup rules filling both tables, but one of them is told to just fill the rows where the one column says ALL.

This way,

  • If the Part 1 Hidden field is blank, the ALL table always is there, already filled with all the ALL rows.
  • If the Part 1 hidden field is NOT blank, the other table shows up which just fills with every row in the table.

 

This works!  This is actually the idea you got me started on, Matthew, but then I thought it was too complex.  But if I removed one of the Grassfire Awareness rows, then I was able to do it.  Yea!

1 0
replied on April 7, 2022

Fantastic!

That's one thing that is great about Forms, there is often many ways to achieve a goal.

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.