Save form, Close, Print Report and goto another form all in one button ?

anthonyevans

Registered User.
Local time
Today, 08:05
Joined
Mar 15, 2001
Messages
29
Dear All, I know this is a bit long winded but it might be the best way to express the problem. I need a button that will save the current data, close the form, print a report and goto to another form. I have the code for all three in seperate buttons but need to combine them into one.

Here is the code for the button going to the other form taking some data with it. It is within this code that i wish to add the other fuctions :

Private Sub accidentreport_Click()
On Error GoTo Err_accidentreport_Click

Dim stDocName As String
Dim stLinkCriteria As String
Dim c, d As Date
Dim a, b, f, g, h As String
Dim e As Date

a = Me![First_Name]
b = Me![Surname]
c = Me![Date_Of_Birth]
d = Me![Date_Of_Incident]
e = Me![Time_Of_Incident]
f = Me![Company_Or_Production_Name]
g = Me![Location_Of_The_Incident]
h = Me![Condition_Reason_For_Attendance]
stDocName = "Accident Report Form"

stLinkCriteria = "[Serial Number]=" & Me![Serial Number]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Forms![Accident Report Form]![First_Name] = a
Forms![Accident Report Form]![Surname] = b
Forms![Accident Report Form]![Date_Of_Birth] = c
Forms![Accident Report Form]![Date_Of_Incident] = d
Forms![Accident Report Form]![Time_Of_Incident] = e
Forms![Accident Report Form]![Company_Or_Production_Name] = f
Forms![Accident Report Form]![Location_Of_The_Incident] = g
Forms![Accident Report Form]![Condition_Reason_For_Attendance] = h
Forms![Accident Report Form].Refresh

Exit_accidentreport_Click:
Exit Sub

Err_accidentreport_Click:
MsgBox Err.Description
Resume Exit_accidentreport_Click

End Sub


Here is the code for the printing of the report :

Private Sub printreport_Click()
On Error GoTo Err_printreport_Click

Dim stDocName As String

stDocName = "Treatment Report"
DoCmd.OpenReport stDocName, acNormal, , "[Serial_Number] = [forms]![TREATMENT FORM]![Serial_Number]"

Exit_printreport_Click:
Exit Sub

Err_printreport_Click:
MsgBox Err.Description
Resume Exit_printreport_Click

End Sub


And finally the code for closing the form :

Private Sub closerecord_Click()
On Error GoTo Err_closerecord_Click


DoCmd.Close

Exit_closerecord_Click:
Exit Sub

Err_closerecord_Click:
MsgBox Err.Description
Resume Exit_closerecord_Click

End Sub


Now I'd like to put the two above button codes into the first chunk of code.

If anyone has the time to advise I would be most grateful.

Anthony
 
I suggest that you transfer the function into a public module and that you pass the form as a parameter. Then the code would look as follows with beginning simplification for maintenance reasons:
1.) no unnecessary vars like a,b,c...
2.) no repetitions in using WITH


Public sub MultiSub(f as form)
Dim stDocName As String
Dim stLinkCriteria As String

' Delete these declarations:
a =
b = f![Surname]
c = f![Date_Of_Birth]
d = f![Date_Of_Incident]
e = f![Time_Of_Incident]
f = f![Company_Or_Production_Name]
g = Me![Location_Of_The_Incident]
h = Me![Condition_Reason_For_Attendance]
stDocName = "Accident Report Form"

stLinkCriteria = "[Serial Number]=" & Me![Serial Number]
DoCmd.OpenForm stDocName, , , stLinkCriteria

With Forms![Accident Report Form]

![First_Name] =f![First_Name]
' replace the vars with the form controls like shown above:
![Surname] = b
![Date_Of_Birth] = c
Forms![Accident Report Form]![Date_Of_Incident] = d
Forms![Accident Report Form]![Time_Of_Incident] = e
Forms![Accident Report Form]![Company_Or_Production_Name] = f
Forms![Accident Report Form]![Location_Of_The_Incident] = g
Forms![Accident Report Form]![Condition_Reason_For_Attendance] = h
Forms![Accident Report Form].Refresh

end with

Dim stDocName As String

stDocName = "Treatment Report"
DoCmd.OpenReport stDocName, acNormal, , "[Serial_Number] = [forms]![TREATMENT FORM]![Serial_Number]"

docmd.close acForm,f.name

end sub
 
Thanks ElsVanMiert for your help but i'm afraid it's just confusing me more as i'm only a beginner. Can I ask instead how and where I insert the code to close the form that this code is on i.e. TREATMENT FORM



Private Sub accidentreport_Click()
On Error GoTo Err_accidentreport_Click

Dim stDocName As String
Dim stLinkCriteria As String
Dim c, d As Date
Dim a, b, f, g, h As String
Dim e As Date

a = Me![First_Name]
b = Me![Surname]
c = Me![Date_Of_Birth]
d = Me![Date_Of_Incident]
e = Me![Time_Of_Incident]
f = Me![Company_Or_Production_Name]
g = Me![Location_Of_The_Incident]
h = Me![Condition_Reason_For_Attendance]
stDocName = "Accident Report Form"

stLinkCriteria = "[Serial Number]=" & Me![Serial Number]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Forms![Accident Report Form]![First_Name] = a
Forms![Accident Report Form]![Surname] = b
Forms![Accident Report Form]![Date_Of_Birth] = c
Forms![Accident Report Form]![Date_Of_Incident] = d
Forms![Accident Report Form]![Time_Of_Incident] = e
Forms![Accident Report Form]![Company_Or_Production_Name] = f
Forms![Accident Report Form]![Location_Of_The_Incident] = g
Forms![Accident Report Form]![Condition_Reason_For_Attendance] = h
Forms![Accident Report Form].Refresh

stDocName = "Treatment Report"
DoCmd.OpenReport stDocName, acNormal, , "[Serial_Number] = [forms]![TREATMENT FORM]![Serial_Number]"

Exit_accidentreport_Click:
Exit Sub

Err_accidentreport_Click:
MsgBox Err.Description
Resume Exit_accidentreport_Click



End Sub

Thankyou Again,

Anthony
 

Users who are viewing this thread

Back
Top Bottom