473,405 Members | 2,300 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,405 software developers and data experts.

Codes for FIND button in MS Access Programming

Midzie
25
Hi everyone, anyone could help me with my codes. I have here Find button, txtEmployeeID, txtFirstName, txtLastName, cboUserRole. The user should type an employeeid to be searched in txtEmployeeID then press Find button. If txtEmployeeID is in tblUser then txtLastName, txtFirstName and cboUserRole will be populated with a value assoaciated with the employeeid and it will be enabled for editing. If txtEmployeeID is blank message should appear "Please enter an employeeID" and if txtEmployeeID value is not found in tblUser then "EmployeeID doesn't exist.". Please help me revise my codes, thanks in advance:-)

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdFindEmployee_Click()
  2.  
  3. Dim curDatabase As Object
  4. Dim rsttblUser As Object
  5. Dim fldEmployeeID As Object
  6.  
  7. Set rsttblUser = curDatabase.OpenRecordset("tblUser")
  8. Set rsttblUser = curDatabase.OpenRecordset("SELECT EmployeeID, Lastname, Firstname, Role from tblUser ")
  9. With rsttblUser
  10.     Do Until .EOF
  11.         For Each fldEmployeeID In .Fields
  12.             If rsttblUser("EmployeeID").Value = txtEmployeeID Then
  13.             Me.txtLastName = CStr(rsttblUser("Lastname").Value)
  14.         Exit For
  15.  
  16.             If fldN_Number.Name = "EmployeeID" Then
  17.             If fldN_Number.Value = txtEmployeeID Then
  18.             If Me.txtEmployeeID.Value = DLookup("EmployeeID", "tblUser", "[EmployeeID]='" & Me.txtEmployeeID.Value & "'") Then
  19.             txtEmployeeID = rsttblUser("EmployeeID").Value
  20.             Me.txtLastName = .Fields("Lastname").Value
  21.             Me.txtFirstName.Value = DLookup("Firstname", "tblUser", "[EmployeeID]='" & Me.txtEmployeeID.Value & "'")
  22.             Me.cboUserRole = .Fields("Role").Value
  23.             MsgBox "OK"
  24.             .Edit
  25.             .Fields("Role").Value = cboUserRole
  26.             .Fields("LastName").Value = txtLastName
  27.             .Fields("FirstName").Value = txtFirstName
  28.             .Update
  29.             Exit For
  30.         End If
  31.         End If
  32.     Next
  33. .MoveNext
  34. Loop
  35.  
  36. End With
  37.  
  38. End Sub
Feb 2 '12 #1
5 2400
Mihail
759 512MB
I think you have a tables of employes, isn't it ? So why you not design a simple combo box from where the user can select the employe ? Sort the combo box by names to make the user's life easier.
Feb 2 '12 #2
Midzie
25
Hi Mihail, thanks for the reply. I am using one form for maintaining user information. So I have additional button as ADD, SAVE, EDIT, DELETE so when user select the add button the text box controls will be used also. When user wants to edit or delete, they will also use the txtEmployeeID to find for user to be deleted or edited.
Feb 2 '12 #3
TheSmileyCoder
2,322 Expert Mod 2GB
Please bear with me as I ask this question. It is not meant to be offensive though it may be perceived as such. It is not my intention.

Am I right in assumming that you have little to none knowledge of how the code works and it is simply something you copy/pasted together?

Its quite important information for us to know, in order to best help you.


Example:
Expand|Select|Wrap|Line Numbers
  1.        For Each fldEmployeeID In .Fields 
  2.             If rsttblUser("EmployeeID").Value = txtEmployeeID Then 
  3.             Me.txtLastName = CStr(rsttblUser("Lastname").Value) 
  4.         Exit For 
This loops over a collection of fields, and for each field found, it looks in a specific field in a recordset . Its kinda like saying: For each apple hanging on the tree look through the box on the ground to find a pear. There is no correlation between your loop and your actions.
Feb 2 '12 #4
Midzie
25
Yes, it's ok. I'm new to programming but I understand a little logic:-) I find it hard doing MS Access VBA that's why I joined here. Anyway, I'm done with my Find button. Here's my code to help beginners like me:-) Thanks everyone for helping us.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdFindEmployee_Click()
  2. On Error GoTo cmdFindEmployee_Error
  3.  
  4. Dim curDatabase As Object
  5. Dim rstSIMMEMP As Object
  6. Dim fldN_Number As Object
  7.  
  8.  
  9. If IsNull(txtEmployeeID) Then
  10.     MsgBox "You must enter an N_Number.", vbOKOnly Or vbInformation
  11.     Exit Sub
  12. End If
  13.  
  14. If Not IsNull(DLookup("N_Number", "SIMMEMP", "N_Number='" & txtEmployeeID & "'")) Then
  15.     txtFirstName = DLookup("Firstname", "SIMMEMP", "N_Number='" & txtEmployeeID & "'")
  16.     txtLastName = DLookup("Lastname", "SIMMEMP", "N_Number='" & txtEmployeeID & "'")
  17.     cboUserRole = DLookup("Role", "SIMMEMP", "N_Number='" & txtEmployeeID & "'")
  18. Else
  19.     MsgBox "The N_Number doesn't exist! Please try again"
  20.  
  21. End If
  22.  
  23. cmdFindEmployee_Error:
  24. End Sub
Feb 2 '12 #5
TheSmileyCoder
2,322 Expert Mod 2GB
Its fine to be new in Access. We have all been there at some point. Its just easier to give the correct help if we are familiar with your level.

While your solution will probably give you what you want it is quite in-effective. Having to use 4 Dlookups is one thing when your in the testing phase and probably have around 10 or 100 records in your database. Once it goes live and you maybe have 1000 or 30.000 or more records, having to do 4 dlookups after another can have a quite severe impact on your performance.



I need to ask why you are not using a bound form? Access is based on using bound forms, and it is what makes access a good tool for alot of projects. But if your not using that feature, its kinda look grabbing a good hammer and then using to wrong end to hit the nail with.
Feb 2 '12 #6

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

Similar topics

2
by: amywolfie | last post by:
I would like to place a Find button on a form which uses the built-in Access Find facility. If no records are found, I would like to display a custom "no records found - plesae try again" message...
2
by: Chris Fink | last post by:
This should be relatively simple but I am unable to find an asp:button tag in a datalist footer. I have tried it numerous ways including the FindControl method from the many events that the...
2
by: ad | last post by:
I have a Button (named btnOK) and a ImageButton (named ImageButton1) in a Template field of a GridView; I use the codes below to find the Button and ImageButton, the ImageButton can be found,...
1
by: TheScullster | last post by:
Hi all Bit of an obscure one this, but hopefully someone will be able to help. 4 years ago we had a potentially catastrophic problem with our Access database. From memory it was written in...
4
by: saima khushbakht | last post by:
hi i need coding for find button using adodc control.the search should be by name.if we enter the name of employee then search button should search name from data base either it exists in batabase...
0
by: Mike | last post by:
I have a web page that is using the asp.net wizard control. On this wizard, there is a next, cancel buttons, how can I find these buttons in this wizard. If I do something like this...
2
by: daniel_nolan | last post by:
I'm brand new to Python--and programming in general. I'm trying to use IEC to control Internet Explorer. I've navigated to a page, and now I'm trying to click a button. The button appears to be...
0
by: gokul | last post by:
You can find all related details regarding programming languages right from C/C++, Java, .net and many more. Find details of the jobs available in C/C++ programming language platforms ...
1
ddtpmyra
by: ddtpmyra | last post by:
If I use the wizard to built a find button it will generate below codes. But what about if I wanted to have two fields how will I do that? Private Sub Command33_Click() On Error GoTo...
0
by: Solomon Obeng | last post by:
Access 2003 enables you to print codes from print menu. But it is not so with Access 2007. How do I print the codes from an Access 2007 form?
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.