VBA - Access emailing - vba

I have a table of 1000 emails with unique identifiers and 5 other tables with matching unique identifiers. I am trying to email attachments from the first table the attachments from the remaining 5 table. one issue is that all information is store on the 5 tables. is there code I can write to "automate" the emailing without having to write thousands lines of code?

You did not provide sample data. I assume by 'emails' you mean email addresses.
Let's say the table with mail addresses is called 'accounts'. It is not clear why there are 5 other tables. Let's assume they are called a, b, c, d and e. The information in these tables is stored in field 'info'. You did not provide information on the type of database (access, mysql, postgres, oracle, ms-sql), so I am going to use as generic SQL as possible. Let's assume the id field in accounts is called 'id' and the corresponding field in the other tables is called 'accountid'. (Edit:)Let's assume the other tables do not all reference all 1000 accounts.
select accounts.email, a.info, b.info, c.info, d.info, e.info
from accounts left join a on accounts.id=a.accountid
left join b on accounts.id=b.accountid
left join c on and accounts.id=c.accountid
left join d on account.id=d.accountid
left join e on account.id=e.accountid;
Note that for some accounts some of the info fields may be empty (NULL) if the corresponding table does not have a row referencing the account.

Related

Comparing 3 tables in an Access Database

I am trying to compare a series of tables within an access database, 2 local and one linked.
Table A (local) contains UserID, Title, Position; Table B (linked) contains UserID, Title, and Position from the previous week (records could possibly change on a week to week basis); Table C (local) contains UNIQUE UserID's and Titles.
I need to ensure that all UserID's contained in Table C still exist
in Table A.
I need to ensure that all UserID's contained in Table C have not had
a change in Title or Position from the previous week. If so Add to a temp table.
I'd prefer to use Access VBA or SQL in accomplish this task and the information will be displayed in a report.
Basically the same logic for both examples. use a left join to to identify mismatches.
Identify missing users in A
Insert into TableA (userID,Title)
select TableC.UserID, TableC.Title
from TableC
left join TableA on TableC.UserID=TableA.UserID
where TableA.UserID is null
Identify changes from B to A
insert into temp (userID,title,position)
select c.userID,c.title,c.position
from TableA a
left join tableB b on b.userid=a.userID and b.title=a.title and b.position=a.position
where b.userID is null

a query to compare data from two tables A AND B and give the results as per table A

I have a list of phone numbers that am sending messages to. in table A I record all the delivery status of each number.
I created another table B and stored some contacts in it.
I want an SQL query that I can use to compare the data in A and B such that, I want to get delivery status of numbers in table B from table A. If a number is appearing in the table A and B then I get the delivery status of that number.
I am using this SQL query:
SELECT address, delivery_status FROM safaricom_receipt
WHERE address IN (SELECT mobile FROM saf_dispute);
This will work for you :
SELECT address, delivery_status
FROM safaricom_receipt
INNER JOIN saf_dispute
ON safaricom_receipt.address=saf_dispute.mobile;
Assuming you are using SQL Server; this should work for you
SELECT sd.mobile, sr.delivery_status
FROM saf_dispute sd with (nolock)
LEFT OUTER JOIN safaricom_receipt sr with (nolock) on sd.mobile=sr.address
where sr.delivery_status is NOT NULL
you would get duplicate rows if you are storing multiple rows for the same mobile number in any table. I would suggest adding a datetime column in the future

MS Access SQL Query merging two different fields from separate tables shows reference ID's instead of actual values

I have two separate tables in my access database, which both use a third table as the reference for one particular field on each table. The data is entered onto the different tables by separate forms. Then I have several queries that then reference those particular fields that count and show unique values. Those queries show the actual values, then I created an sql query that does the same thing, only it shows the reference ID instead of the value in the actual field.
table ODI----------table CDN----------reference table
id RHA---------id CHA----------------id HA
1 blank----------1 radio---------------1 internet
2 internet-------2 tv------------------2 radio
3 referral-------3 radio---------------3 referral
4 tv-------------4 blank---------------4 repeat customer
5 blank----------5 internet------------5 tv
6 internet-------6 referral------------6 employee
7 referral-------7 referral------------7 social media
this is the code I am trying to make work.
SELECT m.[Marketing Results], Count(*) AS [Count]
FROM (SELECT RHA as [Marketing Results] FROM ODI
UNION ALL
SELECT CHA as [Marketing Results] FROM CDN) AS m
GROUP BY m.[Marketing Results]
HAVING (((m.[Marketing Results]) Is Not Null))
ORDER BY Count(*) DESC;
and what my desired result is,
Marketing Results--Counts
referral------------------4
internet------------------3
radio---------------------2
tv------------------------2
Lookup fields with alias don't show what is actually stored in table. ID is stored, not descriptive alias. Lookup alias will carry into regular queries but Union query only pulls actual stored values. At some point need to include reference table in query by joining on key fields in order to retrieve descriptive alias. Options:
in each UNION query SELECT line, join tables
join UNION query to reference table
join aggregate query to reference table
Most experienced developers will not build lookups in table because of confusion they cause. Also, they are not portable to other database platforms. http://access.mvps.org/Access/lookupfields.htm

JOIN of 4 tables, how to restrict SELECT columns to one table only?

I am working on ABAP program - user input is to query column ANLAGE and output is to get all records from table EADZ (and only fields of EADZ) based on ANLAGE.
Statement and joins should work like this:
Input ANLAGE, find in table EASTL, gets LOGIKNR
Input LOGIKNR, find in table EGERR, gets EQUNR
Input EQUNR, find in table ETDZ, gets LOGIKZW
Input LOGIKZW, find in table EADZ, gets all records (this is the final output)
Here is the code I tried:
DATA: gt_cas_rezy TYPE STANDARD TABLE OF eadz,
lv_dummy_eanl LIKE eanl-anlage.
SELECT-OPTIONS: so_anl FOR lv_dummy_eanl NO INTERVALS NO-EXTENSION.
SELECT * FROM eadz
INNER JOIN etdz ON eadz~logikzw EQ etdz~logikzw
INNER JOIN egerr ON etdz~equnr EQ egerr~equnr
INNER JOIN eastl ON egerr~logiknr EQ eastl~logiknr
INTO CORRESPONDING FIELDS OF TABLE #gt_cas_rezy
WHERE eastl~anlage IN #so_anl.
I got the records from table EADZ except that the date fields are empty (even though, they are filled in database table). I am assuming there is a problem with JOINs since in statement like this I join all the fields of all 4 tables into one "record" and then to corresponding fields of internal table.
How to get the values of date fields?
You can find the answer in the documentation.
If a column name appears multiple times and no alternative column name was granted, the last column listed is assigned.
In your case, at least two tables share the same column name. Therefore the values from the last mentioned table are used in the join.
You can solve this by listing the columns explicitly (or eadz~* in your case), giving an alias if required.
SELECT EADZ~* FROM EADZ INNER JOIN ETDZ ON EADZ~LOGIKZW = ETDZ~LOGIKZW
INNER JOIN EGERR ON ETDZ~EQUNR = EGERR~EQUNR
INNER JOIN EASTL ON EGERR~LOGIKNR = EASTL~LOGIKNR
INTO CORRESPONDING FIELDS OF TABLE #gt_cas_rezy
WHERE EASTL~ANLAGE IN #SO_ANL.
If you require additional fields, you can add them explicily with e.g. EADZ~*, EASTL~A.

MS Access One-to-Many Query

I'm working on a service type database. Customers can have many equipment types and many service calls.
I'm trying to make a query that selects the customers name, address, etc when certain parameters are met with relation to the equipment types.
A customer can have upto 5 different equipment types. Some only have 1, some have all 5.
My query is trying to find customers with equipment type 3 installed, but not equipment type 5. The equipment type 3 must also have a service date between to date fields on my search form.
I'm drawing a blank when it comes to writing this query. I was able to query the customers with equipment type 3 and specific service dates, but I can't seem to be able to eliminate the customers that also have equipment 5 installed, even if they also have equipment type 3 installed.
This is my SQL:
SELECT
tblCustomers.WCWF_ID,
tblCustomers.CustBusiness,
tblCustomers.CustLastName,
tblCustomers.CustFirstName,
tblCustomers.CustAddress,
tblCustomers.CustCity,
tblCustomers.CustST,
tblCustomers.CustZip5,
tblEquip.EquipResinDate,
tblEquip.EquipType,
tblCustomers.CustPostCard
FROM
tblCustomers
INNER JOIN
tblEquip ON tblCustomers.WCWF_ID = tblEquip.WCWF_ID
WHERE
(((tblEquip.EquipResinDate) Between [forms]![MailSearchSelect]![StartDate] And [forms]![MailSearchSelect]![EndDate])
AND ((tblEquip.EquipType)=3
AND (tblEquip.EquipType)<>5)
AND ((tblCustomers.CustPostCard)=True));
Any help would be greatly appreciated.
The problem with your query is that you are tying to use a single JOIN to filter in customers with equipment type 3 and filter out customers with equipment type 5 : you would need to split that logic into two distinct parts.
To pull out customers that have equipment type 3 installed, using a JOIN is fine (I just moved the related conditions from the WHERE clause to the JOIN, for more clarity). It would also have been possible to express this requirement as a WHERE EXISTS condition with a correlated subquery.
To exclude customers that have equipment type 5 installed, you can either :
use a NOT EXISTS condition in the WHERE clause with a correlated subquery (I chose that option)
or use a LEFT JOIN with WHERE ... IS NULL.
Query :
SELECT
c.WCWF_ID,
c.CustBusiness,
c.CustLastName,
c.CustFirstName,
c.CustAddress,
c.CustCity,
c.CustST,
c.CustZip5,
e.EquipResinDate,
c.EquipType,
c.CustPostCard
FROM
tblCustomers AS c
INNER JOIN tblEquip AS e
ON e.WCWF_ID = c.WCWF_ID
AND e.EquipType = 3
AND e.EquipResinDate
BETWEEN [forms]![MailSearchSelect]![StartDate]
AND [forms]![MailSearchSelect]![EndDate])
WHERE
c.CustPostCard = True
AND NOT EXISTS (
SELECT 1
FROM tblEquip AS e2
WHERE
e2.WCWF_ID = c.WCWF_ID
AND e2.EquipType = 5
)
PS : it is also a good idea to give alias to the table names ; it makes the query more readable and may avoid subtle bugs caused by name clashes when the same table is referenced more than once in the query.