Microsoft Access "if" statement - ms-access

A very specific question - wondering if someone would simply be able to put together the code for me! I have no experience with code... at all.
I have a table consisting completely of associate information (Name, Phone, ID, Position, etc). I'm building a report on which I only want the Name to display, but ONLY IF their Position = "Tech"
I've come to find this formula for displaying their names in the "last name, first name" format:
=[LastName] & ", " & [FirstName]
but as I said... I only want this script to run if they're a "Tech" how would I write out the "IF" statement to make this work?

The best place for that would be in the query - add "WHERE Position = 'Tech'". Assuming you only want Techs to show up on the report.
If you want everyone to show up but only want names to be visible for techs, you can use an IIf statement: = IIf(Position = "Tech", [LastName] & ", " & [FirstName], "")
That will show the name for a tech, or a blank for anyone else.

Related

Create new lines in a database based on a condition

I have an issue for which I cannot seem to find an answer (I am new to this and clearly I do not know how to create an append query properly...)
I have a table "Basket" in which I create new lines each week on a form.
Each time I create a new "basket", I want my "follow basket" table to be incremented with a new line, attached for each customer following the proper conditions (of my table "customers").
So I need a query (i think) that copy the data from "basket" in the "follow basket" for each "customer" where its conditions match the "basket"'s.
Do you see what I mean ?
Example :
I create basket "Type P" for week S1
It creates week S1 basket in "follow basket" for each customer who subscribed for basket "Type P".
In "follow basket" I can then say if they had their basket delivered and paid.
Thank you for your help...
Here is my database structure as asked
Panier : basket
suivi_paiement_panier : follow basket
Particuliers : customers
I also have a "semainier" field (weeks) because we create specific baskets each week and all the follow up is made like that (S1, S2, ... = Week1, week2...)
You want to 'batch create' a set of new records based on new data as well as existing data. An INSERT SELECT SQL statement is commonly used to accomplish that.
Multi-value field Type_de_panier complicates but it is manageable.
Another complication is referencing form controls to pull criteria parameters because likely form/subform arrangement is involved in data entry and parameters are sourced from different forms. If you have Samainier as main (parent) form and Panier as subform and code is behind Panier form, consider:
DoCmd.RunCommand acCmdSaveRecord
CurrentDb.Execute "INSERT INTO Suivi_paiement_paniers(IDDate, IDT1) " & _
"SELECT " & Me.Parent!Semainier_IDDate & " AS SID, IDT1 FROM Particuliers " & _
"WHERE Fréquence='" & Me.Parent!Paire_Impaire & "' AND Actif=True " & _
"AND IDT1 IN " & _
"(SELECT IDT1 FROM Particuliers WHERE Type_de_panier.Value='" & Me!Type_Panier & "')"
Make adjustments if your form arrangement is different.
Now have to figure out what event to put code into. If you want to rely on user to initiate this action, then use a button Click.

Dynamic Query in a MS-Access form

I have a form in Microsoft Access (2016) where I have a dropdown list named KID. When the dropdown list looses Focus I want to run a query that would look like this:
SELECT max(Cnt)
FROM Table
WHERE KID = ValueOfDropdownList
The result of this query should be usable in a variable in the VB Code of the Event.
Unfortuantly I am restricted to Access at the Moment. Am I even using Access the right way (Choose from dropdown in form -> Event -> SQL Query in VB -> Display in form) or are there better ways to do that?
Thanks for your help in advance!
Firstly I would use the AfterUpdate event of the combo box, otherwise it will fire even if someone just tabs through the control.
Secondly if this is just to set a variable you would probably be better to use a Domain function in this case a DMax()
iVariable = DMax("Cnt","Table","[KID] = " & Me.YourComboControlName )
This assumes KID is a number field. If KID is text you would need to delimit the criteria like this;
iVariable = DMax("Cnt","Table","[KID] = '" & Me.YourComboControlName & "'" )
Lastly if you want this to update as you scroll through records you would need to add the same code to the OnCurrent event of the form. That fires when the record changes.
If it is for display purposes you can't use the same technique on a continuous form if this is setting an Unbound control, you would need to pull this in to the underlying forms recordset.
You can use DMax:
YourVariable = DMax("[Cnt]", "[Table]", "[KID] = " & Me!DropdownListName.Value & "")
In case KID is text, apply quotes:
YourVariable = DMax("[Cnt]", "[Table]", "[KID] = '" & Me!DropdownListName.Value & "'")
FYI there is another option. As the data source of your combo,
use a query returning 2 columns, like select KID, max(cnt) from table1 group by KID.
Then in your text box, retrieve your value using =combo1.columns(1).
For this to work you need to specify in the combo properties that it has 2 columns. In the formula, don't forget that those combo columns are 0 based.
This is the most efficient solution when you need to retrieve several values linked to the combo (and you don't have millions of records).

Function DLookup in Ms Access 2016

gives me
when saving records in the system.
Can anyone let me know what is the problem in this?
xx = DLookup("[part_number]", "tblModelCost", "[Customer_ID]= " & me.Customer_ID & " and [Model] = '" & me.[txtModel] & "' and [Description] = 'screen'")
Looks like Me.Customer_Id should be Me!Customer_Id. However, that depends on the form control name and what you are trying to do. Assuming you have a control named "Customer_ID" and a field named "Customer_ID" Me.Customer_ID will try to use the the control value, me!Customer_ID will try to use the underlying bound field value. If you don't have a control named Customer_ID then using Me.Customer_ID will cause the error you are getting. (This is why you want to learn to use controls names like txtCustomer_ID for your controls. )

MS-Access using Dlookup to pull results from query into textbox on form

I am very new to access and I am using the Dlookup function to pull the results from a query into a text box to appear on a form.
I have the following expression in the Data Control Source field for the text box and it is pulling in the very first result from the query.
=DLookUp("SumOfSales","qTtlSalesbyComp","Company = Company")
I need it to act as a vlookup and match the Company in the query to the Company in the Company text box and pull the SumofSales that corresponds. Any hep would be greatly appreciated.
If Company is text:
=DLookUp("SumOfSales","qTtlSalesbyComp","Company = '" & Company & "'")
If it is numerical:
=DLookUp("SumOfSales","qTtlSalesbyComp","Company = " & Company )
I just could not seem to get the Dlookup to work. I ended up using subforms to pull in the query results. I believe this ended up being a much cleaner way of executing what I wanted.

Query by Form for multiple fields in Access 2010

I want to create a query which takes field parameters through a form. For this, I created a form with combo boxes and drop down options to select the values from, this populates a text value in the respective invisible text fields whose default value I have set to null. Now in my query I give criteria for column as iif(isNull([Forms]![Conditions]![text_on_form]), [column_in_table], [Forms]![Conditions]![text_on_form]). I have done this for all the columns on which the where clause comes from the form. I have tried running this. The results seem to be random. It worked for three columns, but when I played around with it, it was giving me empty result set. Can anyone tell me what I am doing wrong? Or if there is a better way to implement query by form in Access.
It sounds like you are trying to create dynamic SQL. Here is the method in vba I generally prefer:
Dim SQL As String
SQL = "SELECT tblName.* From tblName WHERE (1=1)"
If Not IsNull(Me.combo1) Then
SQL = SQL & " And ([Field1] Like ""*" & Me.combo1 & "*"")" ' I am using like statements here, but that is because this is a search tool.
End If
If Not IsNull(Me.combo2) Then
SQL = SQL & " And ([Feild2] Like ""*" & Me.combo2 & "*"")"
End If
Docmd.RunSQL SQL
End Sub
Basically, add on to the SQL statement only if the user has put a value into your text box/ combo box or whatever. The "Where (1=1)" is to account for a situation where all fields are null.
Play with this concept to create your SQL statements. Avoid using invisible text boxes to store data, it generally means you are doing something wrong and will get mixed results (someone else on this forum can explain better than me why that is).
Just use the Like operator. Put this in your criteria field in the query Like "\*" & Forms![Form_Name]![Form_Field] & "\*" -- This tells it to get anything if the field is blank (or null) and matches whatever you have in the field. This may not be what you want. It should be noted that it will return anything with the text string in it. For example: if you type "the" it will return tether, these, theses, thermometer (anything with the word "the" in it. It works best for multi word or longer strings that can be matched more accurately, however it works for a search query because there is usually a set of human eyes looking for the result and erroneous results is not a huge problem.