473,385 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Pass a Parameter to a Query in Access Front-End

I thought would be a super simple project, but I was wrong. I need
Help!!!!

I am using the Word.Application.Mailmerge.OpenDataSource to try and
send a Dynamic Query to my stated Document.Open File. For the life of
me I cannot fiqure out how to pass a parameter from this code to the
"Proposal" Query in the Access Front End. If I do not add the param
variable then everthing works fine except that it pulls all of the
matching records instead of the specific one based on the proposal
number. I am open to any Suggestions.
SEE CODE BELOW:
Dim oMainDoc As Word.Document
Dim oSel As Word.Selection
Dim sDBPath As String
Dim strSQL As String
Dim param As Integer
Set oApp = CreateObject("Word.Application")

Set Db = CurrentDb
Set oMainDoc = oApp.Documents.Open("c:\MARK\proposal.doc")
With oMainDoc.MailMerge

.MainDocumentType = wdFormLetters

param = Me![ProposalNumber]
strSQL = "Proposal" & param
.OpenDataSource Name:=Db, SQLStatement:=strSQL
End With
With oMainDoc
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute Pause:=False
.Close False
End With
oApp.Visible = True
Nov 12 '05 #1
3 2323
"Mark Horton" <mh*****@texaspension.com> wrote in message
news:dc**************************@posting.google.c om...
I thought would be a super simple project, but I was wrong. I need
Help!!!!

I am using the Word.Application.Mailmerge.OpenDataSource to try and
send a Dynamic Query to my stated Document.Open File. For the life of
me I cannot fiqure out how to pass a parameter from this code to the
"Proposal" Query in the Access Front End. If I do not add the param
variable then everthing works fine except that it pulls all of the
matching records instead of the specific one based on the proposal
number. I am open to any Suggestions.
SEE CODE BELOW:
Dim oMainDoc As Word.Document
Dim oSel As Word.Selection
Dim sDBPath As String
Dim strSQL As String
Dim param As Integer
Set oApp = CreateObject("Word.Application")

Set Db = CurrentDb
Set oMainDoc = oApp.Documents.Open("c:\MARK\proposal.doc")
With oMainDoc.MailMerge

.MainDocumentType = wdFormLetters

param = Me![ProposalNumber]
strSQL = "Proposal" & param
.OpenDataSource Name:=Db, SQLStatement:=strSQL
End With
With oMainDoc
.MailMerge.Destination = wdSendToNewDocument
.MailMerge.Execute Pause:=False
.Close False
End With
oApp.Visible = True


There are a number of things I'm puzzled with here. Firstly, it looks like
you have elected for late-binding with the line:
Set oApp = CreateObject("Word.Application")
But a few lines down, it looks like you have placed a reference to the Word
library and using its constants:
.MailMerge.Destination = wdSendToNewDocument

Are you aware of the pros and cons of early / late binding and have you made
a conscious choice?
Secondly, I'm not sure what this is:
strSQL = "Proposal" & param

Is "Proposal" the name of a query? Using a naming convention where you
called it "qryProposal" would help others understand if that was the case.
One possible way would be to create a query "qrySelectedProposal" which you
re-write at run-time to get the result you want (see function below), but at
a guess, your query is supposed to only return one record anyway and so
perhaps an actual mail-merge is not the best way. You could do other things
in code beside mailmerge.
Public Function DefineQuery(strQueryName As String, strSQL As String) As
Boolean

On Error GoTo Err_Handler

Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef

Set dbs = CurrentDb

Set qdf = dbs.QueryDefs(strQueryName)

qdf.SQL = strSQL

DefineQuery = True

Exit_Handler:
If Not qdf Is Nothing Then Set qdf = Nothing
If Not dbs Is Nothing Then Set dbs = Nothing
Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function
Nov 12 '05 #2

Thanks for the Help. I fixed the late binding mistake, it was not a
intentional coding choice. I am sorry for my ignorance, but you said:
"You could do other things
in code beside mailmerge." What other things would be better to pass a
query to a Word Document besides using mailmerge?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #3
"Mark Horton" <mh*****@texaspension.com> wrote in message
news:40*********************@news.frii.net...

Thanks for the Help. I fixed the late binding mistake, it was not a
intentional coding choice. I am sorry for my ignorance, but you said:
"You could do other things
in code beside mailmerge." What other things would be better to pass a
query to a Word Document besides using mailmerge?


Hi Mark

Well if it's only one document, you could have open a standard Word document
or template (no mailmerge data) and replace certain key terms with
information from the database.
One advantage is that your users need zero mailmerge skills to alter or
create new documents. In the example below, the code replaces the
text[[ProposalNumber]] with the value from the current record.

strPropNo = Cstr(Nz(Me!ProposalNumber, 0))
Set WdDoc = WdApp.Documents.Add(strTemplatePath)
Set WdRange = WdApp.ActiveDocument.Content
WdRange.Find.Execute FindText:="[[ProposalNumber]]", ReplaceWith:=strPropNo,
Replace:=wdReplaceAll
Fletcher
Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Belinda | last post by:
Hello All I have the following test.asp page which needs one parameter querystr but my querystr is a very long string value. When I send a long value the query string is getting truncated after...
5
by: Don Seckler | last post by:
I have an update query that runs when a report closes. I have several reports that will need to run the update query with diferent criteria. I'd like to simply make the criteria change in the...
3
by: Paradigm | last post by:
I want to create a pass through query in Access to get data from a MYSQL table. I need to do this using code so that certain aspects of the query can be changed. When I look at the SQL version of...
2
by: rg | last post by:
Hi all, I have query about passing a parameter from form to a query. My situation is as follows: I have a query that pulls up some data from a table, however there is a condition that must...
1
by: | last post by:
Hi, I've defined an ObjectDataSource and a parameterized DataSet. I would like it if I could pass the parameter value that describes the query that creates the DataSet as part of a user control...
2
by: Roger | last post by:
Anyone know how to pass a parameter to a query via a report in Access 2003?
2
by: Dud Bug | last post by:
I have a query in an Access db that prompts the user for info. (e.g. a primary key representing a company) before compiling the relevant table of results. This works fine in Access but I want to use...
1
by: woody | last post by:
I have a bit of a newbie question (basically stated in the subject of the posting). If need to get some data out of a SQL Server Database. Can I use a control from a form to supply a parameter. ...
1
by: lesmccoy | last post by:
Hi I am using the openrecordet in the Ms Access to open saved queries I have in my database. I am trying to pass a parameter into the openrecordset the query has to use. For example the query...
22
by: Lewe22 | last post by:
I am creating a small Access db which performs a series of updates to a SQL database. The Access db consists of a ‘Main Form’, from which the user can run each update via a series of command...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.