Filtering data by clicking a button - ms-access

I'm pretty new to Access so bear with me.
First of all I have a Table with 6 columns, namely: first name, second name, address, city, state and zip.
I have designed a report where only the first names appear with a button behind them.
If the button is pressed I want a form to open on the row so that it gives the details of the person clicked.
I have already created the form it opens up, yet I can't get to open the right record.
What must I do to get the right record?

The OpenForm method of DoCmd has the following arguments:
expression.OpenForm(FormName, View, FilterName, WhereCondition,
DataMode, WindowMode, OpenArgs)
-- http://msdn.microsoft.com/en-us/library/aa141520(v=office.10).aspx
The one you need is the WhereCondition
You do not mention a unique ID on your table, but you will need one, so add an autonumber field to your table if it does not already have one. You will also need to ensure that the form (not report) includes this field, although it does not have to be visible. Your code will then look something like:
DoCmd.OpenForm "PersonDetails",,,"ID=" & Me.ID

Thanks for your reply
I already solved the problem!
I droped the report and created a form instead(I thought that only reports were able to list the rows)
Added a macro when clicked on the ID number on the dataview form the detailed view opens with the same ID number

Related

Am I looking for an "After Update" event or something else?

I'm trying to make a database for paperwork for a summer camp. Right now I have a form setup that has a combo box with the the children's name in the header. And I have three tabs separating the different papers they need to turn in (medical, permissions, etc). I have the combo box working, sort of. When I click on it I have it showing the list of names, but I don't seem to have it connected to the underlying table, so it could show the records. The records are all a mix of checkboxes (yes/no) and text boxes (for additional comments). Does the combo box need to have an After Update event, or would it be in another event? I've tried copying some code that I've found, but it hasn't seemed to work. Right now there should be some information that shows up that I've already entered directly into the table, but other fields I know are blank.
My goal is to be able to select a child from the combo box and pull up their records and edit information as needed. I'm a real beginner with coding, but I think it's something that would lead me to a solution here. I've been across a variety of forms today and nothing seems to have worked for me yet. I'm also just beginning to learn access so I'm only starting to know the kinds of questions to ask. Any help or suggestions would really be appreciated.
In an Access form, there are either bound or unbound controls. Bound means table fields are connected and unbound means there are no table field connection. The same can be said about bound and unbound forms where bound has a whole table connected to form and unbound does not. If you enter Design View, you will either see a field name in textbox or "Unbound" written in textbox. For checkboxes you cannot tell unless you enter its Property Sheet \ Data tab \ Control Source.
From your explanation, you seem to refer to a search box. This would be an unbound form as no data should be tied to it. Your header of Student's Name seems to be a bound textbox of student name. Search boxes have an AfterUpdate event built in either as a macro or VBA that searches a record that matches the selection of the drop down:
Macro:
Search Record
Object Type: Form
Object Name: YourFormName
Record: First
Where Condition: ="[ID] = " & Str(Nz([Screen].[ActiveControl],0))
VBA:
DoCmd.SearchForRecord acDataForm, "yourFormName", acFirst, "[ID] = " & Forms!YourFormNam!DropDownField
Please note you can create a search box simply by placing a new combo box on the form and following the Wizard all the way through. The outcome of this will be an AfterUpdate event macro as listed above. You must have canceled the wizard before finishing to have an unbound combo box with no functionality.

Access VBA - Button on Report to show additional information

I have a report called 'Expenditures' that shows various information about each record (date, amount, type, etc.). Each record also has a 'Notes' field.
I want to add a button on each line of the report (I've done that part) that will show the Notes for the selected record. I'm not sure if this would just be running a query, then displaying it, or something else.
All I have right now is a query that shows the Notes field, but when you click the button, it shows the Notes field for all the records since I don't have any criteria yet.
Is this possible? If so, I would appreciate any help! Thanks in advance.
Typically reports in access are not 'interactive' (unless you view them in "Report" view). For example, the button will not be visible on "print preview".
I suggest you add a field to your table that controls the visibility of the field (ShowNotes = Yes/No). That checkbox would be added to the form where you maintain the data. When running the report you would then add code to the "On Format" event for the details section.
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Me.Notes.Visible = Me.[ShowNotes].Value
End Sub
Or, if you are going to the trouble of adding the field to the table, you could adjust your query below the report to show values in the "Notes" field only if the check-box is checked. (a "no code" solution).
I had the same issue with a report 'Invoice List' that displays the summary info about all invoices, and I wanted to have a button on each line to open the report 'Invoice' that displays the full details of just that invoice. In the event properties of the button I added an OnClick event (using the macro builder). This event is OpenReport 'Invoice' and includes a filter with the following condition;
[Invoice_Data]![Invoice_No]=[Reports].[Invoice List].[Invoice_List_Invoice_No].[Value]
The report 'Invoice' has got a query added to its Record Source (Data Tab) property called 'Invoice_Data' which pulls the invoice data from the various tables, the above filter condition then just tells it which individual Invoice No to use when running this query.

"Run time error 3167: Record is deleted" when unchecking a selection in multi select listbox

I have a form that contains a multi-select list box, a user can check off as many selections as required, then hit the "save" button, which saves the form fields, and the checked off selections in their respective tables.
Everything works perfectly fine, until a user unchecks a selection that had previously been saved.
I have tried several things, including refreshing the form, and the recordset, to no avail.
While testing to find a solution, I commented out every single line of VBA code tied to the form's save button. When clicking this button, obviously nothing happens with the code commented out, but if I try and close the form with the window's "x" button, I then get a message box stating "Record has been deleted"
I have no idea where to look at this point, I've tried to compact and repair database, also with negative results.
I have no code to post, as it doesn't matter what code I try to run, I get the error, a completely empty sub still gives me the error.
Again, this only happens when a user unchecks a selection that previously had been checked off.
Here is a screen shot of the form:
The list box in red is the culprit.
A user can edit every single field on the form and it all works fine, a user can even "add" selections from the listbox by checking items off, and it will save them, and show them the next time the record is viewed.
The problem occurs when a user unchecks one of the selections that were previously checked off.
Details of the listbox:
It is a multi select list box populated by a "lookup" that was created with the listbox wizard
The values selected are saved in a field as a comma separated list
The field itself is a lookup of another table, that allows multiple values
At this point I'm not even sure I'm explaining myself properly, I've gone so far down the "rabbit hole"!
If any clarifying statements are needed please ask away.
The following describes my implementation of what I think you were trying to do, but there may be some variations. The key point is I was able to reproduce your 'Deleted record' error on a regular basis, but somehow finally got it to stop.
I created a table named 'res_area' with an ID field and an 'area'. I populated with rows for '1E, 1F, 1I, 1J, 3C, 3D, 3E, 3F, 3I, 3J, 3K, etc.'
I created table 'res_tow' with all the fields you show on your form. I included a field named 'area' that is a LOOKUP field with the following source: "SELECT [res_Area].[TTID], [res_Area].[Area] FROM [res_Area] ORDER BY [Area];". Allow Multiple Values = yes.
I created form 'frmEditTow' with the Record Source:
SELECT res_Tow.TowID, res_Tow.TCompany, res_Tow.TPhone1, res_Tow.TPhone2,
res_Tow.TPhone3, res_Tow.TType, res_Tow.TTown, res_Tow.TAddress,
res_Tow.TFileName, res_Tow.TComments, res_Tow.TChecks, res_Tow.area
FROM res_Tow;
I added the 'Save Record' button with code to: 'DoCmd.RunCommand acCmdSaveRecord' and 'Me.Requery'
I am able to add or delete any combination of list items and save the changes.
For what its worth, I think my earlier version of the rowsource for the form included field 'area' and 'area.value'. With that, the form recordcount reflected the total number of listbox items selected - not the number of rows in table 'res_Tow'.
Good luck!

Access 2010 - combobox - unbound form - displaying data from an existing record

The problem I am having is displaying the correct information in a combobox on an unbound form when a users views/edits data.
The table that populates the combobox:
tblLocation
idLocation
Location
Location Description
In the tblPerson table, there is a FK field called idLocation. I have a form that allows a user to pick a person from a listbox and displays the information in textboxes and comboboxes.
The combobox is setup with these items:
idLocation <--- column width set to 0
Location
The problem I am having is having the data show up correctly in the comobox when I view/edit a new person.
When a person is selected from a ListBox, the information from tblPerson should display in textboxes and comboboxes. The textboxes work just fine. However, I'm struggling with the comboboxes. Keep in mind all of the fields
My research finds only two methods on solving this problem:
DLOOKUP
Manual check and set
If I use the DLOOKUP method:
cmbLocation = (DLookup("Location", "tblLocation", "idLocation=" & .Fields("idLocation")))
The problem is that msgBox cmbLocation will display the text and not the FK. If the user tries to edit the data, but makes no changes, it will try to save the text and not the FK.
I found a manual way that does work, but I'm not sure it is the best approach:
For i = 0 To (cmbLocation.ListCount - 1)
If Val(cmbLocation.Column(0, i)) = Val(.Fields("idLocation").Value) Then
cmbLocation = cmbLocation.ItemData(i)
Exit For
End If
Next
Again, this works - but I have to think that I'm doing something wrong - probably something obvious.
Is there a better way to do this?
you can dynamically change which data is displayed in a combobox. in your scenario, i suggest you use the OnClick event of the listbox (once the person is selected). Add the following code:
YourComboBoxName.RowSource = "SELECT * FROM tblLocation WHERE idLocation=" & FK
If after clicking on a person, the data doesn't change in the combobox, you may need a Me.Refresh
Base the form on a query that left joins in the location column.
Then that field when bound to a text box will will display automatic and without any code. And better is if the actual location value changes, then no update code etc. is required. In fact this is the whole beauty of a relational database!

How to use a query as a source for a report in MS Access 2007?

I did the following in MS Access: I made a form which had a combo box and a button. You select an option from there and click on the button and it is supposed to open a report. Now, I wrote a query selecting a few fields from a table and in the where clause, gave the condition as where name=str(combo1.value) and the report source was specified as this query. Now, when I select the value and click on the button, it opens a blank report. How can I make it load only those particular values?
I am not saving the combo box value anywhere. It said that it would remember the value for later use. Am I doing the right thing by not saving it? What should I do to make this work? Please help me!
Edit: The combo box is using values from a column 1 in a table X. I've not bound the value to any field and am using the "Remember the value for later use" option provided. The combo box is essentially a list of hotels and the report is a list of people staying at the selected hotel. When I put the ID of the field (as defined in the X), it works. But the thing is, it should refer to the names of the hotels and not the ID, which I am supposed to enter in a popup that asks for it. What do I do?
Edit 2: The query is as follows:
SELECT Table_1.Hotel_Name, Table_2.Name_of_Delegate, Table_2.Address, Table_2.City, Table_2.Center, Table_2.Spouse_Present, Table_2.No_of_Children, Table_2.No_of_Guests, Table_2.No_of_Rooms
FROM Table_1 INNER JOIN Table_2 ON Table_1.ID=Table_2.Hotel_of_Residence
WHERE Table_1.Hotel_Name=FormName.Combo7.Text;
When I click on the button (which opens the report), it asks for the name of the hotel in a popup box. How can I avoid this? What I am doing wrong?
You can use a WhereCondition with the DoCmd.OpenReport Method as a "dynamic WHERE clause" for your report's record source. Use something like this in the click event of the command button which opens your report.
DoCmd.OpenReport "YourReport", , , "[name]=" & Me.combo1
Remove the WHERE clause you added, where name=str(combo1.value), from the report's query.
I surrounded name with square brackets because name is a reserved word. See Problem names and reserved words in Access
Edit: In a comment, you said this about the combo box:
"Row Source is SELECT [Table_Name].[ID], [Table_Name].[Name] FROM [Table_Name];. Bound Column is 1 (which I assume shows the names I wish to be displayed in the combobox.)"
When you refer to the value of a combo box, that value is the value of the "Bound Column". So in your case, the bound column is 1, which means the combo value will be [Table_Name].[ID]. However, you want to open your report based on the [Name] column of the combo. So change the bound column of the combo from 1 to 2.
To open a report using the value of your combobox, in your report query you need to do the following:
SELECT fields
FROM table
WHERE YourValue = [Form]![FormName]![ComboBox Value]
You have to specify the name of the Form, plus the value so the report query knows the value. Hope this helps.