URGENT! REPLY NEEDED ASAP...

K

ktfrubel

Hello,
I have a parameter query that ask for a First name and a last name. I would
like for an message to appear if no one by the names appear. I can't seem to
find anything in the discussions.

Example:
Enter First Name:
Frank
Enter Second Name
Smith

Message pops up if there is no Frank Smith in the database and Says oops
sorry try again.


Can anyone help me....I need to have t his completed by monday, and I am at
my whits ends.


Thanks
Tara
 
S

Steve Schapel

Tara,

Just a hint for the future... If you really think it is important for
your message to appear in more than one newsgroup (in practice this is
seldom necessary), it is much preferable to address the same message to
both newsgroups simultaneously (crosspost), rather than send a separate
copy to each newsgroup (multi-post). Thanks.
 
W

William Hindman

....the reason you can't find anything in the discussions is because its a
pretty odd thing to do and would require more than a bit of code ...do you
mind telling us what you are trying to accomplish with this approach?

....for instance, if you are just trying to look up a contact name in a
table, there are far better ways to do it ...but if it is indeed as another
suggested, a "homework" type assignment where you really have to do it this
way, that's another story.

William Hindman
 
K

Ken Sheridan

Tara:

Make the parameters references to a control on an unbound dialogue form, e.g.
Forms!YourtForm!txtFirstname and Forms!YourForm!txtLastName.

Open the query, or a report or form based on the query from a button on the
form. In the code check to see if there is a matching record in the table,
using the DLookup function, and if this returns a Null then pop up a message
box, else open the query/report/form.

Please take note of what Steve said about multiple postings. And please
don't use demanding upper case subject titles for the post. Its likely
merely to antagonise people rather than elicit a helpful response.

Ken Sheridan
Stafford, England
 
K

Ken Sheridan

Tara:

Another thought:

If the query is the RecordSource of a report you can use the reports NoData
event procedure to pop up the message box. Set the return value of the
procedure's Cancel argument to True to stop the report opening.

Ken Sheridan
Stafford, England
 
K

ktfrubel

To all who replied...Thank you for the suggestions. No this is not a
homework assignment. I didn't mean to post twice....I was looking for
anwsers under the macro area of the discussions group. When I posted my
question it went there. I didn't see it on the general discussions group,
and thought that it did not work. I am sorry if I annoyed anyone with my
post, I was just in need of an answer as soon as possible. Now that I have
been rightfully scolded, I guess I won't be able to achieve what I am trying
to do, becasue when it comes to writing in code, I am quite ignorant. I am
trying to learn, but to no avail. I know how to work in access directly with
controls and queries and such. I thought that there could have been an
easier way to get this to work.
Thanks again for all of the help, I will make sure not to post in all caps
again.
Tara
 
K

ktfrubel

William,
I have a "button" on a form that allows a user to search the database for
entries all ready there. But if they are not there, I would like for the
message to appear, letting them know. That is all...really
 
K

Ken Sheridan

Tara:

Don't give up hope. Its not nearly as difficult as you might think. First
thing to do is create the form and put two text boxes on it. Name them
txtFirstName and txtLastName say. To do this select each and change their
Name properties in the properties sheet from the name Access gives them.
Save the form with a suitable name, frmNameDialogue for instance. Open the
form and enter a first and last name, remembering to press the return key
after the last one so the control updates.

With the form still open, open your query in design view and in place of the
[Enter first name] and [Enter last Name] parameters in the criteria row put
refernces to the form's controls:

Forms!frmNameDialogue!txtFirstName
and
Forms!frmNameDialogue!txtLastName

Save the query. If you now run the query (with the form still open) you
should see just the row(s) for the name you entered.

If everything is working OK close the query and go back to the form and
open it in design view. Add a button to the form. Select the button and in
its properties sheet select the On Click event property. Click on the
'build' button; that's the one to the right with 3 dots. Then select 'Code
Builder' in the next dialogue. When you exit the dialogue the VBA window
will open at the button's event procedure. The first and last lines will be
in place and you add the new code between them. Lets assume you want to open
the query directly, so the code would be as follows. This time I've included
some validation and customized the message so it includes the not found
name. I've commented the code so you can see what it's doing. Comments are
an important part of programming. Hopefully the indenting of the code will
have been preserved when you raed it, but that depends on what you are using
to read it. Indenting shows the flow of the code, e.g everyting between an
If and an End If will be indented.

Here's the code:

' declare variables to hold messages and criteria string
Dim strMessage As String
Dim strCriteria As String

' first make sure both names have been entered
If Not IsNull(Me.txtFirstName) And Not IsNull(Me.txtLastname) Then
' find out if name exists in table.
' build criteria string; the followig two lines
' are actually a single line of code; the underscrore
' character at the end of the first line is a continuation character
strCriteria = "[FirstName] = """ & Me.txtFirstName & _
""" AND [LastName] = """ & Me.txtLastName & """"
' look up any row(s) which match the criteria
If Not IsNull(DLookup("[LastName]", "[YourTableName]", strCriteria) Then
' name exists so open query
DoCmd.OpenQuery "YourQueryName"
Else
' build message to inform user that name not found
strMessage = Me.txtFirstName & " " & Me.txtLastName & " not found."
' pop up message box
MsgBox strMessage, vbInformation, "Warning"
End If
Else
' one or both names missing so inform user
' build message to inform user that name not found
strMessage = "Both names must be entered."
' pop up message box
MsgBox strMessage, vbExclamation, "Invalid Operation"
End If

If you want to print a report based on the query just open the report
instead by calling the OpenReport method rather than the OpenQuery method:

DoCmd.OpenReport "YourReportName"

If you want to preview the report:

DoCmd.OpenReport "YourReportName", View:=acViewPreview

If you want to open a form based on the query call the OpenForm method:

DoCmd.OpenForm "YourFormName"

You are doubtless feeling by now that all that looks horrendously
complicated, but if you copy and paste the above code into the event
procedure and make sure that your table, field and query/form/report names
are correctly substituted for the names I've used it should work. Unless
I've made some silly minor error(s) that is! If so than get back to me;
debugging is another important part of programming. Hopefully I've not made
any errors in the code but I wouldn't rule it out. When writing code its not
unusual to make small errors, just a missing comma or quote can be enough,
but weeding out the bugs is part of the process.

Do try and understand what the code is doing rather than just copying it in.
If you understand how it works its easier to spot any things which might
either raise a runtime error, or just not give the results you expect.

Good luck,

Ken Sheridan
Stafford, England
 

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