Hi everyone,
I've tried to google this problem and not found anything that has helped so far so thought I'd bring the problem here. I'll try and explain it as clearly as I can!
I'm currently updating an old fundraising database for a small charity and trying to automate some of the functions. I've got a form that contains all of the data of all the contacts, and then buttons that run various code to write different letter types. I've now added in a new button that opens a pop up form to select a type of letter that then needs to populate a new record on the subform (containing details of all the letters written to a particular contact) of the original form (that contains all the contact details). The code that I have used for it is copied from a similar button that does the same thing, but without the pop up form. So, easy peasy I thought, I just need to change the form/subform references...except that seems to have made it go wrong! The syntax SEEMS to be right, as it does focus on the subform, and it does populate something, BUT it populates the top record in the subform and doesn't create a new record first. What have I done wrong? Have I made a typo? Messed up the syntax? Done something strange with a string? I'll leave the code below and see if any of you can come up with anything!
This is the code that works:
Code:
Dim strApType As String
strApType = InputBox("What is the appeal type?", "Please Enter Appeal Type", "Operating")
If strApType = vbNullString Then
MsgBox "Letter cancelled.", , "Cancelled"
Exit Sub
End If
With Me![LettersSentSub]
.SetFocus
.Form![Appeal Type].SetFocus
DoCmd.GoToRecord , , acNewRec
.Form![Appeal Type] = strApType
.Form![Date Letter Sent] = Date
End With
'Run Mail Merge Function: (This part all works fine)
MailMergeLetters
End Sub
This is the code that doesn't:
Code:
Dim strApType As String
Dim strCurrentForm As String
Dim strControlName As String
strApType = InputBox("What is the appeal type?", "Please Enter Appeal Type", "Operating")
If strApType = vbNullString Then
MsgBox "Letter cancelled.", , "Cancelled"
Exit Sub
End If
If CurrentProject.AllForms("frmTrustCompanyDetailsWithSearch").IsLoaded Then
strCurrentForm = "frmTrustCompanyDetailsWithSearch"
ElseIf CurrentProject.AllForms("frmPrevGiversTrustCompanyDetailsWithSearch").IsLoaded Then
strCurrentForm = "frmPrevGiversTrustCompanyDetailsWithSearch"
End If
strControlName = "LettersSentSub"
With Forms(strCurrentForm).Controls(strControlName)
.SetFocus
.Form![Appeal Type].SetFocus
DoCmd.GoToRecord , , acNewRec '''DoCmd.RunCommand acCmdNewRecord
'''(I tried this ^^ as an alternative and it came up
'''with an error that said it was cancelled
'''- maybe this is a clue?)
.Form![Appeal Type] = strApType
.Form![Date Letter Sent] = Date
End With
'Run Mail Merge Function: (This part all works fine)
MailMergeLettersSpecialistAppeal