GaryCanuck
Technical User
Hi All,
I have a somewhat messy chunk of code to run a mailmerge for 2 documents. I've attempted to set up and qualify all of my variables but it appears that I haven't succeeded. The code will run as desired the first time (on click event of a button). And then error the second time which is usually from unqualified variables. The error says the remote server in unavailable. I'm using access 2007 and word 2007. If anyone can spy the coding error I would be grateful. My guess is that it has something to do with the recordset because the error comes after the second query runs. Any help always appreciated!
I have a somewhat messy chunk of code to run a mailmerge for 2 documents. I've attempted to set up and qualify all of my variables but it appears that I haven't succeeded. The code will run as desired the first time (on click event of a button). And then error the second time which is usually from unqualified variables. The error says the remote server in unavailable. I'm using access 2007 and word 2007. If anyone can spy the coding error I would be grateful. My guess is that it has something to do with the recordset because the error comes after the second query runs. Any help always appreciated!
Code:
On Error GoTo Err_Command16_Click
DoCmd.SetWarnings False
DoCmd.OpenQuery "Discharge"
DoCmd.SetWarnings True
Dim db As DAO.Database
Set db = OpenDatabase("\\Taxerver\taxervice\Access\TaxSale_be.mdb")
District = InputBox("Enter District", "Name")
db.Execute "UPDATE Discharge SET District = '" & District & "' "
dischargeno = InputBox("Enter Discharge of Instrument No.", "Name")
numthree = InputBox("Who is Applicant? Donna (d) or Muni (m)?")
If numthree = "d" Then
db.Execute "UPDATE Discharge SET numthree2 = 'I am a designated officer and have authority to bind same pursuant to by-law Registered as No. ' "
db.Execute "UPDATE Discharge SET Signer = 'Don' "
db.Execute "UPDATE Discharge SET Title = 'Sale Manager' "
End If
If numthree = "m" Then
db.Execute "UPDATE Discharge SET numthree3 = '' "
db.Execute "UPDATE Discharge SET numthree1 = 'I am an employee of the Corporation and have authority to bind same' "
End If
fp = InputBox("Full (f) or Partial (p) Discharge?")
If fp = "f" Then
db.Execute "UPDATE Discharge SET Dischargex1 = 'X' "
db.Execute "UPDATE Discharge SET Otherx1 = 'X' "
db.Execute "UPDATE Discharge SET fulldisno = '" & dischargeno & "' "
db.Execute "UPDATE Discharge SET specify1 = 'Notice of Sale' "
End If
If fp = "p" Then
db.Execute "UPDATE Discharge SET Dischargex2 = 'X' "
db.Execute "UPDATE Discharge SET Otherx2 = 'X' "
db.Execute "UPDATE Discharge SET partdisno = '" & dischargeno & "' "
db.Execute "UPDATE Discharge SET specify2 = 'Notice of Tax Sale' "
db.Execute "UPDATE Discharge SET seesched = 'A' "
End If
Dim objWord As Word.Document
Set objWord = GetObject("\\Taxerver\taxervice\Templates\Discharge.doc", "Word.Document")
objWord.Application.Visible = True
If fp = "f" Then
objWord.Application.Run "ScratchMacro"
End If
If fp = "p" Then
objWord.Application.Run "ScratchMacro2"
End If
objWord.Application.Visible = False
objWord.MailMerge.Execute
objWord.Close False
DoCmd.SetWarnings False
DoCmd.OpenQuery "Discharge Schedule"
DoCmd.SetWarnings True
Dim dbs As Database
Dim Lrs As DAO.Recordset
Set dbs = OpenDatabase("\\Taxerver\taxervice\Access\TaxSale_be.mdb")
Set Lrs = db.OpenRecordset("Discharge Schedule", dbOpenDynaset)
If Lrs.RecordCount = 0 Then
MsgBox ("There are no records for the schedule.")
Word.Application.Quit SaveChanges:=wdDoNotSaveChanges
Else
Dim bWord As Word.Document
Set bWord = GetObject("\\Taxerver\taxervice\Templates\Discharge Schedule.doc", "Word.Document")
bWord.Application.Visible = True
bWord.MailMerge.Execute
bWord.Close False
End If
Lrs.Close
Set bWord = Nothing
Set Lrs = Nothing
Set dbs = Nothing
Set objWord = Nothing
Set db = Nothing
Exit_Command16_Click:
Exit Sub
Err_Command16_Click:
MsgBox Err.Description
Resume Exit_Command16_Click