Checking if form is open

bknight
SilverLounger
Posts: 1705
Joined: 08 Jul 2016, 18:53

Checking if form is open

Post by bknight »

While testing a Db, I find that some of my code assumes that specific forms are open. Everyone knows what assume means, so I wrote a bit of code to check whether the form was loaded.

Code: Select all

If Forms("frmDailyprof").IsLoaded Then
    Forms("frmDailyprof").Requery
    Forms("frmDailyprof").SetFocus
Else:
DoCmd.OpenForm ("frmOpenPositionsFirst"), view:=acFormDS
    Forms("frmDailyprof").Requery
    Forms("frmDailyprof").SetFocus
End If
[code]
whatever the code will have to be run at least 4 times checking other forms.

User avatar
HansV
Administrator
Posts: 80248
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Checking if form is open

Post by HansV »

Forms("frmDailyprof").IsLoaded is not valid. Try

Code: Select all

    If Not CurrentProject.AllForms("frmDailyprof").IsLoaded Then
        DoCmd.OpenForm ("frmOpenPositionsFirst"), View:=acFormDS
    Else
        Forms("frmDailyprof").Requery
        Forms("frmDailyprof").SetFocus
    End If
Note that there is no need to set focus to and to requery the form if it has just been opened.
Best wishes,
Hans

bknight
SilverLounger
Posts: 1705
Joined: 08 Jul 2016, 18:53

Re: Checking if form is open

Post by bknight »

If Not If CurrentProject.AllForms("frmDailyprof").IsLoaded Then
compiles red.

bknight
SilverLounger
Posts: 1705
Joined: 08 Jul 2016, 18:53

Re: Checking if form is open

Post by bknight »

removing the second if compiles normal but I'm not sure if that would fit the scheme you are developing.
That opens the form.
It worked but why the AllForms instead of just Forms?

User avatar
HansV
Administrator
Posts: 80248
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Checking if form is open

Post by HansV »

Sorry, that was a typo. I'll correct it.
Best wishes,
Hans

bknight
SilverLounger
Posts: 1705
Joined: 08 Jul 2016, 18:53

Re: Checking if form is open

Post by bknight »

Code: Select all

Private Sub Command0_Click()
    If Not CurrentProject.AllForms("frmEnterTransactions").IsLoaded Then
        DoCmd.OpenForm ("frmEnterTransactions"), View:=acFormDS
    Else
        Forms("frmEnterTransactions").Refresh
        Forms("frmEnterTransactions").SetFocus
    End If
    If Not CurrentProject.AllForms("frmOpenPositionsFirst").IsLoaded Then
        DoCmd.OpenForm ("frmOpenPositionsFirst"), View:=acFormDS
    Else
        Forms("frmOpenPositionsFirst").Refresh
    End If
    If Not CurrentProject.AllForms("frmDailyprof").IsLoaded Then
        DoCmd.OpenForm ("frmDailyprof"), View:=acFormDS
    Else
        Forms("frmDailyprof").Refresh
        Forms("frmDailyprof").SetFocus
    End If
    If Not CurrentProject.AllForms("frmWeekprof").IsLoaded Then
        DoCmd.OpenForm ("frmWeekprof"), View:=acFormDS
    Else
        Forms("frmWeekprof").Refresh
        Forms("frmWeekprof").SetFocus
    End If
    If Not CurrentProject.AllForms("frmMonthprof").IsLoaded Then
        DoCmd.OpenForm ("frmMonthprof"), View:=acFormDS
    Else
        Forms("frmMonthprof").Refresh
        Forms("frmMonthprof").SetFocus
    End If
    If Not CurrentProject.AllForms("frmDividends").IsLoaded Then
        DoCmd.OpenForm ("frmDividends"), View:=acFormDS
    Else
        Forms("frmDividends").Refresh
    End If
    If Not CurrentProject.AllForms("frmDivByMonth").IsLoaded Then
        DoCmd.OpenForm ("frmDivByMonth"), View:=acFormDS
    Else
        Forms("frmDivByMonth").Refresh
    End If
    Forms("frmEnterTransactions").Refresh
    Forms!frmEnterTransactions!Type.SetFocus
    
I am having difficulty with this code, its runs but the individual forms don't refresh, as I need to go and select each form and then the select the refresh icon. In addition the second to last command is not executed, the main form FrmEnterTransactions starts off being in focus but the form is sorted newest to oldest and when I had entered some records, hit the "Refresh" command button, the form did not resort the last records entered were still at the bottom of the table. The last command was executed correctly, but All the forms that were coded to be refreshed, none were. What is incorrect?

User avatar
HansV
Administrator
Posts: 80248
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Checking if form is open

Post by HansV »

Perhaps you should use Requery instead of Refresh.
Best wishes,
Hans

bknight
SilverLounger
Posts: 1705
Joined: 08 Jul 2016, 18:53

Re: Checking if form is open

Post by bknight »

Does requery force the underlying query to refresh?
I'll change it and report tomorrow.

User avatar
HansV
Administrator
Posts: 80248
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Checking if form is open

Post by HansV »

From Refresh or requery data:
The refresh process updates the existing data in a datasheet or form, and doesn’t reorder records, display new records, or remove any deleted records or records that no longer meet specified criteria.[/url]
Best wishes,
Hans

bknight
SilverLounger
Posts: 1705
Joined: 08 Jul 2016, 18:53

Re: Checking if form is open

Post by bknight »

The requery works fine, thanks.