Paul,
An update, have spent some time getting at least used to the syntax of VBA, the code which is running on the button click is shown below.
I've been playing about with adding break points as you mentioned, and uncommenting out lines which I'm not sure why they were commented.
As an example, the line "DoCmd.OutputTo acOutputQuery, "delimited_proforma_qry", acFormatTXT, l_dir & l_filename" was commented out - when I removed the ' and put a breakpoint, I got the ominous eggtimer suggesting something was happening, but no error message or completion/email being sent.
I've checked that delimited_proforma_qry does actually exist.
The error I am getting now is "FILE CREATION UNSUCCESSFUL - Microsoft Office Access can't save the output data to the file you've selected" ... any ideas? The break is currently after :
.Subject = "RCA Stage1 Profroma File"
.Attachments.Add l_dir & l_abv & l_filename
.Send
End With
Final edit, hopefully, I figured out the problem - there was a typo in the directory where the files were trying to save! Hooray!!!!!
Code:
Private Sub save_but_Click()
On Error GoTo Err_save_but_Click
Dim l_result As String
Dim l_dir As String
Dim l_file_abv As String
Dim l_filename As String
Dim l_email_addr As String
Dim strEmail As String
Dim strMsg As String
Dim olook As Object
Dim oMail As Object
Dim rstSettings As ADODB.Recordset
Dim fldEach As ADODB.Field
Dim rstEmailaddr As ADODB.Recordset
Dim fldEach1 As ADODB.Field
DoCmd.SetWarnings False
Set rstSettings = New ADODB.Recordset
rstSettings.Open "SELECT dir & abv from settings", _
Application.CodeProject.Connection, _
adOpenStatic, adLockOptimistic, adCmdText
For Each fldEach In rstSettings.Fields
l_dir_value = fldEach.Value
Next
rstSettings.Close
Set rstSettings = Nothing
' get email addr
Set rstEmailaddr = New ADODB.Recordset
rstEmailaddr.Open "SELECT email_addr from settings", _
Application.CodeProject.Connection, _
adOpenStatic, adLockOptimistic, adCmdText
For Each fldEach1 In rstEmailaddr.Fields
l_email_addr = fldEach1.Value
Next
rstEmailaddr.Close
Set rstEmailaddr = Nothing
l_dir = l_dir_value & "1" ' Hospital abv with file type 1 = first file 2 = second file
'l_filename = id & nhs_no & Replace(FormatDateTime(specimen_date, vbGeneralDate), "/", "") & ".CSV"
l_filename = nhs_no & Replace(FormatDateTime(date_of_awareness, vbGeneralDate), "/", "") & ".CSV"
If IsNull(nhs_no) Or IsNull(organism_code) Or IsNull(patient_loc_id) Or IsNull(dob) Or IsNull(date_of_awareness) Or IsNull(sex) Or IsNull(age_of_admission) Or IsNull(date_of_admission) Or IsNull(pre_post_48_hours) Then
MsgBox "Cannot Save/Create file whilst record has no values.", vbCritical, "Proforma Creation Error"
Else
created_on = date
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.OutputTo acOutputQuery, "delimited_proforma_qry", acFormatTXT, l_dir & l_filename
DoCmd.TransferText acExportDelim, "Stage 1 Export Specification", "delimited_proforma_qry", l_dir & l_filename
DoCmd.SetWarnings True
MsgBox "Record saved and file successfully created, email is being generated.", vbInformation, "File Creation Message"
' Email Settings
Set olook = CreateObject("Outlook.Application")
Set oMail = olook.createitem(0)
With oMail
.to = l_email_addr '"ruth.cheesley@suffolkpct.nhs.uk"
'.cc = "another@email"
.body = "Attached is the RCA Stage1 Proforma File"
.Subject = "RCA Stage1 Profroma File"
.Attachments.Add l_dir & l_abv & l_filename
.Send
End With
Set oMail = Nothing
Set olook = Nothing
prt_but.Enabled = True
email_but.Enabled = True
exit_but.SetFocus
save_but.Enabled = False
prt_but.SetFocus
nhs_no.Enabled = False
hospital_no.Enabled = False
organism_code.Enabled = False
'sex = False
dob.Enabled = False
diagnosis.Enabled = False
date_of_awareness.Enabled = False
date_of_admission.Enabled = False
repeat_case.Enabled = False
previous_admission.Enabled = False
gp_name.Enabled = False
'patient_loc_id.Enabled = False
patient_free_txt_loc.Enabled = False
augmented_care.Enabled = False
notes.Enabled = False
report_cause_of_death.Enabled = False
'part_death.Enabled = False
current_antib_txt.Enabled = False
previous_antib_txt.Enabled = False
mrsa_length_of_stay.Enabled = False
cdiff_length_of_stay.Enabled = False
current_antib_use.Enabled = False
previous_antib_use.Enabled = False
ppi_therapy.Enabled = False
surgery.Enabled = False
colonised.Enabled = False
immuno_suppressed.Enabled = False
indwelling_device.Enabled = False
no_surgical_wound.Enabled = False
End If
Exit_save_but_Click:
Exit Sub
Err_save_but_Click:
MsgBox "FILE CREATION UNSUCCESSFUL - " & Err.Description
Resume Exit_save_but_Click
End Sub