Access - input value dependes on a select box - ms-access

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

Related

Combine multiple queries using same fields and table

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.

Coding a button on a form in MS Access

I'm trying to build a database with MS Access. I have two tables- StockFrames and Projects, and I have a form- FrameCheckOut. On the form I have a FrameID field (where we will type in a frame id number or scan its barcode) and a ProjectName field, with a drop down of project names from the Projects table. I also have a button- Assign Frame. I want the button to update the StockFrames table with the projectID number so that I can know whether or not a frame is currently in use (or "checked out") to a project.
I have tried assigning this code to the button On Click:
UPDATE StockFrames
SET StockFrames.projectID = [SELECT Projects.projectID
FROM Projects WHERE Projects.projectName LIKE projectName]
WHERE frameID = frameID;
.. but that code contains invalid syntax. I am very new to Access and coding and I would really appreciate some help if anyone is willing.
Include key field in combobox (column can be hidden) RowSource. Value of combobox will be ID but users see and type name. Combobox properties:
RowSource: SELECT ProjectID, ProjectName FROM Projects ORDER BY ProjectName;
BoundColumn: 1
ColumnCount: 2
ColumnWidths: 0";2"
ControlSource: leave blank if control is used to enter search criteria, otherwise field you want to save into
If form is bound to StockFrames, an UPDATE action is not needed. Find record for specific FrameID and simply select project from combobox.
If you prefer to use UPDATE, then have UNBOUND comboboxes for Frames and Projects designed as described above. Example VBA:
Private Sub AssignFrame_Click()
CurrentDb.Execute "UPDATE StockFrames SET ProjectID = " & Me.cbxProject & _
" WHERE FrameID = " & Me.cbxFrame
End Sub

How to Prefill form from another DB based on the field value

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.

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.

Tagging Records in a Continuous Form

In a continuous subform, I display records based on a DISTINCT query. Because it's distinct, each row does not include a record ID.
Does anyone know of a way to add a checkbox (or similar), so that a user can select any of the records, which will then be used to create new records via code?
I prefer to use a subform to a list, as it features lots of column sorting and filtering functions.
MTIA
Depending on what you need to create the records, something like this sample may suit:
Function DisplaySelectedCompanyNames()
Dim i As Long
Dim F As Form
Dim RS As Recordset
'' Get the form and its recordset.
Set F = Forms![Customers1]
Set RS = F.RecordsetClone
'' Move to the first record in the recordset.
RS.MoveFirst
'' Move to the first selected record.
RS.Move F.SelTop - 1
'' Enumerate the list of selected records presenting
'' the CompanyName field in a message box.
For i = 1 To F.SelHeight
MsgBox RS![CompanyName]
RS.MoveNext
Next i
End Function
Further information: http://support.microsoft.com/kb/208502
FYI, I decided to use the Windows ListView OCX control, as it offers the ability to add a checkbox for each row.