473,422 Members | 2,096 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,422 software developers and data experts.

Cancel Print Action if Sub form has No Records

DJRhino1175
221 128KB
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -

Expand|Select|Wrap|Line Numbers
  1. If subfrmLotInfo Is Null Then
  2.     MsgBox "You are required to enter your Material Lot Info", vbCritical, "No Material Lot Info"
Here is the code as a whole...

Expand|Select|Wrap|Line Numbers
  1. Private Sub CMBPrint_Click()
  2. If Me.Dirty Then
  3.       Me.Dirty = False
  4. End If
  5.  
  6. If subfrmLotInfo Is Null Then
  7.     MsgBox "You are required to enter your Material Lot Info", vbCritical, "No Material Lot Info"
  8.  
  9. Else
  10.  
  11. If Me.NewRecord Then
  12.         MsgBox "Select a record to print", vbOKOnly, "No Record To Print"
  13. Else
  14.  
  15.     DoCmd.OpenReport "OEE-RPT", acViewPreview, , "LineID =" & [LineID], acWindowNormal
  16.     DoCmd.PrintOut acSelection, , , acHigh
  17.     DoCmd.Close acReport, "OEE-RPT", acSaveYes
  18.     DoCmd.GoToRecord acDataForm, "FrmLine", acNewRec
  19. End If
  20. End If
  21. End Sub
My question is how do I make this work?

Thanks in advance for the help. I hope I asked the question correctly so NeoPa doesn't scold me...lol - I'm kidding my friend...
Sep 7 '23 #1

✓ answered by NeoPa

Hi DJ.

Now I've really hit that Doh! moment. Is Null is SQL syntax - not VBA. When working so heavily in both it's easy to get them confused :-(

The Is Operator in VBA is for comparing/checking one object against another. Using it with Null is not valid. Instead, if we want to check for Null, we use the IsNull() Function. See if this works for you :
Expand|Select|Wrap|Line Numbers
  1. If IsNull(Me.subfrmLotInfo.Form.Controls("LotNo").Value) Then
You may even get away with dropping the .Value part at the end, but check first.

Sorry I misled you along the way. Hopefully this just works for you :-)

14 17453
NeoPa
32,556 Expert Mod 16PB
Hi DJ.

I suspect that friendly guidance from me in the past, far from disadvantaging you, has enabled more of your threads to garner positive & helpful responses ;-) As a friend, however, you're more than welcome to throw little digs my way & can expect them to bounce off my gnarly old hide with nothing but amused acceptance :-)

Let me start by pointing out that lines #2 through #4 actually ensure that any changed data currently on the Form (so necessarily a bound Form) is saved to disk. I suspect that is your intent so just making sure we're on the same page.

Moving on to the code in question - lines #6 through #9 - I would assume that you have a SubForm control on your Form which is called [subfrmLotInfo]. Please clarify at any point if any of my assumptions are invalid. Of course I understand you do your best to provide all relevant information, but that isn't always easy and you develop better skills as you proceed. So, where I need to I'll have to make assumptions ;-)

If, then, that is a reference to your SubForm, we know that can never be Null. However, and this is important, your reported error message indicates, not a failed check, but a failure even to recognise [subfrmLotInfo] as an object. So, assuming you do have a SubForm control, and we know that while Me.subfrmLotInfo may be a more appropriate way to reference it your reference would also be recognised if it were valid, it seems the name is not correct/valid in some way.

It may be that the name [subfrmLotInfo] is actually the Form object you have designed to be used within your SubForm control - rather than the name of the SubForm itself. I don't know but it's worth considering.

When you have this resolved appropriately then we still have a problem because, as stated earlier, a reference to an existing control will never be Null. We need to change that to a check of the records within the Form that is held in the SubForm control. This may be a good time to check out Referring to Items on a Sub-Form.

Unfortunately, some Forms have Recordsets that are too big to load all at once and when that happens the .RecordCount property returns 0. This means you cannot simply rely on checking that value and must be more creative to determine if there are any records.

To know which approach to use I would need some more details as to what you expect in that SubForm. The OnCurrent event of the Form itself (The one used in the SubForm I mean.) could prove fruitful. Sometimes you can check a Bound Control on that same Form if you have any which cannot be Null except for an empty record.

Perhaps you can reply back with what approach suits you best. Anyway, good luck with your project :-)
Sep 7 '23 #2
DJRhino1175
221 128KB
NeoPa:
Let me start by pointing out that lines #2 through #4 actually ensure that any changed data currently on the Form (so necessarily a bound Form) is saved to disk.
I suspect that is your intent so just making sure we're on the same page.
This is intentional, the form is bound.
NeoPa:
Moving on to the code in question - lines #6 through #9 - I would assume that you have a SubForm control on your Form which is called [subfrmLotInfo].
No this is the name of the subform...Controls are "Section", "Material" and "LotNo" - I'm guessing this is where I went wrong. I need to add a control to the code, like "subformlotinfo.section"....
NeoPa:
Please clarify at any point if any of my assumptions are invalid.
OK
NeoPa:
If, then, that is a reference to your SubForm, we know that can never be Null. However, and this is important, your reported error message indicates, not a failed check, but a failure even to recognise [subfrmLotInfo] as an object. So, assuming you do have a SubForm control, and we know that while Me.subfrmLotInfo may be a more appropriate way to reference it your reference would also be recognised if it were valid, it seems the name is not correct/valid in some way.
Most likely because of no control in the code...
NeoPa:
When you have this resolved appropriately then we still have a problem because, as stated earlier, a reference to an existing control will never be Null. We need to change that to a check of the records within the Form that is held in the SubForm control. This may be a good time to check out Referring to Items on a Sub-Form.
I just need to check if there is data in the subform linked to the main form. If there isn't I need the msgbox to pop up and force them to put the info in...

I hope this clarifies what I'm looking to do. It sounded way easier in my head until I started writing the code.
Sep 7 '23 #3
NeoPa
32,556 Expert Mod 16PB
Hi DJ.

Thank you for responding directly to all my questions :-) I've edited your post simply to make the quoting clearer to readers.

DJRhino:
NeoPa:
Moving on to the code in question - lines #6 through #9 - I would assume that you have a SubForm control on your Form which is called [subfrmLotInfo].
No this is the name of the subform...Controls are "Section", "Material" and "LotNo" - I'm guessing this is where I went wrong. I need to add a control to the code, like "subformlotinfo.section"....
If you read my linked article (Referring to Items on a Sub-Form) you'll know that simply referring to 'subform's is almost guaranteed to be confusing. Actually, the only item that can properly be referred to as a SubForm is the SubForm control itself. This is a control on your main form that may contain a separate Form (Such controls can & do exist without any separate Form being specified at both run-time & design time.) but is not a Form itself, but a Control obviously.

My assumption at this point is that [subfrmLotInfo] is actually a Form object that happens to be designed into use by a SubForm control on your main Form whose name we have not yet shared. It would be helpful to determine this name & share it in order to be able to continue reliably.
DJRhino:
NeoPa:
... it seems the name is not correct/valid in some way.
Most likely because of no control in the code...
Not remotely likely.
Yes, you do need to format the reference either to include a Control or otherwise determine if the Form is empty of data.
No, that is not the point the compiler error message & I were both trying to bring to your attention. The name of the SubForm control must be something other than [subfrmLotInfo]. This is important as, until you get that right, nothing else will even get to being tested.
DJRhino:
I just need to check if there is data in the subform linked to the main form. If there isn't I need the msgbox to pop up and force them to put the info in...
No kidding ;-) This is basically a re-statement of the original requirement. Not wrong, but nor does it really help at all.

We do now have the names of the bound Controls. To reference them in code we will need to know the name of the SubForm control. We will also need to select a Control, from those you've listed, that can never be Null unless in an empty record. Obviously (I hope), we also need to be clear that no navigation of records occurs before we run the test. I suspect that's pretty safe but it makes sense to raise the point for a fuller understanding.

I hope this helps you :
  1. To solve your current issue.
  2. To better understand what's going on so future situations are clearer in your head & thus easier to deal with.
Sep 7 '23 #4
DJRhino1175
221 128KB
NeoPa,

When I go into design mode and look at the "Name" of the control[sub form] it says "subfrmlotinfo"

I did read the article link you gave and based on my under standing I did the following:

Expand|Select|Wrap|Line Numbers
  1. If me.subfrmLotInfo![SectionID] Is Null Then
  2.     MsgBox "You are required to enter your Material Lot Info", vbCritical, "No Material Lot Info"
It compiles just fine but still gives me the same runtime error. So my guess is my understanding of the issue isn't quite right.
Sep 8 '23 #5
NeoPa
32,556 Expert Mod 16PB
Hi DJ.

Let's see if we can build this up from what we have then. I will assume, as this was not clarified or confirmed in your latest reply, that :
  1. The Control on the Form you use within your SubForm is called [LotNo] and that if a record exists then the value of this Control can NOT be Null.
  2. At the point when you are running this code the SubForm and its Form are running normally and no record navigation has occurred.

We now know that the SubForm control is named [subfrmLotNo] so checking the value of the [LotNo] control for Null should work reliably. The code you'd need for that woul be :
Expand|Select|Wrap|Line Numbers
  1. If Me.subfrmLotInfo.Form.Controls("LotNo") Is Null Then
Obviously, if the names of any objects are not as we've been led to believe then all you need to do is amend that part in the code to match your actual names. For instance, if the Field is [LotNo] but your actual control, reported in post #3 as that, is actually [txtLotNo], then simply replace one with the other. Ultimately, make sure the names in the code exactly match those actually in your project.

You can even abbreviate the code by using bang (!) references in place of the fully-specified dot (.) notation I've used, but I suggest you only try that if/when you understand what the changes mean.

I hope you enjoy your weekend :-)
Sep 8 '23 #6
DJRhino1175
221 128KB
NeoPa,

I updated the code to the following:

Expand|Select|Wrap|Line Numbers
  1. If Me.subfrmLotInfo.Form.Controls("LotNo") Is Null Then
It compiles just fine, but when I run it I still get the same "Runtime Error of 424: Object Required". I go to debug and it highlights the line of code above.

I verified on the subform there is a text box with the name of "LotNo"
Sep 11 '23 #7
NeoPa
32,556 Expert Mod 16PB
Hi DJ.

Ah. Silly me. An object reference can, itself, be checked for Null. This means the code will assume that is the test, when really we only want to test the .Value. Try this instead :
Expand|Select|Wrap|Line Numbers
  1. If Me.subfrmLotInfo.Form.Controls("LotNo").Value Is Null Then
It's a bit harder when you don't have the code in front of you to test, but hopefully that's the right solution this time :-)
Sep 11 '23 #8
DJRhino
106 64KB
NeoPa,

Nope same Runtime error of 424...
Sep 12 '23 #9
NeoPa
32,556 Expert Mod 16PB
Hi DJ.

Now I've really hit that Doh! moment. Is Null is SQL syntax - not VBA. When working so heavily in both it's easy to get them confused :-(

The Is Operator in VBA is for comparing/checking one object against another. Using it with Null is not valid. Instead, if we want to check for Null, we use the IsNull() Function. See if this works for you :
Expand|Select|Wrap|Line Numbers
  1. If IsNull(Me.subfrmLotInfo.Form.Controls("LotNo").Value) Then
You may even get away with dropping the .Value part at the end, but check first.

Sorry I misled you along the way. Hopefully this just works for you :-)
Sep 12 '23 #10
DJRhino
106 64KB
NeoPa,

This worked perfectly. No worries about any misleads...It does help me learn things that way to....

I so appreciate your help in this matter.

DJ Rhino
Sep 12 '23 #11
DJRhino
106 64KB
NeoPa,

There is one more step I need to add to this. I would like to set focus to SectionID on this subform.

I tried:

Expand|Select|Wrap|Line Numbers
  1. Me.subfrmLotInfo.Form.Controls("SectionID").SetFocus
Sep 12 '23 #12
NeoPa
32,556 Expert Mod 16PB
Hi DJ.

Let's try to keep this to one question per thread if we can.

In this case your question doesn't make clear exactly what happened when you tried that so I'll guess it didn't work and if more detail is required then I'll let you continue by creating a new thread for the question.

I would start by making the SubForm control itself active. If you further need to select a Control on the Form contained within the SubForm then what you tried earlier could be run after that.

To make the SubForm active try :
Expand|Select|Wrap|Line Numbers
  1. Call Me.subfrmLotInfo.SetFocus()
Sep 12 '23 #13
DJRhino
106 64KB
NeoPa,

Worked perfectly. Thanks.
Sep 13 '23 #14
NeoPa
32,556 Expert Mod 16PB
I'm pleased to hear that DJ.

Onwards & upwards.
Sep 13 '23 #15

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

Similar topics

3
by: sea | last post by:
I need to be able to print the current form because the form has an embedded object of a .gif file, so report will not work -- does anyone know the way to do this? I tried some code, not sure what...
3
by: Michele | last post by:
Hi all, I have a form with 3 combobox whitin a asp page called data.asp, whose action form is itself; the first combobox drives the behaviour of the other two. The onchange event of the first...
5
by: Dino M. Buljubasic | last post by:
I'd like to be able to print my form or some controls on it. I have found lots of references for this but they all talk about printing a text document from a form. Any help will be appreciated...
4
by: Bernard Bourée | last post by:
How to print a Form and to fit it with the printer page and layout ? -- Bernard Bourée bernard@bouree.net
4
by: Deano | last post by:
I'm in the situation of having to print a form - not a report. This will fit nicely in a landscape format but by default it prints portrait. Is there any code to make it play nice?
4
by: shaiful | last post by:
Hi all, I have a very small problem that i want to print on form, I wrote code under commandbutton1: Dim MyVar MyVar = "Hello." Print MyVar its works in vb6, but it doesnt work in VBA? could...
4
by: Robson Felix | last post by:
It may sound like a silly question, but I would like to print a form or component from within Visual Studio when designing such form component. Is that possible?
1
by: nuwan1988 | last post by:
i want to print active form with active record in access 2010. i used the following cord to print my form, it's working, but it is always print first record of the database only. active record of the...
3
by: dylankirs | last post by:
Hi. Apologies for any incorrect/off technical lingo. Fairly new to Access and would appreciate any help! I'm trying to create a command button that will filter form records based on an already...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.