| | | Tom Wickerath |  |
| Posted: Sat Sep 20, 2008 9:11 pm Post subject: RE: Command Button |  |
| |  | |
First, you will be doing yourself, and anyone who may take over maintenance of your database in the future, a big favor by using more reasonable names for your command buttons instead of "Command126" and "Command132". Also, this part of your code should have caused a compile error:
Private Sub Command126_Click()
Private Sub Command132_Click()
since you are starting a new event procedure, without having an End Sub to terminate the Private Sub Command126_Click() procedure. Just curious....do you have Option Explicit shown as the second line of code at the top of your module? If not, you should add this line of code. Here's why:
Always Use Option Explicit LINK
I recommend using this instead: DoCmd.Close acForm, Me.Name
This way, the intended object will be the one that gets closed every time, instead of the off-chance of a different object that might have focus at the wrong time.
In addition, if you are using a bound form (ie. a form that includes a recordset), then you should force a save prior to closing the form, to avoid losing data. For example:
Private Sub cmdClose_Click() On Error GoTo ProcError
If Me.Dirty = True Then '<---Use this only for bound forms Me.Dirty = False End If DoCmd.Close acForm, Me.Name
ExitProc: Exit Sub ProcError: MsgBox "Error " & Err.Number & ": " & Err.Description, _ vbCritical, "Error in procedure cmdClose_Click..." Resume ExitProc End Sub
More information here on this topic:
Losing data when you close a form LINK
To answer your original question, you can use the optional OpenArgs argument to notify the form you are opening who it's 'daddy' is. An example is shown for opening a form named "frmIssues":
Function OpenRecordForEditing() As Byte On Error GoTo ProcError
'Pass the name of the calling form in as an OpenArg. Dim strTemp As String strTemp = Me.Name glngDefaultOpenMode = acNormal
If Not IsNull([pkIssueID]) Then DoCmd.OpenForm "frmIssues", OpenArgs:=strTemp, DataMode:=acFormEdit, _ WhereCondition:="[pkIssueID] = " & [pkIssueID]
End If OpenRecordForEditing = 1
ExitProc: Exit Function ProcError: MsgBox "Error " & Err.Number & ": " & Err.Description, _ vbCritical, "Error in OpenRecordForEditing event procedure..." OpenRecordForEditing = 0 Resume ExitProc End Function
Now, when we go to close frmIssues, and re-open (or requery) the calling form, we can use something like this, in the class module associated with frmIssues:
Private Sub cmdClose_Click() On Error GoTo ProcError
If Me.Dirty = True Then Me.Dirty = False End If DoCmd.Close acForm, Me.Name
ExitProc: Exit Sub ProcError: MsgBox "Error " & Err.Number & ": " & Err.Description, _ vbCritical, "Error in procedure cmdClose_Click..." Resume ExitProc End Sub
Private Sub Form_Close() On Error GoTo ProcError
If Not IsNull(Me.OpenArgs) Then DoCmd.OpenForm Me.OpenArgs 'or 'Eval ("[Forms]!" & Me.OpenArgs & ".[Form].[ControlName].Requery") End If
ExitProc: Exit Sub ProcError: MsgBox "Error " & Err.Number & ": " & Err.Description, _ vbCritical, "Error in Form_Close event procedure..." Resume ExitProc End Sub
where [ControlName] is the name of the control (Alerts?) that you wish to requery.
Tom Wickerath Microsoft Access MVP LINK LINK __________________________________________
"Pass-the-reality" wrote:
| Quote: | I have two forms (frmSpecialistView and frmSpecialistViewEmail) that contains a command button to open up another form (CallLog). I added a command button to form Call Log that closes the form. You will see in the code below that I then added [Forms]![frmSpecialistView]![Alerts].Requery. This works great if I go through frmSpecialistView to access frmCallLog. What can I add to the code to say requery [Alerts] on frm SpecialistView or frmSpecialistViewEmail, depending which form I went through.
Private Sub Command126_Click()
Private Sub Command132_Click() On Error GoTo Err_Command132_Click
DoCmd.Close [Forms]![frmSpecialistView]![Alerts].Requery
Exit_Command132_Click: Exit Sub
Err_Command132_Click: MsgBox Err.Description Resume Exit_Command132_Click
End Sub |
|
|