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

IsLoaded

12
here is the deal I have a form with a command button to open another form. I would like to check if another form is loaded and use the code to close that form. simple right?

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Dim obj As AccessObject, dbs As Object 
  4.  
  5. Set dbs = Application.CurrentProject 
  6. Set frm = dbs.Forms!frmCaseInfo 
  7.  
  8. If frm.IsLoaded = True Then 
  9.  
  10.  DoCmd.SelectObject acForm, "frmCase", False 
  11.  DoCmd.Close
  12.  
  13.  
I've dinked with variations on the above code and get an error "the form is not loaded" or "application object not defined" or "that method not available".

I cannot determine why. What am I missing?

any help is greatly appreciated & I'm much obliged

/jh
Mar 4 '09 #1
15 2575
FishVal
2,653 Expert 2GB
Hello, JohnHo.
  • "Form" class as well as "Form_<formname>" (class of particular form) class has no method/property IsLoaded.
  • "Forms" collection contains only opened forms (except those opened within subform controls).

Do you really need to check whether a form is opened just to close it?

Regards,
Fish
Mar 4 '09 #2
NeoPa
32,556 Expert Mod 16PB
@FishVal
I think you'll find this means that your line #6 will fail if the form isn't already loaded (open).

You can use On Error code to determine whether or not the form is open when you try to access it.
Mar 4 '09 #3
missinglinq
3,532 Expert 2GB
This code will do it, replacing FormToBeClosedName with your actual form name:
Expand|Select|Wrap|Line Numbers
  1. Dim cp As CurrentProject
  2. Dim Frms As Object
  3.   Set cp = CurrentProject()
  4.   Set Frms = cp.AllForms
  5.  
  6.   For Each Frm In Application.Forms
  7.     If Frms("FormToBeClosedName").IsLoaded Then
  8.       DoCmd.Close acForm, "FormToBeClosedName"
  9.     End If
  10.   Next Frm
  11.  
  12.   Set Frms = Nothing
  13.   Set cp = Nothing
But as FishVal suggested, you don't really need to check to close it. If you try to close a form that isn't loaded, nothing untoward happens.

Linq ;0)>
Mar 4 '09 #4
ADezii
8,834 Expert 8TB
Us 'Old Dudes' rely on the traditional approach to checking as to whether or not a given Form is Loaded, and sorry NeoPa, but IMHO, I do not feel as though you should ever rely on an Error being generated to verify that a Form is Loaded/Unloaded, when a viable alternative exists.
Expand|Select|Wrap|Line Numbers
  1. Function IsLoaded(ByVal strFormName As String) As Boolean
  2. 'Returns True if the specified form is open in Form view or Datasheet view.
  3.  
  4. Const conObjStateClosed = 0
  5. Const conDesignView = 0
  6.  
  7. If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
  8.   If Forms(strFormName).CurrentView <> conDesignView Then
  9.     IsLoaded = True
  10.   End If
  11. End If
  12. End Function
Expand|Select|Wrap|Line Numbers
  1. If IsLoaded("<Your Form Name>") Then
  2.   DoCmd.Close acForm, "frmmemo", acSavePrompt
  3. End If
Mar 4 '09 #5
MikeTheBike
639 Expert 512MB
Hi

In the early days (A97?) this seemed to be the way to check for open forms
Expand|Select|Wrap|Line Numbers
  1. Function FormIsLoaded(ByVal strFormName As String) As Boolean
  2.     FormIsLoaded = False
  3.  
  4.     Const conObjStateClosed = 0
  5.     Const conDesignView = 0
  6.  
  7.     If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
  8.         If Forms(strFormName).CurrentView <> conDesignView Then
  9.             FormIsLoaded = True
  10.         End If
  11.     End If
  12.  
  13. End Function
  14.  
(taken directly from am MS publication).

Layer versions of Access permitted/provided this syntax/object

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_AfterUpdate()
  2.     If CurrentProject.AllForms("frmEmployees").IsLoaded Then Form_frmEmployees.LineManagerID.Requery
  3.     If CurrentProject.AllForms("frmNewEmployee").IsLoaded Then Form_frmNewEmployee.LineManagerID.Requery
  4. End Sub
Just though it might help !?


MTB
Mar 5 '09 #6
NeoPa
32,556 Expert Mod 16PB
ADezii,

I agree that using On Error is not ideal. For me though, to use SysCmd is even less so. From my point of view that should be reserved for doing something that's logically outside of the application. I'm a strong believer in the logic of code. Being readable as well as working correctly has always seemed an important issue to me. That's not to say that I'm right and you're wrong by any means. Simply that we would disagree on this particular point.

In fact, since MTB has posted, I suspect he's shown some checking code that we would both be happy to recommend.
Mar 5 '09 #7
ADezii
8,834 Expert 8TB
@NeoPa
I just figured since Microsoft has use this code syntax in the Northwind Sample Database since the beginning ot time, there must be something to it! (LOL)!
Mar 5 '09 #8
missinglinq
3,532 Expert 2GB
Right! They also went from the beginning of time until 2007 with the Command Button Wizard generating the code DoCmd.Close to close a form, even though it will dump a record, without warning, that fails validation before closing the form!

Not a very compelling argument, I'm afraid!

Linq ;0)>
Mar 5 '09 #9
NeoPa
32,556 Expert Mod 16PB
@ADezii
I'm afraid it's true that much of the sample code provided by Microsoft, as well as much of the code created by the wizards and macro-recording (Excel, Word etc) is some of the worst I've seen.

I wouldn't say that's true for the code you suggested ADezii. That wouldn't be my choice but it's far from naff. The fact that it's from Microsoft though is not really much of a recommendation I'm afraid.
Mar 5 '09 #10
ADezii
8,834 Expert 8TB
@missinglinq
I kinda thought that would be up to the Programmer to make sure that a Form will not close if the data contained within fails Validation, or at the least, give the User a Warning Message indicating failure, then an Option of some sort. I don't think you can blame Microsoft for that, Wizards, of any kind, were never designed to be an end all and should never be.
Mar 6 '09 #11
missinglinq
3,532 Expert 2GB
@ADezii
That the problem! If the programmer uses standard validation code like this

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.  If IsNull(Me.NName) Then
  3.    MsgBox "Name Field Cannot be Left Blank!"
  4.    Cancel = True
  5.    NName.SetFocus
  6.   End If
  7. End Sub
and the user leaves the NName field empty, then tries to move to another record or close the form, using the big Access X, the messagebox pops up and when OK is clicked, focus goes to the empty field.

On the other hand, if the user clicks on a custom "Close" button which uses the command DoCmd.Close, the messagebox appears, but on hitting OK the form immediately closes, not giving the user the opportunity to correct his/her error and dumping the record!

Likewise, if the "Required" property on a field has been set to Yes, the field left empty and the custom "Close" button hit, the form will close and dump the record without the usual warning.

In both cases the programmer has attempted to prevent the user from leaving a record incomplete, but using DoCmd.Close to close the form has negated these efforts.

Linq ;0)>
Mar 6 '09 #12
FishVal
2,653 Expert 2GB
Wow, Hollywar.

Kind regards,
Fish.
Mar 6 '09 #13
OldBirdman
675 512MB
If the form did not have an Access X, and when opened, the Close button were cmdClose.Enabled = False, then
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtText1()
  2.     If Nz(txtText1, "") = "" Or Nz(txtText2, "") = "" Then
  3.         cmdClose.Enabled = False
  4.     Else
  5.         cmdClose.Enabled = True
  6.     End If
  7. End Sub
  8.  
with similar code for txtText2.

This is not meant to cover all cases, as a Cancel button might want to drop any changes and close the form, and Close might be OK if Me.Dirty = False.
Mar 6 '09 #14
missinglinq
3,532 Expert 2GB
The simple workaround is, in fact, to use

Expand|Select|Wrap|Line Numbers
  1. If Me.Dirty Then Me.Dirty = False
  2. DoCmd.Close
as you suggested, which forces the save, thus triggering any validation/required field warnings. I'm told, in fact, that Microsoft actually added this to the Wizard "Close" code in 2007, thus finally addressing the problem after many years. Of course, this, in turn, introduced another bug! If you use the Wizard to generate a "Close" button on an Unbound form, such as one used as a menu or one used for selecting and printing reports, etc. it'll pop an error! Unbound forms have no Dirty Property!

Linq ;0)>
Mar 6 '09 #15
NeoPa
32,556 Expert Mod 16PB
@FishVal
Mischievous !
Mar 8 '09 #16

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

Similar topics

4
by: Jim Langston | last post by:
I understand the rule of three, that if I have a custom constructor, copy or destructor I probably need the other 2. My class object definately has a custom constructor and destructor, but I'm...
9
by: Tony Williams | last post by:
I have two tables 1.tblmonth which holds two fields txtmonth and txtqtrlabel and 2. tblmain which holds a number of fields but in particular a field called txtqtrlabel2. The two tables are linked...
3
by: Mark Richards | last post by:
I have this code behind a report (same report as previous post); Sub Report_Open(Cancel As Integer) DoCmd.OpenForm "Report Date Range", , , , , acDialog, "Activity By Location" If Not...
1
by: bbdata | last post by:
strange thing happened when i split my database. i have a search form where you enter parameters, and get back filtered records displayed on another form. when you close this one, theres a code in...
39
by: eruanion | last post by:
Hi, I've been working on this for a while now and I can't seem to find out what is wrong with my code. I have 2 files one c3common.js which only contains javascript functions for my main html page...
2
by: cp | last post by:
I'm a Perl programmer by background, and JavaScript is dim second or third choice. I have an AJAX application, and at one point in the code, I need to be sure that an object has been created before...
7
by: billelev | last post by:
Does anyone know if it is possible to close all objects in a database (tables, forms, queries etc.) using VBA code? I have not been able to find anything online to help me so far... My motivation...
7
imrosie
by: imrosie | last post by:
Hello, Is there anyone who can help me figure out how to resolve this conflict when I try to re-compile? It keeps telling the sub or routine is not defined...but it is!?! I think (I'm a newbie)...
5
JustJim
by: JustJim | last post by:
I'm not, though I wish I was! The un-help files tell me that I can use Isloaded to determine if an object is loaded (duh), but it doesn't tell me what loaded actually means. I'm trying to...
3
by: banderson | last post by:
Hello, I am not sure why this code is not working. I have a pop-form to edit a drop down list that is found in a control (combo box) on 8 different forms. On this pop-up form, I have added a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.