Populating a form from a table - ms-access

I'm not sure if I used the right title for this question but I'll try to ask it. Be patient with me I'm new to Microsoft Access.
I'm making an MS-Access database for employee reviews at my company and I made the form and all of the data from the form is going to two seperate table. The tables are linked through the same rating ID for each time the form is filled.
I have a seperate form that when you select the name of the stakeholder reviewed it fills a listbox with the name of the leader who reviewed them, the date, and the rating ID. I used VB to make it to where if you select the rating ID then it will open the form in which the data was put into using DoCmd.OpenForm "FRM".
Now for the question I hope that was all understandable. I would like for the form that is brought up from selecting the rating ID to be repopulated with all of the information that was entered into the table for that specific ID. What do I need to do to do this?

Another option is to base the form on a table or query (its RecordSource) and use the Where clause argument when opening the form:
DoCmd.OpenForm "frmName", acNormal, , "[SomeID]=" & frmYours!ctlControlName
This has the advantage that the form can be opened separately, as it will just show all the data from its RecordSource, not producing an unwanted parameter request.
If SomeID is text then its value needs to be quoted with apostrophes:
DoCmd.OpenForm "frmName", acNormal, , "[SomeID]='" & frmYours!ctlControlName & "'"

There are various possible solutions to this.
The first one (and simplest) is to write a query in the RowSource property of your form with the appropriate data source, and include a filter field in that query that corresponds to the Id you are filtering:
select a.*
from [your table] as a
where a.[id] = forms!frmYourForm![theControlName]
That way, every time you open the form, it will show the appropriate data.
The second solution (a little more sophisticated) is to edit the RowSource property on run-time. In the code that opens the form, you can do something like this:
strSQL = "select a.* from [your table] as a where a.Id = " & theControlName.Value
DoCmd.OpenForm "theDetailedForm"
form_theDetailedForm.RowSource = strSQL
form_theDetailedForm.Requery
Hope this helps you

Related

Store filtered employees in training record

Good afternoon,
I have a form used to create a "toolbox talk". A toolbox talk is not always applicable to every employee but might be limited to a specific department, gender, or competency (or any combination of the three). I have created a subform which using unbound text boxes allows me to filter my list of employees based on the toolbox talk criteria - great!. See screenshot below:
However what I cant work out now is how I can then store the filtered results (list of employees) as recipients against the toolbox talk record.
I know that I could include a control to select employee ID/Names line by line as per the filtered results, however I was hoping that a quicker option could be created.
Can someone please suggest a possible solution?
Thank you in advance.
If you want to 'batch' create records, one approach is an INSERT SELECT action SQL that uses the same criteria selected by the form controls. If your code applies this criteria to the form Filter property, that property can be referenced to incorporate filter criteria for SQL. I prefer to use CurrentDb.Execute because then don't have to deal with RunSQL popup warnings.
Private Sub Command16_Click()
Dim mySql As String
mySql = "INSERT INTO tblEmployeeToolboxTalk (EmployeeID, ToolboxTalkID) " & _
"SELECT EmployeeID," & Me.ToolboxTalkID & " AS TTID FROM tblEmployees WHERE " & Me.Filter
CurrentDb.Execute mySql
End Sub
If you need guidance on conditionally building filter criteria in VBA, review http://allenbrowne.com/ser-62.html

Customize auto-complete functionality on Microsoft Access datasheet

I have a main form tied to a User record, with a subform tied to a number of Client objects the user "owns." So, there is a one-to-many relationship between User and Client.
As the subform exists, the user can add, remove, and edit entries in the Clients subform. When a user adds an entry to the subform datasheet, there is an autocomplete functionality that kicks in the user types part of a Client name that matches any names in the Client database, thus saving the user a few keystrokes and ensuring that the user enters a name that exactly matches one in the Client database.
One thing to note with the Clients table is that in addition to each Client having a unique numerical ID, each Client has a full company name (Test Agency, Inc.), a colloquial name (Test Agency) and an abbreviated name (TA).
I am trying to edit the subform so that the autocomplete functionality will match against any of the three fields listed above (full name, colloquial name, and abbreviated name). Right now, the autocomplete only works against the full name, as that is the field linked to the subform. I would like the user to be able to type in a part of a string, the subform to try and match it to any of the three fields (full name, colloquial name, abbreviated name) and return a list of potential matches to any of the three fields. When the user selects the correct potential match for the Client they are trying to search for, then then full company name would be entered into the datsheet. Basically, these additional fields just make it easier for the user to find the Client they are looking for (imagine typing in AMD instead of Advanced Micro Devices, Inc.)
My first question--is this possible to do with a simple datasheet? I've looked into using lookup fields and multi-value lookup fields, but I'm not sure this is the right method. Or will I need to build myself a custom control that does this matching on multiple fields?
Made a query like this
SELECT *
FROM Company
WHERE fullName LIKE '*' & pCompany & '*'
OR Colloquial LIKE '*' & pCompany & '*'
OR Abbr LIKE '*' & pCompany & '*'
and on my form I did this
Private Sub cboCompany_KeyUp(KeyCode As Integer, Shift As Integer)
ClearCombo cboCompany
Dim sql As String
Dim rs As DAO.Recordset
Dim companySearch As DAO.QueryDef
Set companySearch = CurrentDb.QueryDefs("CompanySearch")
companySearch.Parameters("pCompany") = cboCompany.Text
Set rs = companySearch.OpenRecordset
Do While Not rs.EOF
cboCompany.AddItem rs("ID") & ";" & rs("FullName") & ";" & rs("Colloquial") & ";" & rs("Abbr")
rs.MoveNext
Loop
End Sub
Private Sub ClearCombo(cbo)
For i = cbo.ListCount - 1 To 0 Step -1
cbo.RemoveItem i
Next i
End Sub
It's not super fast at all but it works. I think what would make it faster is not cuing off the KeyUp event and instead on a timer once users start typing in that field. Then turn the timer off when they stop typing or focus leaves the combobox.
So you are already able to search with partial string - probably by means of a query with a like condition.
The next step is very easy. Do the search for every field, combine them by UNION, and remove duplicates by means of a SELECT DISTINCT.
Hope this succinct answer will suffice?

Blank subform: Unable to find a record, based off multiple fields

I've looked around for an answer to this issue, but have fond none. Thank you in advance to anyone who's able to help. I'm trying to look up records and alter them, based off multiple fields. However, my form shows up blank.
I have a database with one to many links for the following tables:
Sample->Set->Catch->Length->Diet (Key fields: SampleID, SetID, etc.)
Preliminary data is entered. I have additional data for some individuals to be entered into the Length and Diet tables. So, I created a form with combo boxes that allow the user to navigate to the correct fish by selecting: Date, Station, Set, Species, and Length. So, when I select a date, I'm restricted to stations sampled on that day and so on. I have a query string set up to restrict results to those matching the criteria entered into the combo boxes. My subform is based off the final query in this string (Query 5). It's linked on the primary key field for the length table (LengthID). All fine so far.
The issue: When I open my form and select values for each combobox, the subform remains blank. However, I can run Query 5 from the sidebar at this point and it runs successfully. I could just enter data directly into the query, but it would be less streamlined and vulnerable to human error.
I've also tried opening my subform directly from the sidebar. When I do this, Access prompts me for the Date, Station, Set, Species, and Length. Twice. The form then shows up and all are fields blank including the LengthID field, which should be filled in (since I'm looking up an existing record). I don't know why it prompts me twice, but I think that the subform isn't showing up in regular form view because the database sees the LengthID field as blank.
My combo boxes appear to navigate correctly to a given record. The query string my combo boxes and subform are based on all work when run directly. But I can't enter data into my subform, presumably because the subform can't find the correct record even though the query it's based off of can find it just fine. I've run out of troubleshooting ideas, any advice is greatly appreciated. Thanks!
If I understand correctly, you're trying to achieve a query WHERE clause utilizing combo-boxes. There are many SO questions out there on this but this should get you going. Let me know if you run into trouble and I'll help you out.
I assume you have a linked parent/subform combo. This code would be on the parent form search button:
Dim strSQL As String
strSQL = " 1=1 "
If Not IsNull(cmbComboBox1) Then
strSQL = strSQL & " AND Field1 = " & cmbComboBox1
End If
If Not IsNull(cmbComboBox2) Then
strSQL = strSQL & " AND Field2 = " & cmbComboBox2
End If
If Not IsNull(cmbComboBox3) Then
strSQL = strSQL & " AND Field3 = " & cmbComboBox3
End If
Me.Filter = strSQL
Me.FilterOn = True

Can I use criteria from any current form in a single query in Access 2003

I have a report (ReportX) that I wish to open from two different forms (FormA and FormB) in my database. I wish to do this because FormA and FormB address different aspects of my data, even if they ultimately get to the same output. ReportX is based on data from QueryX.
The problem I have is that QueryX would ideally filter the data based on the current RecordID in the current form. But I don't know how to accomplish this. I'd like to design QueryX so that the criteria for RecordID is essentially CurrentForm!RecordID, but research suggests that I cannot do this. Must I make separate but otherwise identical queries and reports for each form? Or is there a way to use VBA to define the query criteria when I click on the OpenReportX command button?
I already tried using the WHERE condition in the OpenReport command:
DoCmd.OpenReport "ReportX", acViewPreview, ,"RecordID = " & RecordID
but that did not display the results I wished. I need the report header to display/print for each RecordID and the page count in the page footer to reflect only the current/total pages of the RecordID in question. (In other words, if record 1 is one page, record 2 is two pages and record 3 is three pages, then ReportX, when displaying the first page of record 2, should say "Page 1 of 2" and not "Page 2 of 6.") So being able to display and print a single record properly using record filters would also solve my problem.
Which is the least cumbersome/most possible solution?
You should be able to accomplish this using the WHERE condition argument when you open a report:
DoCmd.OpenReport "rptName", acViewPreview, ,"RecordID = " & Me!RecordID
In the case where a I need more control over a Report's recordsource than what the Where condition argument can do for me, I will set the Reports RecordSource to be blank (after designing the report). Next I write code create the correct SQL statement in the Open Report button's Click event, followed by code to open the report and pass in the SQL as an opening argument for the report.
Private Sub cmdOpenReport_Click()
Dim sSQL as string
sSQL = "SELECT * FROM tblWhatever WHERE RecordID = " & Me!RecordID
DoCmd.OpenReport "rptReportName", acViewPreview, , , ,sSQL
End Sub
Then in the report's Open event I write code to set the recordsource:
Private Sub Report_Open(Cancel As Integer)
If IsNull(Me.OpenArgs) = False Then
Me.RecordSource = Me.OpenArgs
End If
End Sub
If neither of those accomplish what you want then you have an issue of a different sort. It's a little difficult for me to understand why you need All Records to show up in the header but only one record in the detail area. And how you expect to accomplish this. You might be best off trying to write a query first that gives you the exact results that your looking for so you know that it can be done.
As a side note, I actually use very few saved queries in my designs. It's not that there's anything wrong with using them, as the option is there for your convenience. I frequently use raw SQL on both forms and reports and set the RecordSource to the SQL on the Form or Reports Open or Load events.
Yes you can point to form data items in a query, and subsequently use that query in a report. The forms need to be open before the report runs. As far as having a header for each record, that is controlled in the settings of the report and how it displays the data.
In the Field, or Critera you can use the format:
[Forms]![frm_Process_Candidates]![QuestionTemplate]
Where frm_Process_Candidates would be the name assigned to your form, and QuestionTemplate is either the name of a control on your form, or a field from the data source of your form.
If you have a sub-form, there will be another [Form] call in the middle there.
[Forms]![frm_Dropdown_Admin]![frm_Dropdown_Admin_Detail].[Form]![text22]
Access should figure it out from there.

Passing values in Access Forms

I have two tables and a form in a Microsoft Access Database. Lets the first table tblCUSTOMERS, and the second table tblINVOICES. tblCUSTOMERS contains fields called CustomerID (primary key), FirstName, and LastName. tblINVOICES contains fields called InvoiceID (primary key), CustomerID (foreign key), and Amount. The form is called frmInvoices and contains textboxes for the fields in tblINVOICES.
What I what to do is create functionality that allows me to search through my customers table, select a customer record from that table, and then return the CustomerID of that record to frmInvoices. Ideally the searching would be in a format like a data grid that allows searching by FirstName, LastName, and CustomerID.
Specifically can you advise me of the simplest way to:
1. Insert a form (lets call this new form frmCUSTOMERS) with something like a datagrid control to show customer records.
2. Update the datagrid on frmCUSTOMERS to display only records matching a query of tblCUSTOMER (such as only customers where firstname starts with 'B')
3. Pass the CustomerID from frmCUSTOMERS to frmINVOICES.
Many thanks,
Brett
You can pass a value as an opening argument to a form when you open it.
DoCmd.OpenForm "frmFormName", , , , , ,"B"
Or you can pass in a criteria statement:
DoCmd.OpenForm "frmFormName", , , "FirstName LIKE 'B*'"
If you go the first route you would do something like this:
Private Sub Form_Load()
Me.Filter = "FirstName LIKE '" & Nz(Me.OpenArgs, "") & "*'"
'or
Me.Subform1.Form.Filter = "FirstName LIKE '" & Nz(Me.OpenArgs, "") & "*'"
End Sub
To hand a value such as CustomerID back from the search form, there are numerous options. You could put the value in a global variable. Or you could have the search form give the value back to the calling form by using a Public Variable or Public Function on the calling form.
Arguably, a more correct and cleaner way of doing all this would be to design a Class that handles opening the search form and passing of the inbound and outbound values. Classes do take more knowledge and skill to write and it would probably be overkill for what you are trying to do. A class is certainly not required to make this work. It would only be a little cleaner. In reality, you would use many of the same techniques I listed above if you did write a class for this.