shortcut for buttons?

Crash1hd

Registered CyberGeek
Local time
Today, 09:38
Joined
Jan 11, 2004
Messages
143
I use the same buttons in almost all of my forms none of them change is there a way of only having to write the code once and reference it on all of them so that if I change it! the all change

here is the code that is looped

Code:
Private Sub Previous_Record_Click() 'Button
On Error GoTo Err_Previous_Record_Click

    DoCmd.GoToRecord , , acPrevious

Exit_Previous_Record_Click:
    Exit Sub

Err_Previous_Record_Click:
    MsgBox Err.Description
    Resume Exit_Previous_Record_Click
    
End Sub
Private Sub Next_Record_Click() 'Button
On Error GoTo Err_Next_Record_Click

    DoCmd.GoToRecord , , acNext

Exit_Next_Record_Click:
    Exit Sub

Err_Next_Record_Click:
    MsgBox Err.Description
    Resume Exit_Next_Record_Click
    
End Sub
Private Sub New_Record_Click() 'Button
On Error GoTo Err_New_Record_Click

    DoCmd.GoToRecord , , acNewRec

Exit_New_Record_Click:
    Exit Sub

Err_New_Record_Click:
    MsgBox Err.Description
    Resume Exit_New_Record_Click
    
End Sub
Private Sub DeleteRecord_DblClick(Cancel As Integer) 'Button
    On Error GoTo Err_DeleteRecord_Click
    DoCmd.RunCommand acCmdDeleteRecord

Exit_DeleteRecord_Click:
    Exit Sub

Err_DeleteRecord_Click:
    MsgBox Err.Description
    Resume Exit_DeleteRecord_Click
    
End Sub
Private Sub Print_Preview_Click() 'Button
On Error GoTo Err_Print_Preview_Click

    Dim stDocName As String
    Dim stLinkCriteria As String
    
    stDocName = "Critical_Criteria_Report"
    
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.OpenReport stDocName, acPreview, , "[Critical_Criteria.tblInitials] = """ & Me.tblInitials & """ And [tblCritical_Criteria_ID] = " & tblCritical_Criteria_ID

Exit_Print_Preview_Click:
    Exit Sub

Err_Print_Preview_Click:
    MsgBox Err.Description
    Resume Exit_Print_Preview_Click
    
End Sub
Private Sub Email_Critical_Criteria_Report_Click()
On Error GoTo Err_Email_Critical_Criteria_Report_Click

    If [Forms]![Critical_Criteria]![tblEmp_First_Name] <> "" Then
        DoCmd.OpenForm "CC_Email", , , , , , Me.Name
    Else
    Dim Msg, Style, Title, Response
    Msg = Me.tblInitials & " report does not contain a First Name!" & _
        Chr(13) & Chr(13) & "Continue?"
    Style = vbExclamation + vbYesNo
    Title = "Error!"
    Response = MsgBox(Msg, Style, Title)
        If Response = vbYes Then    ' User chose Yes.
            DoCmd.OpenForm "CC_Email", , , , , , Me.Name
        Else    ' User chose No.
            
        End If
    End If

Exit_Email_Critical_Criteria_Report_Click:
    Exit Sub

Err_Email_Critical_Criteria_Report_Click:
    MsgBox Err.Description
    Resume Exit_Email_Critical_Criteria_Report_Click

End Sub
Private Sub Save_Record_Click() 'Button
On Error GoTo Err_Save_Record_Click

    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_Save_Record_Click:
    Exit Sub

Err_Save_Record_Click:
    MsgBox Err.Description
    Resume Exit_Save_Record_Click
    
End Sub
Private Sub CloseForm_Click() 'Button
    On Error GoTo Err_CloseForm_Click
    DoCmd.Close

Exit_CloseForm_Click:
    Exit Sub

Err_CloseForm_Click:
    MsgBox Err.Description
    Resume Exit_CloseForm_Click
    
End Sub

Of course the only part that would probably change would be the form name in this example its Critical_Criteria in another it would be Inbound ext...
 
Last edited:
Ok so what you are saying is to create a module and reference to the module everytime I want to use that perticular button??

but how do I make code form generic like this one

Code:
Private Sub Email_[COLOR=Red]Critical_Criteria[/COLOR]_Report_Click()
On Error GoTo Err_Email_[COLOR=Red]Critical_Criteria[/COLOR]_Report_Click

    If [Forms]![[COLOR=Red]Critical_Criteria[/COLOR]]![tblEmp_First_Name] <> "" Then
        DoCmd.OpenForm "CC_Email", , , , , , Me.Name
    Else
    Dim Msg, Style, Title, Response
    Msg = Me.tblInitials & " report does not contain a First Name!" & _
        Chr(13) & Chr(13) & "Continue?"
    Style = vbExclamation + vbYesNo
    Title = "Error!"
    Response = MsgBox(Msg, Style, Title)
        If Response = vbYes Then    ' User chose Yes.
            DoCmd.OpenForm "CC_Email", , , , , , Me.Name
        Else    ' User chose No.
            
        End If
    End If

Exit_Email_[COLOR=Red]Critical_Criteria[/COLOR]_Report_Click:
    Exit Sub

Err_Email_[COLOR=Red]Critical_Criteria[/COLOR]_Report_Click:
    MsgBox Err.Description
    Resume Exit_Email_[COLOR=Red]Critical_Criteria[/COLOR]_Report_Click

End Sub
 
It does help! ;) However I just need more guided advise lol :D
 
I would recommend leaving the code under the click event of this button. In the long run it will easier to manage.

Dave
 
well I was hoping cause I use the same button in 5 forms or more and every time I change 1 thing I have to change it 5 times which is why I was hoping I could create a module to do the same and only have to change it once and all would be reflected
 

Users who are viewing this thread

Back
Top Bottom