Create an error message from a search button (1 Viewer)

macftm

Registered User.
Local time
Today, 00:30
Joined
Aug 31, 2011
Messages
31
Hello,
I am new to MS Access in terms of using the Macro functions. I am trying to create a search button which will return a set of forms related to a particular person which I have created a table for.

At present I have got this to work by linking a query (and its form) to the textboxes in search form and using the open form Macro on the button located on a search form. However, if I enter the details of a person who does not already exist in the person details table, the form still appears but without a related record.

What I would like is an error message to appear telling the user that they have entered their search details incorrectly instead of bring up a blank form?


Sorry to be a pain and probably very vague request.


Macftm
 

vbaInet

AWF VIP
Local time
Today, 00:30
Joined
Jan 22, 2010
Messages
26,374
What I would like is an error message to appear telling the user that they have entered their search details incorrectly instead of bring up a blank form?
What really constitutes an incorrect entry? By entering details of someone that doesn't exist doesn't mean that the entry was incorrect. Having a message box pop up everytime will be a bit of a nuissance.

What you can do instead is to show the number of matched records in a textbox. That will be more intuitive.
 

macftm

Registered User.
Local time
Today, 00:30
Joined
Aug 31, 2011
Messages
31
Thanks for your suggestion. I want to use the search form and button as a verification of the user as well as providing the user with the relevant forms to complete.
 

vbaInet

AWF VIP
Local time
Today, 00:30
Joined
Jan 22, 2010
Messages
26,374
Please elaborate. You still want to display a msgbox when no records are found?
 

macftm

Registered User.
Local time
Today, 00:30
Joined
Aug 31, 2011
Messages
31
Sorry to be a pain. Essentially what I am looking to do on the inital form is for the user to enter their details (Surname, dept and password) and to return the forms related to that individual so they can fill in the appropriate sections. At the same time I want to use this inital form to validate themself by running these details through a query.

If the person details are within the datebase table, the forms related to that person are return once the search button is click.

However, is any of the details are incorrect, the set of forms are still appearing but not link to any users details.

Instead of bring up these blank forms, I want a warning message to appear telling the user that they have enter their details incorrectly and that they must re-eneter their details.

Thanks for you help and interest in attempting to resolve my problem.

Macftm
 

vbaInet

AWF VIP
Local time
Today, 00:30
Joined
Jan 22, 2010
Messages
26,374
How many search boxes do you have and what are the names?
 

macftm

Registered User.
Local time
Today, 00:30
Joined
Aug 31, 2011
Messages
31
3 search boxes (all unbound) named as: Surname; Registration Class and Password
 

vbaInet

AWF VIP
Local time
Today, 00:30
Joined
Jan 22, 2010
Messages
26,374
Can you change those names to txtSurname, txtRegClass and txtPassword.

Let's see the code you use to perform the search. The one behind the command button.
 

macftm

Registered User.
Local time
Today, 00:30
Joined
Aug 31, 2011
Messages
31
I've changed the names as you stated. The first textbox was actually Username not Surname. Therefore, these textboxes are now called: txtUserName, txtRegClass and txtPassword.

I have a query linked to these boxes which have the following expressions in the criteria section of the relevant fields:
User Name: [Forms]![F_PupilDetailsSearch]![txtUserName]
Registration Class: [Forms]![F_PupilDetailsSearch]![txtRegClass]
Password: [Forms]![F_PupilDetailsSearch]![txtPassword]

The button connected to the search form has an 'on click' event action: Open Form which holds a Navigation form containing all the relevant forms the user must complete.

As mentioned in earlier posts, if the details entered into the search form match up with those in the query, the users details appear in the Navigation form. However, if any of these details are incorrect, a blank form appears. I do not want a blank form to appear, but an error message stating that some of their search details are incorrect.

Thanks for all your help so far vbaInet.

Macftm
 

macftm

Registered User.
Local time
Today, 00:30
Joined
Aug 31, 2011
Messages
31
I do not have any code attached to the button, just the macro event 'on click' Open Navigation form
 

vbaInet

AWF VIP
Local time
Today, 00:30
Joined
Jan 22, 2010
Messages
26,374
Can you convert that macro to code instead? What I'm about to advise is done in code.

You simply need to add this code behind the Click event (i.e. Event Procedure) of the button.
Code:
If Nz(DCount("*", "[COLOR=Red]Query Name[/COLOR]"), 0) = 0 Then
    Msgbox "[COLOR=Red]Your warning message here[/COLOR]"
Else
    DoCmd.OpenForm "[COLOR=Red]Form Name[/COLOR]"
End If
Amend the bits in red.
 

macftm

Registered User.
Local time
Today, 00:30
Joined
Aug 31, 2011
Messages
31
Hi vbaInet, thanks once again for your prompt reply. I've tried your code with no success. Can I send you a test sample of the database so you can have a look at what I am doing wrong? My test database just has the pupil information table, Query & form query and search form within it.
 

macftm

Registered User.
Local time
Today, 00:30
Joined
Aug 31, 2011
Messages
31
Cheers

The details for the search textboxes can be found in T_Pupil_Information
 

Attachments

  • Database2.accdb
    1,012 KB · Views: 93

Taruz

Registered User.
Local time
Today, 00:30
Joined
Apr 10, 2009
Messages
168
Hi..

vbaInet's implementation of the proposal ..:


Code:
Private Sub Command10_Click()
If Nz(DCount("*", "Q_PSE_Pupil_Search"), 0) = 0 Then
    MsgBox "Your warning message here"
Else
    DoCmd.OpenForm "F_PSE_Pupil_Profile"
End If
End Sub


this is a different solution..:

use the "F_PSE_Pupil_Profile" form of

Code:
Private Sub Form_Open(Cancel As Integer)
If RecordsetClone.RecordCount = 0 Then
MsgBox "Your warning message here"
DoCmd.Close
End If
End Sub
 

vbaInet

AWF VIP
Local time
Today, 00:30
Joined
Jan 22, 2010
Messages
26,374
use the "F_PSE_Pupil_Profile" form of

Code:
Private Sub Form_Open(Cancel As Integer)
If RecordsetClone.RecordCount = 0 Then
MsgBox "Your warning message here"
DoCmd.Close
End If
End Sub
... and remember to trap for the error 2501
 

macftm

Registered User.
Local time
Today, 00:30
Joined
Aug 31, 2011
Messages
31
Cheers guys, can I just asks where to put this code??
 

macftm

Registered User.
Local time
Today, 00:30
Joined
Aug 31, 2011
Messages
31
Sorry, I have sorted it now. Thanks for all your help. Can you recommend and good books or webpages to learn about macros and VBA from a beginners level?? Thanks once again for all your help and support.
 

Users who are viewing this thread

Top Bottom