Computer Forum Forum Index
Register  FAQ Profile Log in to check your private messages Log in Register 

Add a query based on a variable

 
Post new topic   Reply to topic    Computer Forum Forum Index -> word vba customization
View previous topic :: View next topic  
Author Message
Al
Guest





PostPosted: Wed Sep 19, 2007 4:03 pm    Post subject: Add a query based on a variable Reply with quote

Hi everyone...

I have a word document that runs some VB code which prompts the
user to select an item from a list. Once an item is chosen, a separate
text field is automatically filled in. This works fine. I have also
added a query on the same page that gets info from an access database.
This works fine as well.

What I want to do is have one of the parameters of my query be the
value in the text field that was automatically filled in. That way, if
a user selected a different item on the list, the query would auto-
update. Currently, the query must be reconstructed manually - and it's
not something a regular user knows how to do...

The relevant portion of the field code in which the database info
resides is:

`Source` FROM `MY_TABLE` WHERE ((`Item` = 'my_item')) ORDER BY
`Price`

so instead of 'my_item' I want to say 'the value stored in the text
field'

How do I do that?

Thanks for the help,

Al
Back to top
Google
Sponsor





PostPosted: Wed Sep 19, 2007 4:03 pm    Post subject: Advertisement

Back to top
Doug Robbins - Word MVP
Guest





PostPosted: Thu Sep 20, 2007 6:22 am    Post subject: Re: Add a query based on a variable Reply with quote

It is not clear what type of fields you are using. If it is formfields as
used in a protected document, you can use the .Result property of the field
to get the value that is entered into it.

If the item that was filled in was actually a Docvariable field that was
displaying a value that was saved as a Document Variable, you could have
your query operate on the value of that Document Variable.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"Al" <bernala@gmail.com> wrote in message
news:1190217819.531844.178730@19g2000hsx.googlegroups.com...
Quote:
Hi everyone...

I have a word document that runs some VB code which prompts the
user to select an item from a list. Once an item is chosen, a separate
text field is automatically filled in. This works fine. I have also
added a query on the same page that gets info from an access database.
This works fine as well.

What I want to do is have one of the parameters of my query be the
value in the text field that was automatically filled in. That way, if
a user selected a different item on the list, the query would auto-
update. Currently, the query must be reconstructed manually - and it's
not something a regular user knows how to do...

The relevant portion of the field code in which the database info
resides is:

`Source` FROM `MY_TABLE` WHERE ((`Item` = 'my_item')) ORDER BY
`Price`

so instead of 'my_item' I want to say 'the value stored in the text
field'

How do I do that?

Thanks for the help,

Al
Back to top
Al
Guest





PostPosted: Thu Sep 20, 2007 2:53 pm    Post subject: Re: Add a query based on a variable Reply with quote

Doug,

Thanks for the reply. I will try to clarify a bit. As I mentioned, my
word document displays a form with a list of products. The user then
selects a product, clicks OK, and the form closes. The macro then
retrieves the product code from an excel file, and places the value in
a text form field.

Aside from that, I have an independent database form field that
contains a query. This query links to an access database and lists all
customers who use a certain product code. The product code is
specified as a criteria in the query. Here is the field code for the
database field:


Selection.Range.InsertDatabase Format:=16, Style:=191,
LinkToSource:=True, _
Connection:= _
"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data
Source=c:\Customers.mdb; Mode=Read;Extended Properties="""";Jet
OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet
OLEDB:Databas" _
, SQLStatement:= _
"SELECT `Customer Name` FROM `List of Customers` WHERE
((`Product_ID` = '123456')) ORDER BY `Price`" _
& "", PasswordDocument:="", PasswordTemplate:="",
WritePasswordDocument _
:="", WritePasswordTemplate:="", DataSource:= _
"c:\Customers.mdb", From:=-1, To:=-1, IncludeFields:=True


So in order to make sure the table displays the correct list of
customers, I have to manually change Product_ID` = '123456' to the
product code displayed in my text field. My question is, how can I
automate this? Since the user has already specified a product at the
beginning, and the corresponding product code is already visible in
the text form field, how do I get the query to read the value in the
text form field and update?

Thanks again.

Al
Back to top
Tony Strazzeri
Guest





PostPosted: Fri Sep 21, 2007 1:29 am    Post subject: Re: Add a query based on a variable Reply with quote

On Sep 21, 12:53 am, Al <bern...@gmail.com> wrote:
Quote:
Doug,

Thanks for the reply. I will try to clarify a bit. As I mentioned, my
word document displays a form with a list of products. The user then
selects a product, clicks OK, and the form closes. The macro then
retrieves the product code from an excel file, and places the value in
a text form field.

Aside from that, I have an independent database form field that
contains a query. This query links to an access database and lists all
customers who use a certain product code. The product code is
specified as a criteria in the query. Here is the field code for the
database field:

Selection.Range.InsertDatabase Format:=16, Style:=191,
LinkToSource:=True, _
Connection:= _
"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data
Source=c:\Customers.mdb; Mode=Read;Extended Properties="""";Jet
OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet
OLEDB:Databas" _
, SQLStatement:= _
"SELECT `Customer Name` FROM `List of Customers` WHERE
((`Product_ID` = '123456')) ORDER BY `Price`" _
& "", PasswordDocument:="", PasswordTemplate:="",
WritePasswordDocument _
:="", WritePasswordTemplate:="", DataSource:= _
"c:\Customers.mdb", From:=-1, To:=-1, IncludeFields:=True

So in order to make sure the table displays the correct list of
customers, I have to manually change Product_ID` = '123456' to the
product code displayed in my text field. My question is, how can I
automate this? Since the user has already specified a product at the
beginning, and the corresponding product code is already visible in
the text form field, how do I get the query to read the value in the
text form field and update?

Thanks again.

Al

I'm not sure if the ProductID value is in a Userform Textbox control
or a document text formfield or evan as a variable.
Assuming you can put it into a variable you can modify your SQL query
as shown below. If the ID is not in a variable use textbox
control's .Text property, ot the formfields .Result property instead
of the variable I have used ("ProductIDvar").

If the value is in a Userform control you will not be able to access
it once you close the form. The solution is to hide the form (using
yourUserformName.hide). You can then Unload the userform when you are
finished with it.
BTW if you have moved the userform TextBox value to a variable in the
form you still need to hide the form to gat at the var until you
unload it.


WHERE"((`Product_ID` = '" & ProductIDvar & "')) ORDER BY

Note" there is a single quote before the double quote before the equal
sign and after the second ampersand.


Hope this helps.

Cheers
TonyS.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Computer Forum Forum Index -> word vba customization All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Computer Forum