Looking Up Record, based on Multiple Fields - ms-access

I'm New to MS Access, I'm working on a project for work at the moment and have run into a snag. I'm not sure if I'm doing this right, I made the mistake of making a simple access db to track my workload, and suddenly i'm "The database guy"
So We've recently started a new product review team within the company, and the database im designing is going to help manage the new part introduction process.
I've got our review sheet designed, which will be called up whenever a part is reviewed. I have not made any relationships to this table because at any time a top level part we've reviewed could be a component to a new kit. So doing master/child parts isn't much of an option. (At least from what i can see)
How i have this sheet being called is as follows. We enter in some top level information in the main form (frmDPARTOP) regarding the review, Customer, Revision, PO, etc.. In a subform (frmDPARPARTS Subform) we list Every Part associated with this, including itself.
I've run into some problems getting the next part to work
I have an unbound text box (Text14)on the subform that is supposed to be looking for records in table (tDPARSHEETS) based on the appropriate Customer, Part_Number, and Revision. If it finds a record on file matching these three fields, its value is set to change to "On Record" or "Needs Review" if its unable to locate a file. However, it is always indicating "Needs Review" .LocalPartNumber and LocalRevision are in the table tDPARSHEETS, I've tried several variations, looking for different items, different ways. I've debug printed the values, and they are correct.
If DLookup("LocalPartNumber", "tDPARSHEET", "[LocalPartNumber]="" & Me.Part_Number & ""AND [LocalRevision]='" & Me.Revision & "'") Then
Text14.Value = "On Record"
Else
Text14.Value = "Need Review"
End If
The Other issue i'm experiencing is based on the same situation. I would like to be able to double click the items listed in the Subform and pull up the record on file if there is one, if there is not, i would like to create one, with the information already provided filled in. I've put a test entry in with the Customer, Part Number, and Revision filled out. The MsgBox Fires telling me the record is on file, However, the OpenForm command opens to a blank form.
Private Sub Customer_DblClick(Cancel As Integer)
If DLookup("LocalPartNumber", "tDPARSHEET", "LocalRevision ='" & Me.Revision & "'") Then
MsgBox "Record Found"
DoCmd.OpenForm "frmDPARSHEET", , , "[LocalPartNumber]="" & Me.Part_Number & ""AND [LocalRevision]='" & Me.Revision & "'"
Else
MsgBox "No Luck"
End If
All fields I'm working with are Text Fields, as our part numbers and revisions often include letters and numbers
I'm sure there is a better way to do this entire process in general, but We are rolling out an entire new ERP system in May, and i'm under the impression that this is meant to hold us over until then. I would like to have it built well enough to work indefinitely (as we have a hard time letting go of things), but in reality this could all be thrown away in 6-8 months
I Appreciate any Advice/Help

Have in mind please, that DLookup returns Null for "not found". Also, be careful with spaces. Thus:
If IsNull(DLookup("LocalPartNumber", "tDPARSHEET", "[LocalPartNumber]=" & Me.Part_Number & " AND [LocalRevision]='" & Me.Revision & "'")) Then
Text14.Value = "Need Review"
Else
Text14.Value = "On Record"
End If
If partnumber is text, you need to wrap that in quotes as well:
If IsNull(DLookup("LocalPartNumber", "tDPARSHEET", "[LocalPartNumber]='" & Me.Part_Number & "' AND [LocalRevision]='" & Me.Revision & "'")) Then
Text14.Value = "Need Review"
Else
Text14.Value = "On Record"
End If

Related

Access ODBC Subform field size too small to edit

I have a form with a combo box that allows a user to filter data in a subform using
DoCmd.SearchForRecord , "", acFirst, "[Division] = " & "'" & Screen.ActiveControl & "'"
The user should then add data to the subform below.
I've two problems:
When the user tries to edit this data, they get "The field is too
small to accept the amount of data you attempted to add. Try
inserting or pasting less data". It doesn't matter which of the
fields the user tries to enter data into, they all suffer this
problem.
The "Division" field that has been filtered, shows up as #Error for
the new record line. I've set a temp variable for this (see below), so it should
be showing the value used for the filter, but does so with or without the default set.
TempVars.add "Div", [Combo5].Value
I've looked in my ODBC forms and all the data types/sizes etc look to be the same.
There's data already in this form, which can be edited, even though it's the same size, but no new records can be saved. As well as this I can edit data in the supporting table.
Answer
Thanks to #AVG and #WayneG.Dunn for help. I ended up using the following code and using the filter as suggested. I couldn't get it to work very well on the subform, so I hid the field and used that as a Master Field for the subform. Still getting and error message "field too small" and "#ERROR" in the new record label, but the form works, so I'm just going to use it as is. Code for reference:
DoCmd.SetFilter "Division", "Division = " & "'" & [Combo5].Value & "'"

How to have at most n number of duplicates in a field

My question is somewhat unique I assume. Everybody wants to get rid of duplicates, but me on the other hand, I want to allow a specified number of duplicates to appear.
I'm creating a database for reserving dates for Blood donation campaigns.
My database consists of the
Date of campaign,
Name of organizer,
Contact info & venue.
Additionally every campaign will get a unique ID number. The catch is, we can accommodate only five campaigns per day, that is: The date column should be allowed to have at most five duplicates
Any ideas on how this can be achieved?
If you are entering the information directly into the table from within Access 2010 itself then you can add the following Before Change data macro to the table:
For more information on Data Macros see
Create a data macro
This is what you need to do. Create a New Form, based on the table. A lot more neater and also can capture all the information you need along with special conditions, like the one you have right now. The form can have all or some fields that are present in the table. Once the forms has been created, we will call it frm_EventMgm
You can create another Dashboard/welcome form where you can have one TextBox, for collecting the date you want to enter (dateEntryTxt) in the table and one button (addNewEntryBtn), which on lick will have this.
Private Sub addNewEntryBtn_Click()
If Not IsDate(Me.dateEntryTxt) Then
MsgBox "Please enter a valid date before you proceed.", _
vbCritical, "Missing Information !"
Exit Sub
End If
If DCount("*", "tbl_EventList", "DateFieldName = " & _
Format(Me.dateEntryTxt, "\#mm\/dd\/yyyy\#")) > 5 Then
MsgBox "The Date you selected has already have 5 bookings. " & _
"So please choose another date and try again.", vbInformation, _
"Cannot Add info. !"
Else
DoCmd.OpenForm "frm_EventMgm", DataMode:=acFormAdd
End If
End Sub
This is the best and easy way to get around your issue. Hope this helps.

use msgbox to tell how many records have been inserted and updated

How do I incorporate a mysql query into a messagebox in vba. What I want to do is when a user sends data into the database using dgv. How can I get a messagebox to show how many records are going in as updatse and how many are going in as inserts.
FYI I am using on duplicate key update function.
Thanks
It might help to see a little bit of your code, but you just need to set up a variable represent each element you want to display (say nUpdates and nInserts). Then just display that variable in the message box:
MsgBox("Records going in as updates:" & nUpdates & vbNewLine & vbNewLine & _
"Records going in as inserts:" & nInserts)

MS Access: Filter/Search records in a form using main form and subform criteria from user input

I'm currently working on a database which is rather complex, at least for my ability level. Essentially it is a database of projects, structures and contacts. Within these structures there are sub structures each with unique attributes.
Projects, structures and contacts are joined together in one main control form with a tabbed pane. And within these panes are the forms which have multiple subforms. I am trying to write a search function which I can place on each form that will allow the user to select multiple criteria to filter results. However, some of the criteria or on the forms and other criteria are drawn from multiple subforms and to top it off some fields in the subforms have multiple entries. I understand that this may not sound like very sound database design, but I've been told to adhere to a specific structure and layout which I am not skilled enough to work through.
I've been looking through allen browne's website regarding search criteria, but that is only one a single form. I've attempted to piece together the Subquery tutorial page as well, but to no avail. Essentially, I'm wondering if it is at all possible to filter information say.. the type of structure which is on the main form and then for example.. a range of numbers such as length which exists in a subform and an object proximity which exists in another subform, and have it so that the main form only displays records which fulfill that combination of criteria from the subforms and main form.
My apologies if this is vague, and if it'll help I can post up the general framework I have for my database thus far. Thank you for any assistance you can provide.
EDIT--- Added screenshots for more information
I can't post images since my rep is below 10 but hopefully I can include links
http://img829.imageshack.us/img829/1594/99258898.png
http://img40.imageshack.us/img40/2186/27578829.png
It's difficult to see here, but within the combo boxes some are multi-valued lists. And the combo box on the first image to the right, is switched based on the selection of Type from the upper left combo box, which has different attributes. Ideally I'd like to be able to search through primary forms and subforms. and result in records which only fulfill all the user inputted data. Right now I've tried using filters, one for each criteria, but all that does is blank out information in the subforms which doesn't correspond to the filter criteria.
Do you have any screenshots you could possibly post?
This sounds to me like a larger design issue, perhaps mostly in your UI but possibly in your table structure as well. One my own principles I try to follow is to limit how many things a single screen/form does or shows. What your describing sounds quite confusing to me and I think the average user could easily be overwhelmed by it.
Normally subforms are the result of a main form. That is, they contain child records and are linked to records in the main form via the child records foreign key and the main forms primary key. It isn't that there aren't other ways to design your forms but this is considered "normal" or "standard" design. Your description indicates that filtering the main form is determine by filters set on the subforms. I guess I can't think of any problem that could possibly need solving through something as confusing and clumsy as this. I leave the possibility open that I haven't encountered something as difficult or complex as you're dealing with.
I think you may have some difficulty getting better help here as your question is really better suited to a discussion forum such as UtterAccess.com. You may consider posting a copy of your database on such a site so the users can review your design and possibly recommend a more solid and standard approach.
Edit1:
1) Based on your screenshots, I think you're trying to do too many things in one form. I would normally take each of your top tabs (Projects, Structures, Companies, Contacts) and make each of those their own form. Access 2007 and 2010 allow you to use a tabbed interface natively and I can't think of any real benefit to using the tabbed container control to develop your own tabbed interface.
2) It's my opinion that searches should normally be performed in a single main form that has a single listbox, datasheet view subform, or continuous forms subform. Whichever of these three you choose is not overly important. After performing the search, the user should be able to click in the resulting records in the listbox or subform to select that record and move to it on a details view form similar to what you've already built here. It might take several different search screens to allow your users to search all of the different ways you'd like them to. For example, you might build a screen to search projects, one to search structures, another to search contacts, and a fourth one to search companies. It should be possible to combine some of these in a query and allow the user to search by more than one criteria but it can get confusing if you try to allow them to search too many fields at once, especially if the fields they are searching are in separate tables. For example, if you build a query that shows all projects, structures, and companies together and use this as the recordsource for a datasheet subform, this could allow users to search by project, structure, or company. However, if a project has 10 structures that project will be listed 10 times in the query/form which could confuse users if they are thinking about searching by project. If they are instead searching by structure, it will make perfect sense to them that there are 10 entries for the same project since they'll see 10 different structures.
My basic recommendation is, be careful about trying to build too many functions into a single form. There are some types of tasks or sets of data that do require complex forms with multiple subforms and lots of controls, but in general you want to try to avoid this. It's better to build each form with a very focused task and just simply build more forms to accomplish all of the necessary tasks. Of course, you can take this approach too far as well but you do really need to try to minimize complexity to save yourself from headaches in the future.
As far as filtering based on multiple criteria, I'll see if I can find any good examples or maybe put one together and make it available. It isn't so difficult but it is a multi-step process to build and experience has led me to a general design that I'm happy with and that works almost flawlessly in every possible situation.
Edit2:
Here's how you can create a multi-field filter/search.
1) Build your query. Include all tables and fields which need to viewed by the user or searched on by the user. You can include fields here that will be searched on but not viewable by the user, although that could be confusing to the user. Save your query and give it an appropriate name. Be sure to include your id/primary key fields since those will be needed in step number 4.
2) Now Highlight your query in the navigation pane, go to the top menu and choose Create > More Forms > Datasheet (I'm using Access 2007). Allow access to create and show you a datasheet form which we will use as a subform. Save this and give it an appropriate name like subformProjectSearch of fsubProjectSearch.
3) Get the design view for the new form you just created. Change the properties to disallow additions, deletions, and edits unless you'd really like to allow any of these operations. In general, I don't allow any data entry in these kinds of search forms unless I have it well planned out and tested.
4) Go to the code for this form and add code to popup relevant detail forms on double click. For example, in the projectid or project name field, allow your user to double click to bring up the project details form. Add double click routines to any other textbox where the user might want to pop up a structure, company or contact, assuming you've chosen to include one or more of those in your query. You're code will look something like DoCmd.OpenForm "ProjectDetails", , , "ProjectID = " & Me!ProjectID Now save your form again and close it.
5) Now create a new blank form and add your first form as a subform. Leave room at the top to add controls for filtering. This form does not need to have any recordsource set.
Diversion: I usually take one of two different approaches to filtering. Number one is to give the user one textbox that they can type into. I think do fuzzy searches in a whole bunch of different fields using the data they've typed in. It's a perfectly awful approach performance wise, especially once the database grows past about 20,000 records. It can also lead to some confusion since too many results will likely come up for a short generic search. If they only type in the letter s and search for it they will likely get almost all records.
The second approach I use is probably more common. Give them a different textbox for every field they are likely to want to search by. You an also make it a checkbox or combobox if that fits the field they are searching on. These will not be bound controls. This allows the user to get fairly specific and it also is most likely to return the exact results they are looking for. As far as the code goes, I don't think that one approach is really much simpler than the other. With either approach that I've listed here you really need to check the type of data they've entered before you put it in your filter statement for certain fields. For example, you wouldn't want to try to filter a date field using their non-date, text input. On this second approach you can possibly eliminate more of this problem by using validation rules, input masks, or by setting the textboxes format property. Combos also help to prohibit incorrect data from being entered.
Yet a third option is somewhat of a hybrid approach. You can create one text box that searches on more than one field but not necessarily on all fields. For example, you might create one textbox that will be used to search on Company, FirstName, or LastName. You can create as many of these textboxes as you need. How the input in these textboxes gets used will be totally determined by you in your VBA code.
6) Now that you've decided which approach you want to take, you can go ahead and add your controls. Give them appropriate names and labels.
7) Now it's time to create the function that will build the filtering statement. Use one of the two ideas I listed below. Make sure you run these when the user presses your search button or on the controls AfterUpdate event.
Private Sub txtSearch_AfterUpadate()
Call FilterSubformOption1
End Sub
Private Sub FilterSubformOption1()
Dim strFilter as String
Dim sSearch as String
sSearch = Replace(Nz(Me.txtSearch, ""), "'", "''")
If sSearch <> "" Then
If IsDate(sSearch) = False Then
strFilter = "(ProjectName LIKE '*" & sSearch & "*' OR "
strFilter = strFilter & "StructureName LIKE '*" & sSearch & "*' OR "
strFilter = strFilter & "CompanyName LIKE '*" & sSearch & "*')"
Else
strFilter = "ProjectDate = #" & CDate(sSearch) & "#"
End If
End If
If strFilter <> "" Then
Me.fsubProjectSearch.Form.Filter = strFilter
Me.fsubProjectSearch.Form.FilterOn = True
Else
Me.fsubProjectSearch.Form.Filter = ""
Me.fsubProjectSearch.Form.FilterOn = False
End If
End Sub
Private Sub FilterSubformOption2()
Dim strFilter as String
If Nz(Me.txtProjectName, "") <> "" Then
strFilter = "ProjectName LIKE '*" & Replace(Me.txtProjectName, "'", "''") & "*' AND "
End If
If Nz(Me.txtStructureName, "") <> "" Then
strFilter = strFilter & "StructureName LIKE '*" & Replace(Me.txtStructureName, "'", "''") & "*' AND "
End If
If Nz(Me.txtCompanyName, "") <> "" Then 'Search on multiple fields from one textbox
strFilter = strFilter & "CompanyName LIKE '*" & Replace(Me.txtCompanyName, "'", "''") & "*' AND "
End If
If IsNull(Me.txtProjectDate) = False Then
If IsDate(Me.txtProjectDate) = True Then
strFilter = strFilter & "ProjectDate = #" & Me.txtProjectDate & "#"
End If
End If
If Right(strFilter, 5) = " AND " THEN strFilter = Left(strFilter, Len(strFilter) - 5)
If strFilter <> "" Then
Me.fsubProjectSearch.Form.Filter = strFilter
Me.fsubProjectSearch.Form.FilterOn = True
Else
Me.fsubProjectSearch.Form.Filter = ""
Me.fsubProjectSearch.Form.FilterOn = False
End If
End Sub
Private Sub FilterSubformOption3()
Dim strFilter as String
If Nz(Me.txtProjectName, "") <> "" Then
strFilter = "ProjectName LIKE '*" & Replace(Me.txtProjectName, "'", "''") & "*' AND "
End If
If Nz(Me.txtStructureName, "") <> "" Then
strFilter = strFilter & "StructureName LIKE '*" & Replace(Me.txtStructureName, "'", "''") & "*' AND "
End If
If Nz(Me.txtName, "") <> "" Then 'Search on multiple fields from one textbox, the "hybrid" solution
strFilter = strFilter & "(CompanyName LIKE '*" & Replace(Me.txtName, "'", "''") & "*' OR "
strFilter = strFilter & "FirstName LIKE '*" & Replace(Me.txtName, "'", "''") & "*' OR "
strFilter = strFitler & "LastName LIKE '*" & Replace(Me.txtName, "'", "''") & "*') AND "
End If
If IsNull(Me.txtProjectDate) = False Then
If IsDate(Me.txtProjectDate) = True Then
strFilter = strFilter & "ProjectDate = #" & Me.txtProjectDate & "#"
End If
End If
If Right(strFilter, 5) = " AND " THEN strFilter = Left(strFilter, Len(strFilter) - 5)
If strFilter <> "" Then
Me.fsubProjectSearch.Form.Filter = strFilter
Me.fsubProjectSearch.Form.FilterOn = True
Else
Me.fsubProjectSearch.Form.Filter = ""
Me.fsubProjectSearch.Form.FilterOn = False
End If
End Sub
And that pretty much completes the multiple criteria filtering solution I typically use.
I create complex query-by-form interfaces for nearly all my Access apps. Why? Because my users want them! There are some screenshots of some older examples on my website, but none really is very clear. The point is that I'm writing SQL on-the-fly, and choosing whether to query particular tables based on whether there are criteria sought for that table.
I can say a lot more about, but it quickly shades into proprietary information. If these look like the direction you want to go, why don't you create a new question with enough information to suggest how it could be done based on these principles.

MS Access search for record by textbox instead of dropdown

I'm pretty new to MS Access. I'm trying to create a simple form that will basically search for a particular record using a textbox, rather than a drop down box. Essentially a user would be able to enter an ID number and retrieve some other related Info. However, I do not want the user to be able to add any new records to the database. I've been able to get the forms to look the way I want them, but I'm not sure where to place the code (do I create a macro, insert the code into the properties of the button?) Any help is greatly appreciated!
I assume that you have bound your form to a table or a query and that you want to be able to enter the ID manually in a textbox, then press ENTER and load that record's data or display an error message if there is no such record.
As dsteele said, make sure that the form's Data property Allow Addtions is set to No to disallow users from adding records.
Then, from the AfterUpdate event of the textbox, add the following code (assuming that your textbox is named txtGoTo):
Private Sub txtGoTo_AfterUpdate()
If (txtGoTo & vbNullString) = vbNullString Then Exit Sub
Dim rs As DAO.RecordSet
Set rs = Me.RecordsetClone
rs.FindFirst "[ID]=" & txtGoTo
If rs.NoMatch Then
MsgBox "Sorry, no such record '" & txtGoTo & "' was found.", _
vbOKOnly + vbInformation
Else
Me.RecordSet.Bookmark = rs.Bookmark
End If
rs.Close
txtGoTo = Null
End Sub
Note that you will have to change the line rs.FindFirst "[ID]=" & txtGoTo to something that is adequate for your data:
"[ID]=" should be replaced by the field you want to search (it could be "[POReference]=" or something else.
if you are searching by a numeric ID, for instance because the field is an autonumber column, then the code is fine.
Otherwise, if the field you are searching on is a string (say PN12-G) then you have to change the code to:
rs.FindFirst "[ID]=""" & txtGoTo & """"
Failing to use the proper quoting (or quoting where not necessary) will result in errors of the kind Data type mismatch....
As a new user, I would recommend that you have a look at the sample NorthWind project database that is either shiped with older versions of Access or available as a template for download from Access 2007.
There a lots of techniques to learn from as a new Access developer, including other ways to implement record navigation.
Set the form property Data/'Allow Additions' to No.
Either in the AfterUpdate event of the textbox, or in the Click event of a button, you can write code or assign a macro to look up and display the record you want.