Export and email different selections of a table (1 Viewer)

CaptainSmall

New member
Local time
Today, 14:42
Joined
Sep 22, 2017
Messages
8
As a low ranking member of the Space Federation, I'm looking for an automated solution to export different selections of an Access table to a unique file for each selection. The difference with each selection is the value in one of the fields of the table. There needs to be as many files exported as there are different values in that field. Example of the table:

Field 1 name: SpaceShipName (with values like Zorg7000, InterstellerQueen, etc.)
Field 2 name: Cargo (with values like Medicine, SillyString, Water, etc.)

In other words, export a file with only the Zorg7000's various items of cargo, then a different file with only InterstellerQueen's various items of cargo, etc.

After that, I need an automated way to email each spaceship's freight officer their spaceship's file. The email address can be stored in a second table or in the first table, it doesn't matter. Example if a second table:

Field 1 name : SpaceshipName
Field 2 name: EmailAddress (with values like zorgy at example.com, nolan at test.org, etc.)

In other words, email Zorgy the file listing the Zorg7000's cargo, email Nolan the file listing InterstellerQueens' cargo, etc.

Did I mention that if I don't resolve this the Galaxian President is going to be angry with me?

I need to do this without installing any utilities other than those available in Microsoft Office. It needs to be automated because there are a lot of spaceships out there. I would expect this requires VBA, partly because this will require some kind of non-hyper loop - the spaceship names can change depending on wars, obviously - and partly because the SendObject macro in Access doesn't work on the setup I use in work, which this database is really for (the examples above might not be totally realistic).

I use this sort of code at the moment for sending an email with an attachment in Access via VBA, so hopefully the solution can use this method for emailing too:
Code:
 Dim strPath As String
    strPath = "FileNamePathHere\" & "FileNameHere"
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
    With MailOutLook
            '.SentOnBehalfOfName = ""
            .To = "EmailAddressHere"
            ''.cc = ""
            ''.bcc = ""
            .Subject = "SubjectHere"
            .HTMLBody = "BodyHere"
            .Attachments.Add (strPath)
            .Display
    End With
I use this sort of code at the moment for simple exporting of tables:
Code:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "tblNameHere", "FileNamePathHere\" & "FileNameHere", True
As you can see, this is my first post and I'd love some help please! I am flying to Saturn now but will check back in on Wednesday. Thank you.
 

Minty

AWF VIP
Local time
Today, 22:42
Joined
Jul 26, 2013
Messages
10,371
Well - you are already about 1/2 way to the warp gate.

All you need to do is open a recordset with the appropriate fields to filter the report and have the spaceship names, and email addresses, and loop around it, with your existing code in the loop, adjusted to filter the export to the current record.

To do this yourself simply create a query to achieve the first stage. Save that and then use it as your basis for the recordset.

Then open the recordset and create a loop that steps through it with a debug.print of Email address, and spaceship name.

Once this is working - add back in the code you have to export the spreadsheet to a folder specific to the spaceship, and at this point do nothing.

Finally add back the email bit in the loop.

If you get stuck, hail us on starfleet emergency frequency 119.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 22:42
Joined
Jul 9, 2003
Messages
16,282
I love posts like this, makes it so much easier to put up with the aerosols... Not mentioning any access haters in particular....
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 17:42
Joined
Apr 27, 2015
Messages
6,341
:DIt is posts like these that get me through the rough times! Welcome aboard Capt, we need more members like you...

Minty and Unc Giz, you guys are just too cool. Aerosols and 119...going to be chuckling about this for awhile.
 

CaptainSmall

New member
Local time
Today, 14:42
Joined
Sep 22, 2017
Messages
8
Hi Minty et al, thanks for taking the time to help. Hope this message gets to you through the space debris.

Let's see if I'm following you correctly. I've saved a SELECT DISTINCT query to return each SpaceShipName once and its EmailAddress. It's named qryselectdistinctSS . Hopefully that's the recordset you describe above.

I don't know how to use this with my first table to export each spaceship's cargo. Well, I know how to do it using the spaceship names but not 'dynamically' to account for one replacing another at any time, particularly after a deadly dogfight.

Not having used an Access loop before and fearing I'm not as smart as you're giving me credit for, this is where the struggle begins. I see there are different approaches such as
Code:
Do Until 'something
'code
Loop
or
Code:
Do While 'something
'code
Loop
and others!

Are you able to show a simple example of what I want to achieve with the export files? Then I could try and re-use it myself for the second objective of emailing the files (that is to be done separately because the files will be formatted first due to the ugly way Access exports files to xlsx).
 

Minty

AWF VIP
Local time
Today, 22:42
Joined
Jul 26, 2013
Messages
10,371
Okay - lets start simples Capt. - This is written off pat so may have some typo's in it...
Code:
Dim db as Database
Dim rs as Recordset 
Dim sEmail as string
Dim sSpaceship as String

Set db = CurrentDb
Set rs  = db.OpenRecordSet("qryselectdistinctSS", dbOpenSnapShot)  [COLOR="green"]' Open your query as the recordset[/COLOR]

If Not rs.EOF 		[COLOR="Green"]' Make sure it's got some records[/COLOR]
rs.MoveFirst	
Do While Not rs.EOF
	sSpaceship = rs.Fields("YourSpacehipNameField")
	sEmail = rs.Fields("YourEmailAddressField")
	Debug.Print sSpaceship & " : " & sEmail		[COLOR="green"]'This will print out in the immediate window in the VBA editor.
' This is where you would do stuff in the loop[/COLOR]
	rs.movenext
Loop

rs.Close
Set rs = Nothing
 

CaptainSmall

New member
Local time
Today, 14:42
Joined
Sep 22, 2017
Messages
8
That's great thanks Minty. Of course, it's flawless (except the If line but I've annotated that out as it won't ever happen). I can see the spaceship names safe and sound in the immediate window as the code is stepped through.

Now I'm trying to export the cargo records for each spaceship to their own files. Let's call that second table tblCargo. I expect I can't simply use my existing code
Code:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "tblNameHere", "FileNamePathHere" & "FileNameHere", True
and replace the "tblNameHere" with a query that joins qryselectdistinctSS with tblCargo, because that'll export the cargo for all the spaceships.

So I suppose a way is needed to join the current sSpaceship with tblCargo in place of that?
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 22:42
Joined
Jul 9, 2003
Messages
16,282
As a low ranking member of the Space Federation....
Field 1 name: SpaceShipName (with values like Zorg7000, InterstellerQueen, etc.)
Field 2 name: Cargo (with values like Medicine, SillyString, Water, etc.)

"email each spaceship's freight officer their spaceship's file"

Field 2 name: EmailAddress (with values like zorgy at example.com, nolan at test.org, etc.)

In other words, email Zorgy the file listing the Zorg7000's cargo, email Nolan the file listing InterstellerQueens' cargo, etc.

I am flying to Saturn now but will check back in on Wednesday. Thank you.

Hi Leo,
As an aside, have you really created a database based on spaceships? If you have, I wonder if you would be good enough to provide it to the forum as a sample database? I'm sure it would be most valuable in helping others with their Database problems. I've thought for a long time that we need a sample database we can all hack around and add our little bits of knowledge to...
 

Minty

AWF VIP
Local time
Today, 22:42
Joined
Jul 26, 2013
Messages
10,371
There are quite a few ways of doing this - I have to go out right now but will come back to this later.

I will probably use a querydef that we create on the fly per loop. If you fancy googling that while I'm out on important Starfleet Business - "Fill your spaceboots" :)

Querydefs are a very good way of doing quite a lot of things in Access.
 

CaptainSmall

New member
Local time
Today, 14:42
Joined
Sep 22, 2017
Messages
8
Hi UncleGizmo, in the unlikely event this becomes a thing, I've created and attached it! No what's needed is for slightly more down to earth reasons.

Thanks Minty, I will look that up tomorrow and who knows I may reply before you.
 

Attachments

  • SpaceshipsVersion1.accdb
    308 KB · Views: 108

Minty

AWF VIP
Local time
Today, 22:42
Joined
Jul 26, 2013
Messages
10,371
Hi Capt. I've attached a really basic example of creating files in the loop. Hopefully enough to get you started.

Have a look through the code, I've included some other Excel functions that may help you export the correctly formatted results. You'll probably want to play around with the queries.
 

Attachments

  • SpaceshipsVersion1.accdb
    468 KB · Views: 108

isladogs

MVP / VIP
Local time
Today, 22:42
Joined
Jan 14, 2017
Messages
18,219
Brilliant!

I'd just like to thank both Cap'n Small & Minty for providing an out of this world example to add to the sample files for my JSON parser database.
Yes I know I'm getting obsessed....!

Just in case you or anyone on Planet Zorg is interested, it is attached!
I've zipped it so it will travel faster in cyberspace....and so I can upload it to the forum

EDIT:
My quantum store manager told me off for not packaging the cargo in arrays.
So I've slipped her a few cyberbitcoins & she's parcelled them up properly with silly string.
New version re-attached.

OK, back to the real world....
 

Attachments

  • Spaceship.zip
    606 bytes · Views: 71
Last edited:

CaptainSmall

New member
Local time
Today, 14:42
Joined
Sep 22, 2017
Messages
8
Hi Minty, I can't believe I never replied to say thank you. Yet, I didn't. So... thank you! I was moved away from this project in a stupid rush at the time, by way of explanation. Anyway, am back now where I belong (in space?) so will proceed with this :)
 

Minty

AWF VIP
Local time
Today, 22:42
Joined
Jul 26, 2013
Messages
10,371
Welcome back :)

I will assume the Borg haven't assimilated you, and I'd be unwittingly assisting them towards world domination?
:p
 

Users who are viewing this thread

Top Bottom