Adding a filter to a command button that has a security password

G

Guest

Hi,
I have a form with locked fields. In order to select individual records for
194 countries I use a combo box. To update this form I use another form with
unlocked fields which is opened with a command button with a Macro behind it.
I also use the follwing expression to filter this Edit form:

[ctbto table 1]![country]=[Forms]![experimental_form1]![country]

This all works fine, but I wanted to add a password to the command button
which opens the Edit form to restrict users' ability to edit the form. I
found the following code which creates a password function:

Private Sub Command354_Click()
'Attached to On Click event of cmdOpenEmpForm

Dim strPasswd

strPasswd = InputBox("Enter Password", "Restricted Form")

'Check to see if there is any entry made to input box, or if
'cancel button is pressed. If no entry made then exit sub.

If strPasswd = "" Or strPasswd = Empty Then
MsgBox "No Input Provided", vbInformation, "Required Data"
Exit Sub
End If

'If correct password is entered open Edit form
'If incorrect password entered give message and exit sub

If strPasswd = "graham" Then
DoCmd.OpenForm "Edit Quick info", acNormal
Me.Filter = "country = '" & Me.Combo20 & "'"
Me.FilterOn = True

Else
MsgBox "Sorry, you do not have access to this form", _
vbOKOnly, "Important Information"
Exit Sub
End If

End Sub

My question is: what code can I add to filter the Edit form? I've found that
I have to remove the Macro hwich also removes the filter. I've tried using
"Me.Filter..." as shown above, but without success. What can I do?

Thanks much in advance.
 
S

Svetlana

Try instead of ME to put
Forms.[Edit Quick info].Filter = "country = '" & Me!Combo20 & "'"
Forms.[Edit Quick info].FilterOn = True
 
S

Svetlana

Try instead of ME to put
Forms![Edit Quick info].Filter = "country = '" & Me!Combo20 & "'"
Forms![Edit Quick info].FilterOn = True
 
G

Guest

Hi again,
I've put my form with the edit button as a subform in my database. When I
click the edit button and enter my password a run-time error message (2465)
appears saying that Access can't find the field 'Combo20' referred to in my
expression and then when I click on debug opens the code window with the line

Forms![Edit Quick info].Filter = "country = '" & Me!Combo20 & "'"

highlighted in yellow, so effectively the filter that's been put in is
lost. Do you know what the problem could be here?
Thanks in advance

Kevin
 
S

Svetlana

Replace Me!Combo20 to
Forms!NameOfMainForm!Combo20 if the combo20 is to mainform
Forms!NameOfMainForm!NameOfSubform.Form!Combo20 if the combo20 is to
subform
and see if it works.
 
G

Guest

Hi once more Svetlana,

I have one more problem which if you could help me on I'd be very grateful.
And as a word of warning this is a very long post, but here goes:

I have a form on which there are text boxes displaying summarized info on
the Mission Reports or Memorandums of officers at my organization. I further
have 2 sets of 2 command buttons each, one of which opens a PDF version of
the Mission Report or memo respectively and the second which opens the folder
on the hard drive containing all mission report,etc.
These mission reports/ Memoranda are related to international organizations.
I have therefore created two linked combo boxes, the first of which displays
the list of Int. orgs. and the second the mission report or memo relating to
the organization selected in the first combo box. This is the code and the
Row Source SQL I'm using for the combo boxes:

(abrev2 is the name of the column in my table containg the short name of the
Intl Org. / nameIO is the column with the full name. the first combo box has
2 columns):

The first combo box:
Code:

Private Sub Combo99_AfterUpdate()
Me!Combo107 = Null
Me!Combo107.Requery
Me.Filter = "nameIO = '" & Me.Combo99 & "'"
Me.FilterOn = True
End Sub

Row Source SQL: SELECT DISTINCT [IOs4 Query].abrev2, [IOs4 Query.nameIO] AS
Expr1 FROM [IOs4 Query] GROUP BY [IOs4 Query].abrev2, [IOs4 Query.nameIO]
ORDER BY [IOs4 Query].abrev2, [IOs4 Query.nameIO];

and the second:
Code:
Private Sub Combo107_AfterUpdate()
Me.Filter = "MissionRepDesc = '" & Me.Combo107 & "'"
Me.FilterOn = True
End Sub

Row Source SQL: SELECT DISTINCTROW [IOs4 Query].MissionRepDesc FROM [IOs4
Query] WHERE ((([IOs4 Query].nameIO) Like [forms]![IO Mission
Reports]![combo99]));

Ok, my problem now is adding new mission reports / memos and in a way that
the update is reflected in the second combo box. My Edit form here for some
reason isn't filtering according to the Intl Org selected, even though I have
the same code as for the country-based forms:

If strPasswd = "Garfield" Then
DoCmd.OpenForm "Edit Mission Reports", acNormal
Forms![Edit Mission Reports].Filter = "abrev2 = '" & Me!Combo99 & "'"
Forms![Edit Mission Reports].FilterOn = True

Also, when I make entries in the edit form they're saved in the table, but
don't appear in the second combo box. Do you know how I could resolve this?
I'd be very, very grateful if you could help on this one too.
 
G

Guest

Hi Svetlana,

Thanks again for your help. Been sick the last few days so I'm just looking
at your sugestion now.

However, I have yet another problem (actually two, but I"ll stick to one
here) I'd like to ask you to help me with, I know this is getting to be a lot
now but I have to make a major presentation to my bosses in a few days and I
really need help here!

Ok, The database I'm working on I've actually "inherited" from someone else
and on the switchboard they put in check boxes that filtered the selection of
countries on the main form when you click the "Execute" command button.

The code behind the command button is as follows:
Private Sub ComExec_Click()
On Error GoTo Err_ComExec_Click

Dim stDocName As String
Dim stLinkCriteria As String

If Form_MainForm2.cbsig1.Value = True Then
CurrentDb.QueryDefs("Experimental_Query2").SQL = "SELECT [table 1].*
FROM [table 1] WHERE ((([table 1].annex2)=Yes Or ([table
1].annex2)=[Forms]![MainForm2]![annex2CB]) AND (([table 1].region) Like
[Forms]![MainForm2]![ComboRegion]) AND (([table 1].[IO participation]) Like
[Forms]![MainForm2]![ComboIO]) AND (([table 1].signature) Is Not Null) AND
(([table 1].ratification) Is Not Null));"
ElseIf Form_MainForm2.cbsig2.Value = True Then
CurrentDb.QueryDefs("Experimental_Query2").SQL = "SELECT [table 1].*
FROM [table 1] WHERE ((([table 1].annex2)=Yes Or ([table
1].annex2)=[Forms]![MainForm2]![annex2CB]) AND (([table 1].region) Like
[Forms]![MainForm2]![ComboRegion]) AND (([table 1].[IO participation]) Like
[Forms]![MainForm2]![ComboIO]) AND (([table 1].signature) Is Not Null) AND
(([table 1].ratification) Is Null));"
ElseIf Form_MainForm2.cbsig3.Value = True Then
CurrentDb.QueryDefs("Experimental_Query2").SQL = "SELECT [table 1].*
FROM [table 1] WHERE ((([table 1].annex2)=Yes Or ([table
1].annex2)=[Forms]![MainForm2]![annex2CB]) AND (([table 1].region) Like
[Forms]![MainForm2]![ComboRegion]) AND (([table 1].[IO participation]) Like
[Forms]![MainForm2]![ComboIO]) AND (([table 1].signature) Is Null) AND
(([table 1].ratification) Is Null));"
Else
CurrentDb.QueryDefs("Experimental_Query2").SQL = "SELECT [table 1].*
FROM [table 1] WHERE ((([table 1].annex2)=Yes Or ([table
1].annex2)=[Forms]![MainForm2]![annex2CB]) AND (([table 1].region) Like
[Forms]![MainForm2]![ComboRegion]) AND (([table 1].[IO participation]) Like
[Forms]![MainForm2]![ComboIO]));"
End If

stDocName = "experimental_form1"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_ComExec_Click:
Exit Sub

Err_ComExec_Click:
MsgBox Err.Description
Resume Exit_ComExec_Click
End Sub

Originally this worked, but then I merged this database with two other
Access ones by importing all the forms, tables and so on inot one database.
I also expanded this part of the database by adding new new columns to the
"table 1" form, which is the main underlying form for this portion of the
database. Since then I've been unable to get the check boxes to work. There
are four of them, one for "annex 2 countries"; one for "Treaty ratified"; one
for "Treaty Signed only"; and one for "Not Signed Yet". Now when none of the
boxes are selected all 197 countries in the database are displayed and there
is a text box which displays the number. When one or more of the boxes are
checked and the execute command button is clicked the countries are filtered
according to the selection made.
I've been knocking my head against the wall trying to figure out how exactly
this code works but to no avail. Would you have any idea what I've done wrong
to make it stop working? or at least what does what in this code?

Thanks again for reading another long post from me, but I deeply appreciate
any help you can provide.
 
S

Svetlana

The code changes the sql statement for Experimental_Query2 according
the choices of the user then opens experimental_form1 (that has
probably as record source the Experimental_Query2?)
What you mean it doesnt work? Do you get any error message? Have you
changed any of the original names and data types?
When you saying you merged the database with two other Access ones you
mean that you created two same ones? Do Experimental_Query2, [table 1]
exist in the current database?
You can check if and how your code is executed line by line putting
toggle breakpoints between the lines of the code.
 
G

Guest

Hi Svetlana,
Thanks for the explanation. The code works as is, but my problem is that I
need to add one more country (Montenegro) to the list of countries in the
"Table 1" table. I've done this by recopying into the table a list of
countries with Montenegro included, but the addition is not reflected in
Experimental_Query2 which has the following SQL:

SELECT [table 1].*
FROM [table 1]
WHERE ((([table 1].annex2)=Yes Or ([table
1].annex2)=[Forms]![MainForm2]![annex2CB]) AND (([table 1].region) Like
[Forms]![MainForm2]![ComboRegion]) AND (([table 1].[IO participation]) Like
[Forms]![MainForm2]![ComboIO]));

Also, in the "Combo20" combo box on the "experimental_Form1" form,
Montenegro doesn't work, ie the text boxes do not change to display the info
for that country when it is selected. As well, on this form there is a text
box which displays the number of countries according to the check boxes
ticked on the "Main Form2" before, but which shows for example 133 countries
( which have ratified) when the number should be 137. The expresion behiond
it is this: "=Count(
Code:
)".

I'll check the code by putting in the toggle breakpoints as you sugested,
but if you understand what's happeing here that'd be great.

I merged the databases by copying all the forms, tables, queries, etc of
each database into the database view page. So Experimental_Query2,  and
[table 1] exist in the current database together.

Thank you so much again for your time and help! Really appreciate it!
 
S

Svetlana

Kevin, open your query in design view and check the property MaxRecords
and see if there is a limited number of returning records. If yes then
delete it and leave the property box clear. Same for your combobox. Go
into row source property which opens the sql statement in design view
and check MaxRecords property.

I can't think anything else right now but i suggest you to make a new
post with your problem so someone else to help you further on this.
 

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