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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check to see where form was opened from

Status
Not open for further replies.

jazminecat

Programmer
Jun 2, 2003
289
US
Hi, I have a form that can be opened two different ways. It opens from the switchboard in add new record mode, and it opens from another form to edit a record. How do I tell where the form was opened from, so that when the user is done, they can go back to where they started? If they open the form by clicking edit on the main form, i want them to save, and go back to the main form, to that record, and refresh the data. Any help in pointing me in the right direction is much appreciated. Thanks y'all.
 
Why not opening it as modal (acDialog) ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
hm, i'm not familiar with that I don't think. Basically, i have one form that people use to browse the records, and because they can't be trusted not to change things, edits are disabled there. Then, if they're on a record and need to edit it, they click edit, and that opens that record in the data entry form. That same data entry form is used by the person who uses the db the most to enter new records. Currently, when she clicks 'done' it prompts her to save, and takes her back to the switchboard. but, if she has gotten there from clicking edit on the browsing form, I think it would be better to take her back to the browsing form, to the edited record, after refreshing the info. From what I can see in the help file, modal just means they have to finish what they're doing before they can go to a different form. That's not really a problem as all the other forms are closed when one is open.
 
In the Click event procedure of the edit button:
DoCmd.OpenForm "name of form", , ,"criteria stuff", , acDialog
Me.Requery

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
oh, and here's the code to open it from the main form:

Code:
Private Sub cmdEdit_Click()
On Error GoTo Err_cmdEdit_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "BetterBidEntryForm"
    
    stLinkCriteria = "[ID]=" & Me![ID]
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cmdEdit_Click:
    Exit Sub

Err_cmdEdit_Click:
    MsgBox Err.Description
    Resume Exit_cmdEdit_Click
    
End Sub
[\code]

and this is the code when it's opened from the switchboard:

[code]
       ' Open a form in Add mode.
        Case conCmdOpenFormAdd
            DoCmd.OpenForm rs![Argument], , , , acAdd
 
Private Sub cmdEdit_Click()
stLinkCriteria = "[ID]=" & Me![ID]
DoCmd.OpenForm "BetterBidEntryForm", , , "[ID]=" & Me![ID], , acDialog
Me.Requery
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
hm, we.. requerying the main form when she clicks edit doesn't seem to do what i need. Maybe I'm not being very clear.

I have Form1, which is for browsing the data.
the Edit button is on this form with this code:
Code:
Private Sub cmdEdit_Click()
On Error GoTo Err_cmdEdit_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "BetterBidEntryForm"
    
    stLinkCriteria = "[ID]=" & Me![ID]
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cmdEdit_Click:
    Exit Sub

Err_cmdEdit_Click:
    MsgBox Err.Description
    Resume Exit_cmdEdit_Click
    
End Sub

That opens the BetterBidEntry form, to the record she was viewing, so she can edit it. This works so far. On the BetterBidEntry form is a button that says "done". Here is the code for that button:

Code:
Private Sub cmdExit_Click()
On Error GoTo Err_cmdExit_Click

'prompt user to save changes
'check for changes

If Me.Dirty Then
'prompt user - if they don't want to save, don't save, and go to the switchboard
    If MsgBox("Do you want to close without saving?", vbYesNo, "Close Form?") = vbYes Then
    Me.Undo
    DoCmd.Close , , acSaveNo
    DoCmd.OpenForm "Switchboard"
    Else
    Exit Sub
    End If
 'If there are no changes made to the record, just go to the switchboard
Else
DoCmd.Close
DoCmd.OpenForm "Switchboard"
End If

Exit_cmdExit_Click:
    Exit Sub

Err_cmdExit_Click:
    MsgBox Err.Description
    Resume Exit_cmdExit_Click
    
End Sub

So, this works, and of course sends her to the switchboard. But now what I want is to look at where she came from, and act on that information.

a) she comes from the switchboard, the form opens in add mode, and she adds her records, saves them, and clicks done, and goes back to the switchboard.

or

b) she came from clicking 'edit' on Form1, so when she's done editing the record, and clicks 'done', go back to browsing on Form1, to the same record she just edited, but show the changes.

clear as mud? :)

thanks y'all!
 
Private Sub cmdEdit_Click()
stLinkCriteria = "[ID]=" & Me![ID]
DoCmd.OpenForm "BetterBidEntryForm", , , "[ID]=" & Me![ID], , acDialog
Me.Refresh
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Maybe this approach will help. Since a form's module is a Class module you can add a property to it. So:

Public CallingForm As String

Private Sub Form_Close()
If Not CallingForm = "" Then
DoCmd.OpenForm CallingForm
End If
End Sub

Now, if you want the above form to return somewhere you need to set the callingForm property when you open the form. So from the outside.

Private Sub Command0_Click()
DoCmd.Close
DoCmd.OpenForm "frmCallingForm"
Forms("frmCallingForm").CallingForm = "frmTestCalling"
End Sub
 
How's about:

Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Form_Unload
Dim frmMain as Form

'Get Form opened previously.
Set frmMain = Forms(Forms.Count - 1)
If frmMain.Name="Browsing Form" Then
'Requery "Browsing Form" and go to edited Record.
frmMain.Requery
With frmMain.RecordsetClone
.FindFirst "[Primary Key]=" & Me![Primary Key]
If Not .NoMatch Then
frmMain.Bookmark = .Bookmark
End If
End With
End If

Exit_Form_Unload:
Exit Sub

Err_Form_Unload:
MsgBox Name & "_Unload Error: " & Err.Number & ": " & Err.Description
Resume Exit_Form_Unload
End Sub
 
Nice trick
Forms(Forms.Count - 1)
Makes total sense, but it never occured to me that the order of how you load and unload forms defines the order of the collection. Learn something new everyday.
 
Hi MajP, yeah it's just another collection - last one in first one out! :) This method comes with a health warning though. If you opened multiple instances of the same Form, say frmMultiple, (using a class module to create seperate instances in the same Access session - think creating a card file on screen). And then opened a data entry Form, frmDataEntry, from one of those instances, the Forms Collection would refer to the first instance of frmMultiple and closing frmDataEntry would give unexpected results.

However, for normal situations this is satisfyingly self-referencial and robust...
 
*very* cool! I will give this a shot today and report back to y'all. Thanks so much! This is a very exciting possibility.
 
Ok, I must be doing someting wrong. when she clicks the exit button after editing the record, it still takes her to the switchboard. Here's what i have so far:

for the exit button itself:
Code:
Private Sub cmdExit_Click()
On Error GoTo Err_cmdExit_Click


'check for changes

If Me.Dirty Then
'prompt user - if they don't want to save, don't save, and go to the switchboard
    If MsgBox("Do you want to close without saving?", vbYesNo, "Close Form?") = vbYes Then
    Me.Undo
    DoCmd.Close , , acSaveNo
    DoCmd.OpenForm "Switchboard"
    Else
    Exit Sub
    End If
 'If there are no changes made to the record, just go to the switchboard
Else
DoCmd.Close
DoCmd.OpenForm "Switchboard"
End If

Exit_cmdExit_Click:
    Exit Sub

Err_cmdExit_Click:
    MsgBox Err.Description
    Resume Exit_cmdExit_Click
    
End Sub

and now this in the unload event of the form:
Code:
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Form_Unload
Dim frmMain As Form

'get form opened previously
    Set frmMain = Forms(Forms.Count - 1)
    If frmMain.Name = "BidScheduleForm" Then
'requery bidschedule form and go to edited record
    frmMain.Requery
    With frmMain.RecordsetClone
        .FindFirst "[ID]=" & Me![ID]
        If Not .NoMatch Then
            frmMain.Bookmark = .Bookmark
        End If
    End With
    End If

Exit_Form_Unload:
    Exit Sub

Err_Form_Unload:
    MsgBox Name & "_UnloadError: " & Err.Number & ":" & Err.Description
    Resume Exit_Form_Unload
End Sub

do they appear to be contradicting each other, or is it just that when she clicks exit, it's not telling the program to go back to the main form (BidScheduleForm), but to the switchboard. so it's still not coded right, is it?
 
Simplest approach is to add a textbox to the form that is being opened. Whichever form calls for the form to be opened sends a value to the textbox, which identifies the opeing form, say a 1 or a zero. You can then set up a button on the opened form attached to a procedure with an IF statement which reads the textbox. If the textbox displays a 1, it opens one form and if it displays a zero, it opens the other form.
 
Not suprising really - DoCmd.OpenForm "Switchboard" runs whichever choice the user makes. Also, if Form "Switchboard" is already open it's not necessary to use this function. Try this:

Private Sub cmdExit_Click()
On Error GoTo Err_cmdExit_Click

'check for changes

If Me.Dirty Then
'prompt user - if they don't want to save, don't save, and go to the switchboard
If MsgBox("Do you want to close without saving?", vbYesNo, "Close Form?") = vbYes Then
Me.Undo
End If
End If
DoCmd.Close Me.Name

Exit_cmdExit_Click:
Exit Sub

Err_cmdExit_Click:
MsgBox Err.Description
Resume Exit_cmdExit_Click
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top