4
4charity
Hi. I am fairly new to using VBA in Access and need some help with getting
the syntax and order of this correct.
I have a Function that creates a MailMerge in Word.
It selects data from a query in Access.
It prompts for the user to enter the VendorInvoiceNumber.
I want it to return a Message, if the VendorInvoiceNumber is not valid (does
not exist in the underlying table.)
This is what I have. The 'Prompt with incorrect VendorID is the part thats
not working.
Function MergeIt()
strVendorId = InputBox(Prompt:="Enter the desired Vendor Invoice Number .", _
Title:="ENTER VIN", Default:="")
If strVendorId = vbNullString Then
MsgBox "You have to enter something"
Else
Dim objWord As Word.Document
Set objWord = GetObject("C:\Komar\testletter2.doc", "Word.Document")
' Make Word visible.
objWord.Application.Visible = True
' Set the mail merge data source as the DTD database.
objWord.MailMerge.OpenDataSource _
Name:="C:\Komar\DTD.mdb", _
LinkToSource:=True, _
Connection:="QUERY _reportquery", _
SQLStatement:="SELECT * FROM [_reportquery] WHERE VendorInvoiceNumber = '"
& strVendorId & "'"
' Prompt with incorrect VendorID
If VendorInvoiceNumber = vbNullString Then
MsgBox "This is not a valid Vendor Invoice Number"
Else
'"
' Execute the mail merge.
objWord.MailMerge.Execute
'close the orginal file
objWord.Close wdDoNotSaveChanges
End If
End If
End Function
Thanks in advance for help on this.
the syntax and order of this correct.
I have a Function that creates a MailMerge in Word.
It selects data from a query in Access.
It prompts for the user to enter the VendorInvoiceNumber.
I want it to return a Message, if the VendorInvoiceNumber is not valid (does
not exist in the underlying table.)
This is what I have. The 'Prompt with incorrect VendorID is the part thats
not working.
Function MergeIt()
strVendorId = InputBox(Prompt:="Enter the desired Vendor Invoice Number .", _
Title:="ENTER VIN", Default:="")
If strVendorId = vbNullString Then
MsgBox "You have to enter something"
Else
Dim objWord As Word.Document
Set objWord = GetObject("C:\Komar\testletter2.doc", "Word.Document")
' Make Word visible.
objWord.Application.Visible = True
' Set the mail merge data source as the DTD database.
objWord.MailMerge.OpenDataSource _
Name:="C:\Komar\DTD.mdb", _
LinkToSource:=True, _
Connection:="QUERY _reportquery", _
SQLStatement:="SELECT * FROM [_reportquery] WHERE VendorInvoiceNumber = '"
& strVendorId & "'"
' Prompt with incorrect VendorID
If VendorInvoiceNumber = vbNullString Then
MsgBox "This is not a valid Vendor Invoice Number"
Else
'"
' Execute the mail merge.
objWord.MailMerge.Execute
'close the orginal file
objWord.Close wdDoNotSaveChanges
End If
End If
End Function
Thanks in advance for help on this.