As keyword SQL VBA - ms-access

I was looking at SQL statement for an example database which went something like this'
SELECT INDEX, SUM AS Total FROM INVD WHERE INV=[Forms]![ENTRY].[INV]
What does AS mean on the code above?
Also is me.Entry same as [Forms]![ENTRY]

The key word AS indcates, that you want to use an alias for your column name.
SELECT Sales FROM Table1 'The column name in the output query will be Sales
SELECT Sales As Money FROM Table1 'The column name in the output query will be Money
To your second question it is not the same. If Form1 is the active form and you use me.ID it will be the same like Form1!ID. Same when Form2 is the active form and you use me.ID it will be Form2!ID. So .me refers always to the active form. But when you use Forms!Form2!ID it will always look for Form2 even when Form1 is the active form.

Related

MS Access Identical Comboboxes for Autofill

I have two tables:
'tableStudent' - a list of students given an ID number by the table, with following columns:
student_ID
last_name
first_name
and 'tableProject' which gives each project an ID and will be used to store information about the students involved in the project. Students will work in pairs on the project. This table includes the following columns:
project_ID
project_title
student1_ID
student2_ID
The columns student1_ID and student2_ID are combo-boxes that link student_ID to the student names.
I want to create a form that can be used to record students involved in a project. I want to be able to select the student IDs and have the student names autofill on a form.
I can make a form that autofills, but only with one student with this SQL:
SELECT tableProject.project_ID, tableProject.project_title, tableProject.student1_ID, tableStudent.last_name, tableStudent.first_name
FROM tableStudent INNER JOIN tableProject ON (tableStudent.student_ID = tableProject.student2_ID) AND (tableStudent.student_ID = tableProject.student1_ID);
When I add Student 2, I get a duplicate error on the student names. How do I indicate that the two names belong to different students? This is the code I'm generating:
SELECT tableProject.project_ID, tableProject.project_title, tableProject.student1_ID, tableStudent.last_name, tableStudent.first_name, tableProject.student2_ID, tableStudent.last_name, tableStudent.first_name
FROM tableStudent INNER JOIN tableProject ON (tableStudent.student_ID = tableProject.student2_ID) AND (tableStudent.student_ID = tableProject.student1_ID);
Any help appreciated!
Options for displaying related info from lookup table:
Use Access query designer to build form RecordSource. Pull tableStudent into query designer. In your case pull in twice - second instance will get named like tableStudent_1. Join each to one of the student fields. Bind textboxes to both sets of lookup table fields and set them as Locked Yes to prevent edit.
Don't include lookup table in form RecordSource at all. Include all student info in combobox RowSource. Expressions in textboxes refer to combobox columns by index, index begins with 0: =[comboboxName].Column(1)
DLookup() domain aggregate function, however, since your tables have a relationship, this is an inefficient method.
For all options, set textboxes with TabStop No.

How to do a lookup or index/match in Access?

I'm new to access and I'm struggling on how to use lookups since vlookup is not available in Access. I want to get the values that correspond to a certain ID/tagging.
I have 2 tables.
Table A contains values let's say Product Number, Product Type, Price, Remaining Stock #, Product Type+Product Number Tag. Let's say product number is not unique but combining it with its product type, it will be unique so I created that tag.
Table B contains Seller's Name, # of items Sold, Product Number, Product Type, Product Type+Product Number Tag.
Now using Table A and Table B, how can I create a query/table that will allow me to use that "Product Number + Product Type" Tag when I try to get the price of that certain item so that I can get the total revenue of each seller.
I hope you understand what I'm trying to say. What I just want to do is I want to use this "Type+Number" Tag as a reference point in getting data of that respective item when I try to create queries/tables. It's just like an INDEX/MATCH in Excel. But how to do it in Access?
Please tell me if it's unclear.
Thank you!
You need to join your two tables based on the relationship between the Product Number & Product Type fields in both tables.
The two fields in Table A should be marked as the composite Primary Key (select both fields and in the Design ribbon click the Primary Key icon).
In Table B they will be Foreign Keys - a seller could sell those products more than once, so duplicates are allowed here.
You don't need the Product Type+Product Number Tag field.
The SQL for your query would then be:
SELECT *
FROM [Table A] LEFT JOIN [Table B] ON [Table A].[Product Number] = [Table B].[Product Number] AND
[Table A].[Product Type] = [Table B].[Product Type]
This will return all records from Table A and only those records from Table B that match the Primary Key.
Finally.... don't think of an Access table as an Excel spreadsheet. Access is all about the relationship between pieces of data - for a start queries can be expressed in plain English a lot easier.
E.g return all records from table B where seller name is "Dave" and date is between 1st Jan and 31st Jan would be written as:
SELECT *
FROM [Table B]
WHERE [Seller Name]='Dave' AND
[Sale Date] Between #01/01/2018# AND #01/31/2018#
(SQL only deals in US date format).
You can use DlookUp Function.
It can be used on queries as a calculated field, or in code in VBA:
For example, to get the price of a PRoduct, you could use something like:
DlookUp("[Price]";"Table A";"[Product Type+Product Number Tag]='" & Value & "'")
From my point of view, the complex part of DlookUp is the third argument, the WHERE clausule. If you have any experience with SQL, you will have no problem. If you don't, don't worry, just read some info and if you get stuck, come here to SO
You can use DlookUp to get any value of any field, based on a criteria (criteria applied to a unique field, ofc).
And yes, you can use it to get values from tables or from queries. In the link I provided before, it explains how the arguments works.
The most complex part is the criteria part. You must write as if you were typing a WHERE clausule on SQL more info here
About the criteria, always remember this:
If your criteria is a numeric value, then just type [field_criteria]=my_numeric_criteria
If your criteria is a text value, you must use single quotes. For example, [field_criteria]='my_text_criteria'
SQL requires single quotes around text values.
Try it!

How do you use the value in one field to pull a corresponding value in another field?

I have a table that looks something like this:
The field column is a catch-all for custom fields and is linked properly to other tables. What I need to do is to search through and exclude any Trans ID's that have a Trans Completed Field. So in this instance it would filter out A00001 and A00003.
I know I can use record select and remove those Trans ID's, but now my data shows each of the entries in the field column when really I'm just trying to exclude any Trans Id that has an entry for Trans Completed, and give me one entry for each Trans ID.
Is there a formula that I can use to filter the loan? I don't have access to edit the database directly to pull the data into a new table. I've tried subreports in crystal but it pulls multiple entries.
---Updated 10/12/15---
So in the end I want it to look something like this.
you could
create a formula if {field} ] "Trans Completed" then 1 else 0
create a group on TRANS ID
in GROUP Selection expert enter
sum({field},{TRANS ID}) = 0

Access 2013 - DlookUp

I now have tried DLookUp, but I do not get the result I want.
My Situation: I am creating a database for a cinema. It includes tables named cinemas, movies, sold seats, price.
I have made a form where I can record a new "guest". I can select the ID of the Show and then I see the date and the time. I now have recorded some guests and now there should be less seats free. I want to show that on the same form as the one where I record new guests. I don't get up with the Dlookup.
I have made this query which calculates how many seats are left but I cannot put that into the form.
SELECT
tbl_Vorstellung.ID_Vorstellung,
tbl_Kino.Kino,
Sum([tbl_Kino]![Sitzplätze]-[qry(02)PlätzeVerkauft]![AnzahlvonTicketNummer]) AS Ausdr1
FROM
tbl_Kino
INNER JOIN ([qry(02)PlätzeVerkauft]
INNER JOIN tbl_Vorstellung
ON [qry(02)PlätzeVerkauft].ID_Vorstellung = tbl_Vorstellung.ID_Vorstellung)
ON tbl_Kino.ID_KINO = tbl_Vorstellung.FS_KINO
GROUP BY
tbl_Vorstellung.ID_Vorstellung,
tbl_Kino.Kino;
Save your working query as a new name, for this example MyQuery
Then use
DLOOKUP("Ausdr1", "MyQuery", "ID_Vorstellung=SomeValue AND Kino=SomeValue")
On your form. You'll have to put in the SomeValue values based on fields on your form, etc.

Microsoft Access question

I have a combo box which has 3 pieces of information
COMBO BOX 210: Materials ID, Name of Product, Cost, Description
After I update the combo box the cost figure is stored in another field in the table, so far so good, so in selecting "Apples" the cost "$1" stores.
Now, what I want to is have a different field where the description of the apple (the text) is stored yet I only update the combo box once, in other words after updating the combo box Field 1 stores the price and Field 2 the description of the apple.
I will offer you a different example which I hope is similar to what you're asking.
My form includes a combo named cboNames. This is the query for its row source:
SELECT f.id, f.fname, f.date_added
FROM food_test AS f
ORDER BY f.fname, f.date_added;
In the combo's After Update event I can access the values of those 3 columns in the selected combo row by referring to the Column index. Notice the column index numbering starts with zero.
Private Sub cboNames_AfterUpdate()
Debug.Print Me.cboNames.Column(0)
Debug.Print Me.cboNames.Column(1)
Debug.Print Me.cboNames.Column(2)
End Sub
So if I wanted to put the date_added value into another data control, I could add this to the combo's After Update event.
Me.SomeOtherControl = Me.cboNames.Colummn(2)
However I wouldn't actually store both id and date_added in a row of another table. In my food table, each id is associated with a unique combination of fname and date_added. So I would store only the id, and use a SELECT with a JOIN to the food table to look up the associated fname and date_added values whenever I needed them.