473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Search Button Code?

Hi :

I would like to ask about : how can I write the (simplist) code for search button. I'm bignner in using Access 2007 & VBA . This button will search by name in a query which contains all employees informations then if the name is valid, message box will show and disply all the related informations for the employee.
Attached Files
File Type: zip electrical equipments Co[1]..zip (106.7 KB, 1825 views)
May 11 '09 #1
22 21443
NeoPa
32,556 Expert Mod 16PB
Hi & Welcome to Bytes!

I've moved your question to the Access Questions area as it was mistakenly posted in the Insights (article) area.
May 11 '09 #2
NeoPa
32,556 Expert Mod 16PB
@GoodGirl
I'm not sure what you're asking for. It's hard to know without any information about how the data you're talking about is stored.

Basically, you'd probably want a form with a control where the operator would enter a search phrase. When entered you would search through the data and put up the message where necessary. DLookup() is probably the function to use.

Without more information I can't be much more help.
May 11 '09 #3
NeoPa

Thanks for reply and you understood what I mean (sorry to not explain it). I want a form (which I named it : start : ) which contains a search button which will search by name in q query and then the result will come as a Message Box contains the information about the employee.


How can I use DLookup() method , I want to use it with DetailedInformation query. then if the input name is valid in the query: the information will come, if not message box will show that it's not valid. how can I do this comparison?



thanks
May 12 '09 #4
NeoPa
32,556 Expert Mod 16PB
I will need to know :
  1. The name of the query ([DetailedInformation] I expect) that you're trying to search in.
  2. The name of the field within the query that you're trying to match.
  3. The name of the control on your form where the user will enter the search value.
  4. Whether the match needs to be exact, or if wildcards should be allowable.
If wildcards are allowable :
  1. Are they to be assumed (or would the operator need to enter it/them where required)?
  2. If assumed, would the search stub be for anywhere in the field (or just the start)?
May 12 '09 #5
1- The name of the query is DetailedInformation.

2- The name of the field is FirstName.

3- I don't want to put a text field: I just put a form button and name it (search) and right in it's code the when I click on it , it will give me an Input box then complete the process (the code is attached) , and after the comparison will done, I want another message box to show the results.

4- I don't worry about this side.


I just want the simplisit way for search, after I do it, I will start to learn the more complex.
May 13 '09 #6
NeoPa
32,556 Expert Mod 16PB
@GoodGirl
I would advise against this. If you already have a form, then using an inputbox is like having a dog and barking yourself. Neither is particularly easier to code mind you, but the result looks less polished.
@GoodGirl
If it's not posted then I won't see it.
@GoodGirl
I worry about it. That's why I asked for it. I don't plan to give one solution only to find that it's not right because you haven't given me enough information.

If you want my help, then I think I should be able to expect answers to all my questions.
May 13 '09 #7
This is the code
Expand|Select|Wrap|Line Numbers
  1. Private Sub searchButton_Click()
  2. Dim Inn As String
  3. Inn = InputBox("Would you enter Employee's First Name?")
  4. MsgBox Inn
  5. End Sub
when I tried to write
Expand|Select|Wrap|Line Numbers
  1. If Inn = txtFirstname Then MsgBox ("OK")
  2. Else MsgBox ("This name is not valid")
As try to make simple comparison, it shows to me an error message said (you have to put ElseIf and when I put it , it shows the line which starts with Else as red line.


3- just now I put a text field and button, so how can I do thier code to be match with each other?


2- I post the code above.

1- I'm happy because you are helpfull , but I answred to which I understood , Neoplo, I don't have that experience which you have, so please don't be angry from me.
May 13 '09 #8
ADezii
8,834 Expert 8TB
NeoPa is correct in everything he says, and it would be wise to follow his advice. If you do, however, insist on your present course of action, the code would be:
Expand|Select|Wrap|Line Numbers
  1. Private Sub SearchButtonl_Click(Cancel As Integer)
  2. Dim strInn As String
  3. strInn = InputBox$("Would you enter Employee's First Name?")
  4.  
  5. If strInn = "" Then
  6.   Exit Sub
  7. ElseIf strInn = Me![txtFirstName] Then
  8.   MsgBox "OK"
  9. Else
  10.   MsgBox "This name is not valid"
  11. End If
  12. End Sub
May 14 '09 #9
NeoPa
32,556 Expert Mod 16PB
I'm sorry if that sounded angry GoodGirl. Sometimes I'm less patient than I should be. It was simply meant to sound clear :
I need an answer for my question #4 before I can help with your first questioon.
If you have no preference, at least choose one so that an answer can make sense in that context.

Now I can see the code though, I can tell you why it doesn't like the Else line :
You have tried to put another statement on the same line (Else is a statement on its own. After Else should be a new line).

See ADezii's post for a good example of how the If ... End If should be used. There is (only) one instance where another statement can share the same line, which is when there is a simple If followed by a single statement. In this case no Else, ElseIf, or End If statements are required (or even allowed).

It is actually possible, using a special character, to join two logical lines together. This is never recommended though as it's considered very bad style. It is generally only used when you want to make your code hard to understand (a weak level of security).
May 14 '09 #10
Expand|Select|Wrap|Line Numbers
  1. Private Sub searchButton_Click()
  2. Dim Inn As String
  3. Inn = InputBox("Would you enter the Employee's First Name?")
  4. If Inn = txtFirstName Then MsgBox ("OK")
  5. End If
  6. If Inn <> txtFirstName Then MsgBox ("This name is not valid")
  7. End If
  8. End Sub
ADezii : Thanks, but your code doesn't work! (I don't know why?), I still clicking on the button after I paste the code , but it didn't work and didn't show to me any error message!

After that, I tried to write the code above, but it shows the (End If without block If) , I didn't understand what to do.
May 14 '09 #11
NeoPa
32,556 Expert Mod 16PB
@GoodGirl
Your line #5 should be an Else statement rather than an End If.
May 14 '09 #12
ChipR
1,287 Expert 1GB
If...Then can be a single line statement, or a multiple line block.

Expand|Select|Wrap|Line Numbers
  1. If condition Then action '*implied End If here*
  2.  
  3. 'or
  4.  
  5. If condition Then
  6.   action
  7.   action
  8.   action
  9. End If
So when you write
Expand|Select|Wrap|Line Numbers
  1. If Inn = txtFirstname Then MsgBox "OK"
  2. Else MsgBox "This name is not valid" 
The first line is a complete statement and stands alone. Just think of it as having the invisible End If on the end. The second line starts a new statement with Else, which doesn't make sense to a compiler.
May 14 '09 #13
NeoPa
32,556 Expert Mod 16PB
@GoodGirl
I'm afraid that code is only checking the entered value against the current record. You would need to use something like :
Expand|Select|Wrap|Line Numbers
  1. Private Sub SearchButtonl_Click(Cancel As Integer)
  2.   Dim strInn As String
  3.  
  4.   strInn = InputBox("Please enter employee's First Name?")
  5.   If strInn = "" Then Exit Sub
  6.   If IsNull(DLookup("[FirstName]", _
  7.                     "[DetailedInformation]", _
  8.                     "[FirstName]='" & strInn & "'")) Then _
  9.     Call MsgBox("This name is not valid")
  10. End Sub
NB. It's always a good idea to attempt compilation of code first, before posting.
It is always a good idea to ensure that variable name checking is enabled, AND your code compiles (at least compilation has been attempted), before submitting a question.

This avoids asking questions which are much more easily resolved on your own PC than on a forum.

To ensure variable name checking is enabled for all new modules, go to - Tools / Options / Editor (from the VBA Editor window) and set Require Variable Declaration to True (checked). For existing modules, ensure that the Option lines at the very top include :
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
To compile your project, select (again from the VBA Editor window) Debug / Compile Project Name.

We ARE generally happy to help with compilation problems too (If you find an error reported and you can't resolve it, let us know), but we do expect members to have tried compiling before submitting a question. That way we have a better idea of the sort of problem we're looking at.
May 14 '09 #14
NeoPa
32,556 Expert Mod 16PB
@ChipR
Chip's response is better explained, as well as more correct, than my earlier post. Ignore mine and read his :)
May 14 '09 #15
Neopa & ChipR :

4- I choose the lower case.

When I put my 5th line as Else statement without End If, it shows a compile error [Else without If], when I put ElseIf, it shows syntax error (without any other clarification) and shows [ElseIf] statement with red line.

Neopa : your code in participation #14 didn't work!! , I still click on the search button without any response or even error message.

I wrote this code and shows to me (after entered the name) : [ Wrong name] which means it didn't make any comparison, what I should add?
Expand|Select|Wrap|Line Numbers
  1. Private Sub searchButton_Click()
  2. Dim Inn As String
  3. Inn = InputBox("Would you enter the Employee's First Name?")
  4. If Inn = txtFirstName Then MsgBox ("OK")
  5. If Inn <> txtFirstName Then MsgBox ("Wrong Name")
  6. End Sub
Neopa: I think I have a compilation problem (based on your explanation) because after I checked (Require variable compilation), nothing changed and [Option Explicit] didn't show in the code.

-Every time I post or take any code, I compile and run it before write or do anything.


Thanks guys.
May 15 '09 #16
I'm waiting anybody answer.
May 18 '09 #17
Stewart Ross
2,545 Expert Mod 2GB
I suspect you will need to rethink what you are doing in this one. Aside from your IF statements still being unsoundly written (even after what has posted above to help you) it is not possible for us to know what the source for your txtfirstname variable which you are comparing the input to should be.

You were asked by NeoPa in post # 5 to give us more information on which we can help you, but as yet you have not responded to the detail of that request (what you put into post 6 does not give us enough detail to know how you are intending to compare values at all).

Sorry, but I cannot see how we can help you when you have not given us anything to work on other than a somewhat redundant pair of IF statements which are not the true source of your problem.

-Stewart
May 18 '09 #18
NeoPa
32,556 Expert Mod 16PB
@GoodGirl
Did you change the code to reflect your actual control name. I copied SearchButtonl_Click from ADezii's post. That would explain why nothing happened.
@GoodGirl
Option Explicit will appear for every module created after that is set. I'm afraid it doesn't change any existing modules, but is definitely a good idea to have it set. That way you will never experience the problems associated with this in any future projects.

For existing modules, simply type or paste in a line :
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
@GoodGirl
That's great. I wish everyone were as clever.
May 18 '09 #19
Nice NeoPa:
I post [Option Explicit] and finally it worked with me
I put this code to ensure the simplest level of comparison:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Private Sub searchButton_Click()
  3. Dim Inn As String
  4. Inn = InputBox("Would you enter the Employee's First Name?")
  5. If Inn = "amin" Then MsgBox ("OK")
  6. If Inn <> "amin" Then MsgBox ("Wrong Name")
  7. End Sub
It worked very well, when I entered “amin” it shows OK, When I entered any name other than “amin” it shows to me “Wrong Name”.
So, now I want to create a variable which holds values of FirstName column ,then I can enter this variable into a comparison with [Inn].
After that , I want to select all the information related the FirstName and show it in a message box (in case of true name).
stewart-ross-inverness:

Thanks for contribution , you are right maybe not all details are clear (I’m sorry for that) , but I’m writing as much as I can understand.

I appreciate you guys .
Jun 4 '09 #20
NeoPa
32,556 Expert Mod 16PB
Right then.

We need to start with the table you're planning on keeping the name data in. What are the details (meta-data)?

META-DATA
This will work best if you can post the meta-data (info about the layout / structure) of the table in the same way as I use in my example. Click on the Reply button and you will have access to all the codes I've used. PK & FK stand for Primary Key & Foreign Key respectively. Never use TABs in this as the layout gets mucked up. Use spaces and all is fine.
Table Name=[tblStudent]
Expand|Select|Wrap|Line Numbers
  1. Field           Type      IndexInfo
  2. StudentID       AutoNumber    PK
  3. Family          String        FK
  4. Name            String
  5. University      String        FK
  6. Mark            Numeric
  7. LastAttendance  Date/Time
Jun 4 '09 #21
Nice NeoPa , but I didn't get the point?
what's the relation between the code required for the button and what you said?
Jun 4 '09 #22
NeoPa
32,556 Expert Mod 16PB
The code will need to refer to the items.

When I get to think about what needs to be in the code I will need to have an understanding of what I'm working with.
Jun 4 '09 #23

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: rcmail14872 | last post by:
I have a form that lists several different categories for labels. I made a form that will let the user add a new category. On the Form the user can type in the name for the new category, then I...
9
by: thebison | last post by:
Hi all, I hope someone can help with this relatively simple problem. I am building a timesheet application using ASP.NET C# with Visual Studio 2003.As it is only a protoype application, my...
1
by: fong.yang | last post by:
I'm trying to set up a search button on a form. I went thru the FindRecord macro wizards and it's not working correctly or maybe it is and I just don't understand all the arguements I have to...
0
by: porky008 | last post by:
I have this search button working for the most part. I would like it if some one could take a look at it and let me know what I am doing wrong it though. Basically I want it to display movie not...
1
by: tagnum | last post by:
I noticed a strange behaviour in IE 6. Not sure whether it's a bug in my code or because of IE 6. I have written a search function using a standard search input box and search button. (HTML input...
1
by: stevemanser | last post by:
If you need to make a search to find customers in a group (or groups) and the groups are defined in a separate table with a intermediate join table, then this piece of code is for you. The customers...
1
by: Ledmark | last post by:
I have a database that uses four forms and each form has it's own table and each form has it's own search button to find a specific record within that table. I would like to use one search button...
1
by: cbones | last post by:
Hello, I am trying to create a search button in a windows forms application. I used a Microsoft Access 2007 file as the database and would like to be able to search the file by specific fields. ...
3
by: munkee | last post by:
Using Allen Brownes advanced filter I would like to add the option of an advanced search button. When this is clicked a popup opens with further search options. How would I go about joining the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.