I have a Form for order information. It will populate the fields order date, product info, Customer ID... after selecting the Order number from a combo box on the form(Source for all this information is OrderInfo table). I have a separate table for CustomerInfo which has Customer ID, name, address... There are four Customer ID fields on the form. I want to automatically display the name, address... based on Customer ID field.
I did a query for the first customer to look for the customer ID value on the form and find the name & address. But I do not want to write four queries for the four customer ID fields. Is there any way to combine all four into one or is there a way better to do this process?
As June7 commented, eight lines of DLookup code might be the quickest solution.
I don't personally like to use DLookup, and I'm sure their is a more elegant solution than what I'm suggesting, but below is what I tried (and I assumed unbound textboxes).
Create a private subroutine on the form:
Private Sub PopulateNameAddress(CustomerID As Long, CustomerNameTextBox As TextBox, CustomerAddressTextBox As TextBox)
'CustomerNameTextBox is a variable for the names of the text boxes containing the customer's name
'CustomerAddressTextBox is a variable for the names of the text boxes containing the customer's address
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT CustomerName, CustomerAddress FROM CustomerInfo WHERE CustomerID=" & CustomerID)
CustomerNameTextBox = rs!CustomerName
CustomerAddressTextBox = rs!CustomerAddress
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub
Then after whatever populates the CustomerID (as shown in the image you provided), call this subroutine, and give it the CustomerID, the name of textbox for the customer's name, and the name of the textbox for the customer's address. Could be better running a loop, but since you only have four records to fill, this might be the most straight forward:
'Send the PopulateNameAddress subroutine the name of each text box containing the CustomerID, CustomerName, and CustomerAddress
PopulateNameAddress Customer1IDTextBox, Customer1NameTextBox, Customer1AddressTextBox
PopulateNameAddress Customer2IDTextBox, Customer2NameTextBox, Customer2AddressTextBox
PopulateNameAddress Customer3IDTextBox, Customer3NameTextBox, Customer3AddressTextBox
PopulateNameAddress Customer4IDTextBox, Customer4NameTextBox, Customer4AddressTextBox
Unfortunately, I'm not sure how you have your database setup, so this may not necessarily work. Let me know.
Related
I'm trying to create a solution to fetch data for a new record of SmallDB from secondary BigDB.
I created a linked table of BigDB to have it on SmallDB, so later I could use OnClick event of Button and it would be easier to reach that data.
Situation: I search form for the PersonID, but get negative result (this particular PersonID doesn't exist).
Then, in the form I create new record and enter this new Person ID in the PersonID field.
My plan is to be able to click FetchData button and pre-fill Name, Last Name and other details to the form from BigDB for this particular PersonID.
Then I would save the record with save button.
It looks to me, that solution could be similar to: https://stackoverflow.com/a/53180365
But I don't know how to ensure that I would get data from BigDB only for that particular PersonID that I entered into Form field.
Or perhaps in my situation another method would work better?
Thank you!
Following example code from link referenced in question, apply filter criteria to query object via PARAMETERS. However, unless you have query object set up with PARAMETERS clause, I don't see need for QueryDefs and instead of referencing query object, can use SQL statement in code.
Assuming PersonID is a text type field:
Dim db as Database
Dim rs As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT * FROM tablename WHERE PersonID='" & Me.tbxPID & "'")
If Not (rs.EOF And rs.BOF) Then
If you are not on new record row of form, either first move to it manually or with code, or use another approach. An INSERT SELECT action SQL can create a new record in SmallDB with data from BigDB.
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?
I have a form in access with several fields.
now, lets say a I have a table called TEAMS, which contains team's size, name and id.
now, I have a form with a select box, which shows the teams names, and under the select box I want to have a text field which shows the team's size of each team, dependes what you choosed on the select box.
for example: I you select team A, with 10 members, you'll se 10 in the text field, if you changed it to team B, with 8 members now the text field will show you 8.
If I had to do it with PHP and SQL I would do something like:
SELECT team_size FROM TEAMS WHERE team_id = my_form.team_select_box
and then print those results with JS on the html form... but I am new to ms access and I have no idea how to it...
any help please?
You can add an AfterUpdate() event to you drop down. Your dropdown needs to have two columns. One for the ID and one for the team name. Within the AfterUpdate event you can execute your sql to identify the team size as you described in your question. Then set the value of the text box to the result.
This code will work for an AfterUpdate procedure in form with a dropdown called scltTeam and a text box called txtSize.
Private Sub slctTeam_AfterUpdate()
Dim rst As DAO.Recordset
Dim strSQL As String
Dim id As Integer
id = Me!slctTeam.Value
strSQL = "SELECT * FROM Team WHERE ID = " & id
Set rst = CurrentDb.OpenRecordset(strSQL)
Me!txtSize.Value = rst!Size
End Sub
We have an Access database to track work requests. We have a form where we enter the info for the requests. We wish to limit the departments which can be entered to a list, which we have entered in a Table called Departments.
The Departments control on our work request entry form is a combo box based on the Departments table. This works for limiting the departments entered by hand; however, jobs are often entered by copying and pasting old info as a whole record, and changing any information as necessary. When this occurs, the Department control is not limited by the combo box.
I need a method to validate the data entered in the Department control on the form against the entries in the Department field before the record is saved to the table. If the Department from the pasted entry does not match any of the records in the Departments table, I would like it to throw a message box.
I have attempted to use the BeforeUpdate event procedure, but can't figure out how to evaluate the current Department entry in my form against the entries in my Departments table. Can anyone suggest how to create this validation?
Something like:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strSQL As String
Dim RS As DAO.Recordset
strSQL = "select * from Table1 where (Flda) = '" & Me.txtFldA & "';"
Set RS = CurrentDb.OpenRecordset(strSQL)
If RS.EOF Then
MsgBox "Not a valid Department", vbOKOnly, "Incorrect Department Entered"
End If
RS.Close
Set RS = Nothing
End Sub
Since you are running Access 2010 you could enforce the validation at the table level by using a Before Change data macro like this:
For more information, see
Create a data macro
I think what you want to do is set up a relationship between your Departments field and the corresponding field in the Departments database. Enforce Referrential Integrity (without cascading), and this should prevent anyone from adding a record in your Work Requests table where the department doesn't exist in your Departments table.
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.