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

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.

Related

Microsoft Access vba update date of record on button click

I have a form with combobox cmbProjectName where a project name can be selected from tblProjects. The form also has a button btnComplete.
When the button is clicked, I would like to update the record for the selected project so that field Date Modified is filled with today's date. I'm having trouble coding up the VBA to find the correct record to update.
The field I am looking to update is tblProjects.[Last Modified], and I would like to find the record by referencing the combobox cmbProjectName by using the column tblProjects.projName.
This could be handled several different ways, however, it looks like you might be trying to find a VBA SQL solution. As such, try this:
Currentdb.Execute "UPDATE tblProjects " & _
"SET tblProjects.[Last Modified] = DATE()
WHERE tblProjects.projName = " & cmbProjectName.Value & "", dbfailonerror
This would of course require that the tblProjects.projName be a unique value for the entire table. Otherwise, you will need some other sort of solution.

Looking Up Record, based on Multiple Fields

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

How to make MS Access run a query on focus lose event and take actions accordingly

I am creating a small Access DB for our Data Entry guys. Our main DB is mysql but due to ease of use we are creating an Access DB to make it easier for them to enter the Data. I have done most of it but am stuck with this problem (which I know how to solve in mysql+php) Please pardon my ignorance, but I have just started using MS Access.
I have two tables - ClientPhones and sales. The ClientPhones table has phone, clientid fields. sales table has salesid, clientid, date, etc fields.
I also have a Form which has all relevant fields for the sales table. I want to add a new field, phone_no in that form. When a user inputs the number and on focus lose event, I was access to run a query on the clients table to see if the phone number exists in any of the 3 phone number fields. If it finds a client with that phone number, the client ID should be populated, else a new form to input the client details should be shown.
Is this possible with MS access or am I doing this incorrectly?
Use the text box's After Update event to retrieve the clientid which matches the phone number the user entered.
If a clientid is found, store it in the text box which is bound to clientid.
If no match is found, ask whether the user wants to add a new client, and open that form if they respond yes.
This code outline assumes txtSearchPhone is the name of the text box where the user enters the target phone number, and txtClientId is the name of the text box where you want to store clientid.
Private Sub txtSearchPhone_AfterUpdate()
Dim varClientId As Variant
Dim strCriteria As String
strCriteria = "[phone]='" & Me.txtSearchPhone.Value & "'"
Debug.Print strCriteria '<-- inspect this in Immediate window; Ctrl+g will take you there
varClientId = DLookup("clientid", "ClientPhones", strCriteria)
If IsNull(varClientId) Then
If MsgBox("Add new user?", vbYesNo) = vbYes Then
'DoCmd.OpenForm ... (see Access help topic)
End If
Else
Me.txtClientId.Value = varClientId
End If
End Sub
Make sure the text in txtSearchPhone does not include a single quote character (') because the DLookup will break if it does. You can use the text box's Validation Rule and/or Before Update event to make sure a single quote is not present.

Can't add record with ID from another table

I have a database which functions similarly to a "help desk" system - an outside user creates a request ticket, then a consultant will use the database for various tracking and reporting.
There is a form that the consultant uses to display and work with the end user request. They select an existing record (request ticket) from a combo box. The form then populates with the information of the request, which the consultant can edit if needed.
I then have a series of buttons on this form that the consultant can use to open different forms (worksheets) and enter data in each one. These are all optional and will vary based on the type of request.
These worksheets all populate the "Consulting" table, while the initial requests are in a "Request" table. There is only one record in the "Consulting" table per record in "Request" (1:1 relationship).
Here is the code I have to open the forms (worksheets):
Private Sub ButtonGap_Click()
DoCmd.OpenForm "Gap Analysis", acNormal, , "[ID] = " & Me!ID, acFormEdit, acDialog
End Sub
This only partially works. If there is existing information in the "Consulting" table with an ID that corresponds to the ID from "Request" (what they selected in the combo box at the start), the worksheet will open pre-populated and editable. Great!
But... if there is no existing information in the "Consulting" table with an ID that corresponds to the ID from "Request", the worksheet opens blank, to a new record. This record autonumbers from where the "Consulting" table left off. This creates a problem if several requests have come in - we are not always working on them in order, so the new record ID in "Consulting" does not match the existing record from the "Request".
I'm sure it's something incredibly simple I'm overlooking. Can anyone help?
I was able to find a way to do this, it appears. If I missed something, please let me know! I don't want to break anything else. :)
I created a query to find the unmatched records, giving me a list of all IDs from Request without a matching ID in Consulting.
From there, I based an append query on these results, to append just the missing IDs to Consulting.
That query runs when the Request form is submitted:
Private Sub Form_AfterUpdate()
DoCmd.OpenQuery "Add Blank Consulting"
End Sub
Now my worksheets on the Consulting form are populating with the correct ID!

How do I export a MS Access report to PDF with a formatted ID value as the report name?

I am using Access 2007.
I have created a button that allows me to output my current invoice to a .pdf and I want it to be named by "invoice number plus client name" and I am close but not quite there yet. This is the code I currently have and it works fine:
Private Sub Create_Document_Click()
DoCmd.OutputTo acOutputReport, "Invoices", acFormatPDF, [Invoice_ID] & "_" & [Client_ID] & ".pdf"
End Sub
This gives me a report called "1_1.pdf" what I would like it to be called is "1007735001_XYZCompany.pdf".
In my Invoices table I have formatted the Invoice_ID field which is an auto number field as "1007735"00 but outputing the report names it as 1 instead of the formatted number, I would first like to know if I can change this to show the formatted value. Is it possible?
I would then like to know how I can change the client name value from 1 to the client's name. My field names are Client_ID and Client_Name and the table is called Clients.
I have searched through several forums now and tried many solutions but all have given me errors, Any help would be greatly appreciated. If I need to give more info please let me know.
You appear to have committed a number of sins. You should not add formats to tables, as you have found, the format is just a format, it does not change the content of the field ( http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/MSAccess/AccessVBAJetSQL/why-you-should-not-add ). In additon, you appear to have added a look-up to ClientID, another anti-feature ( http://access.mvps.org/access/lookupfields.htm ).
You will need to look up the client again and reformat the number.
Client = DlookUp("ClientName","ClientTable","ClientID=" & [Client_ID])
Invoice = Format([Invoice_ID], """1007735""000")
FileName= Invoice & "_" & Client & ".pdf"