Reference text field from form in a query

cjmitton

Registered User.
Local time
Today, 19:08
Joined
Mar 25, 2011
Messages
70
I have a query I for mailmerging, Its working great but now I need to add the contense of a text box on the form that I'm running to add it in to the merge information. (the information is from a dlookup ran on opening the form and works fine)

I've tried adding a reference in to query its self as a custom field like this:
Code:
ref: [Forms]![frm_Matters]![txtSecRef]

but it does not get the information required?

Any ideas please.
 
I've tried to run the query and it prompts me for the field information.
When I manually enter the field details (initials in the case) it works. But when i use it in the merge function it does not work and asks to access the database. An error I've had before when the query has an issue.
 
That's because the form frm_Matters must be open before you run the query.
 
When i run the query as part of the merge, the form is active and has the relevant data in the fields required.
 
I use the query as part of a mailmerge to word. It ran fine and merged the required letter in word. But as soon as I added the reference to the text field it stopped working.
 
Let me see the code line where it fails. If the line is not where you wrote the sql, add that too.
 
Thats part of the problem. When i run it the only error I get is when word does the merge. If I run the query from the editor it runs when i give it the data. I don't know how to run the query while the form has live data in it to see if it references correctly or not. I hope I'm making that clear! My terminology could be off a bit....
 
Oh, wait. You are talking about the built-in Merge to Word functionality?
 
Yep! I'm running the code from access to produce a mailmerge in word. As part of the code it uses my query to pick up all the fields for the merge and use usings form information (active matter id and client id) to select the correct records to merge. It's just a matter of adding in the initials from this text box To the merge, hence the reference to the text box in the query.
 
I know what you mean, it doesn't like references to a control but I can't remember the resolution. I think if you create a function instead of the textbox, it should work.

So:
Code:
Public Function GetSecRef()
    GetSecRef = Forms!frm_Matters!txtSecRef
End Function
Then put GetSecRef() in the criteria. Make sure to put the parentheses and ensure that the function is in a global Module.
 
Sorry, parenthesis? My terminology is not that good. If i need to use the function on different forms for different queries I guess I just make them unique for each time as it references the form.
 
The brackets.

Use just that one function and reference different textboxes using an IF...ELSE IF...END IF block or a SELECT CASE block.
 
I'll use the same text box reference its just the form that would change. Sorry to be a complete pain could you give an example of how I'd do that. Thanks
 
Learn something each day....

Sorry to be a complete pain, could you give me an example of how I'd do the function with different forms to reference to as the text box would be the same on the different forms.

Thanks
 
Thanks, I'll give it a try as its something I've never done before.
 

Users who are viewing this thread

Back
Top Bottom