Which event to chech?

  • Thread starter Thread starter Janez Banez
  • Start date Start date
J

Janez Banez

Hi

The datasource of the starting form of my access program
is a query. The query selects the field user and the
properties of this user from the table of all potential
users (and properties). To select the current user I use
the value of function fGetUserName() as criteria (see the
description of the function on:
http://www.mvps.org/access/api/api0066.htm ).

Everythig is OK when the user is in the table of all
users. If he is not then the query is empty an after
opening the form I get empty grey form.

How to avoid such occurrence? I would like to check some
event at the opening of the form to send the user a
message he is not allowed to run the program and to close
the empty form. I tried but I haven't find anything
usefull till now.

Any answer will be welcome

Janez
 
Use the Open event of the form to check if the user is in the table; if not,
cancel the event and tell the user your message. Note that you'll need to
trap for Error 2501 in the form that opens this new form because canceling
the event will give an error re: "You canceled the previous operation".

You can check for user in table by the use of DLookup function.
 
Janez,

Paste the following code in the form's On Open event:

Set rs = Me.RecordsetClone
chk = rs.RecordCount
rs.Close
Set rs = Nothing
If chk = 0 Then
vPrompt = "Sorry! You are not an authorized user."
vTitle = "Security Problem"
MsgBox vPrompt, vbCritical, vTitle
DoCmd.Close acForm, Me.Name, acSaveNo
End If

Whjat it does is, it checks the number of records in the form's
recordsource, and if no record is found, it informs the user and closes
the form.

HTH,
Nikos
 
Thanks for the help. Searching the Google I found in the
meantime another solution - the code:

Private Sub Form_Open(Cancel As Integer)
If Me.RecordsetClone.RecordCount = 0 Then
MsgBox(...)
DoCmd.close
End If
End Sub

Janez
 
You do not need the DoCmd.Close step; replace it with Cancel = True.

Much safer to do that.
 
Back
Top