VBA--click a button to open a TABLE and only show conditioned records ?

M

Martin

In a FORM, I want to create a button, when I click this button, the Access
will open TABLE "Customer Inf", with the condition that field "customer
name"="Martin Lee". Also, I want,below the selected "Martin Lee" records, it
has a blank line for which the users to enter a new record here.

How should the VBA code should be ?


BTW, if it is a QUERY (instead of a FORM), how should the VBA code be, to
filter up "customer name"="Martin Lee"?


Thanks!


Martin
 
D

Dirk Goldgar

Martin said:
In a FORM, I want to create a button, when I click this button, the
Access will open TABLE "Customer Inf", with the condition that field
"customer name"="Martin Lee". Also, I want,below the selected "Martin
Lee" records, it has a blank line for which the users to enter a new
record here.

How should the VBA code should be ?

I don't recommend letting your users work directly in the tables. For
one thing, you have much less control. Why not create a continuous form
(or even a datasheet form) bound to this table; then you can open it
and supply the where-condition at the same time via the DoCmd.OpenForm
method.

If you insist on opening the table directly, there's no way to filter it
in the same statement that opens it. However, you can filter it in the
very next statement, like this:

Dim strCustomerWanted As String

strCustomerWanted = "Martin Lee"

Docmd.OpenTable "Customer Inf"
DoCmd.ApplyFilter , "[customer name]=" & _
Chr(34) & strCustomerWanted & Chr(34)

Note the comma that immediately follows the name of the ApplyFilter
method. That's critical, because it distinguishes the optional
"FilterName" argument (which we're not using) from the "WhereCondition"
argument (which we are using).
BTW, if it is a QUERY (instead of a FORM), how should the VBA code
be, to filter up "customer name"="Martin Lee"?

I'm not sure I understand this question.
 
M

Martin

Thanks, and I have several question base on your answer. New posted.
Dirk Goldgar said:
Martin said:
In a FORM, I want to create a button, when I click this button, the
Access will open TABLE "Customer Inf", with the condition that field
"customer name"="Martin Lee". Also, I want,below the selected "Martin
Lee" records, it has a blank line for which the users to enter a new
record here.

How should the VBA code should be ?

I don't recommend letting your users work directly in the tables. For
one thing, you have much less control. Why not create a continuous form
(or even a datasheet form) bound to this table; then you can open it
and supply the where-condition at the same time via the DoCmd.OpenForm
method.

If you insist on opening the table directly, there's no way to filter it
in the same statement that opens it. However, you can filter it in the
very next statement, like this:

Dim strCustomerWanted As String

strCustomerWanted = "Martin Lee"

Docmd.OpenTable "Customer Inf"
DoCmd.ApplyFilter , "[customer name]=" & _
Chr(34) & strCustomerWanted & Chr(34)

Note the comma that immediately follows the name of the ApplyFilter
method. That's critical, because it distinguishes the optional
"FilterName" argument (which we're not using) from the "WhereCondition"
argument (which we are using).
BTW, if it is a QUERY (instead of a FORM), how should the VBA code
be, to filter up "customer name"="Martin Lee"?

I'm not sure I understand this question.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top