Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving state of open forms

Status
Not open for further replies.

joatmofn

Technical User
Jan 12, 2003
72
US
When access closes, I need to save the state (form name and screen location) of any forms that were open at the time that access was closed. I'll save the info in a table. The next time the database is opened, the forms that were last used and opened need to be opened.

Any help with existing code would be greatly appreciated.

Thanks
 
I'm afraid I can't help with identifying the forms or reports that are currently open when the application closes so hopefully someone else can step in.

The following code on a form or report's open and close events will pull and push screen locations from/to a table called tblObjState with fields
objName
{text}
objLeft
{number}
objTop
{number}
objHeight
{number}
objWidth
[/b]{number}

It will open a form/report to the location and size it was when the form/report was last closed.

Code:
Private Sub Form_Close()
Dim sqlLoc As String
sqlLoc = "UPDATE tblObjState SET tblObjState.objLeft = " & Me.WindowLeft & ", tblObjState.objTop = " & Me.WindowTop & _
", tblObjState.objHeight = " & Me.WindowHeight & ", tblObjState.objWidth = " & Me.WindowWidth & " WHERE tblObjState.objName = " & """" & Me.Name & """" & ";"
DoCmd.SetWarnings False
DoCmd.RunSQL sqlLoc
DoCmd.SetWarnings True
End Sub

Private Sub Form_Open(Cancel As Integer)
If DCount("*", "tblObjState", "[objName] = '" & Me.Name & "'") = 0 Then
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO tblObjState (objName) SELECT " & """" & Me.Name & """" & ";"
DoCmd.SetWarnings True
DoCmd.Maximize
Else
Me.Move DLookup("[objLeft]", "tblObjState", "[objName] = '" & Me.Name & "'"), _
    DLookup("[objTop]", "tblObjState", "[objName] = '" & Me.Name & "'"), _
    DLookup("[objWidth]", "tblObjState", "[objName] = '" & Me.Name & "'"), _
    DLookup("[objHeight]", "tblObjState", "[objName] = '" & Me.Name & "'")
End If
End Sub


 
identifying the forms or reports that are currently open
Simply enumerate the Application.Forms and Reports collections.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
To find the forms, that are currently open,
during close, you can use the Forms collection

Dim frm as frm

For each frm in Forms
Debug.print frm.Name
Next
 
Here is an attempt at a mechanism. In Access 2000 I created a user form ([tt]frmObjectTracker[/tt]) and set it as the Startup form for the database, then I added the following Event Procedures to track the open objects.
Here are a couple of notes on the code:[ol]
[li]In my limited testing, the first form opened was the first form closed, so this approach seems to work.[/li]
[li]I am dumping the listing of open objects to a text file (code [highlight]highlighted[/highlight]), this could be converted to a table Using a variant of [navy]UnicornRainbow[/navy]'s code, or dumped to the registry uning [tt]SaveSetting()[/tt] and [tt]GetSetting()[/tt].[/li]
[li]Access 2000 DOES NOT Support [tt]WindowTop()[/tt] or [tt]WindowLeft()[/tt] but you can use API calls to get this info in earlier versions of Access.[/li]
[li]You could add a control to the form that provides the user with feedback if the process takes too long and then show/hide the form as needed.[/li]
[/ol]
Code:
Private Sub Form_Close()
Dim aoCurrent As AccessObject
[highlight]Dim intFile As Integer
intFile = FreeFile
Open "C:\OpenObject.dat" For Output As #intFile[/highlight]
For Each aoCurrent In CurrentProject.AllForms
  If aoCurrent.IsLoaded Then
    [highlight]Print #intFile,[/highlight] "Form," & aoCurrent.Name & ",Top,Left"
  End If
Next aoCurrent
For Each aoCurrent In CurrentProject.AllReports
  If aoCurrent.IsLoaded Then
    [highlight]Print #intFile,[/highlight] "Report," & aoCurrent.Name & ",Top,Left"
  End If
Next aoCurrent
[highlight]Close #intFile[/highlight]
End Sub
Code:
Private Sub Form_Open(Cancel As Integer)
On Error Resume Next
Dim intFile As Integer, intProp As Integer
Dim strObject As String, strProps() As String
[highlight]intFile = FreeFile
Open "C:\OpenObject.dat" For Input As #intFile[/highlight]
Do
  [highlight]Line Input #intFile, strObject[/highlight]
  strProps = Split(strObject, ",")
  If strProps(0) = "Form" Then
    DoCmd.OpenForm strProps(1)
  ElseIf strProps(0) = "Report" Then
    DoCmd.OpenReport strProps(1)
  End If
  [green]'This code does not capture Top & Left
  'DoCmd.MoveSize strProps(2), strProps(3)[/green]
  DoEvents
Loop Until [highlight]EOF(intFile)[/highlight]
[highlight]Close #intFile[/highlight]
End Sub

Hope this helps,
CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top