473,465 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Put Query results into a memo field - VBA, access 07

8 New Member
Hello,

I was wondering if someone might be able to help me. I’m a little stuck as to how to put the results of a query into a memo field.

I run a ski chalet company and I have a bookings database. In this I store all the emails that go back and forth with my guests, these are stored in a memo field.

I’m trying to make a system to template my emails for me. Ideally I’d like it to template and email that has a list of all the extras they’ve booked (ski lessons, ski hire etc), maybe something like:

Dear <GUEST NAME>

Here is a list of all your ski lessons, hire and lift passes:
<guestextra>
<guestextra>
<guestextra>

Etc.

I’ve put a button on the form that stores this emails and when I click it the code to do the above will hopefully run!

I can get the first bit working to put the guestname in at the start (as for that it only has to look up one value), then put it all into the memo field along with the “here is a list of your ski lessons....” etc. I have the query running fine that selects all the guest extras for that particular guest. I don’t know how to get the results of that query to go as text into the memo field as well.

I’m using access 2007 on a vista computer. I am doing this in VBA, though I’m only a beginner at such things.

If anyone has any ideas I’d be very grateful to hear about them. Even just directions to research as I’ve been searching for ages but I’m not sure really what I’m looking for.

Many thanks for your help,

Phil Teare.
Sep 23 '08 #1
8 4313
NeoPa
32,556 Recognized Expert Moderator MVP
If I understand you correctly, then you want to access the SQL in a Recordset object (DAO.Recordset I would suggest).

From here, you can manipulate and use the returned values (See Basic DAO recordset loop using two recordsets). Add them to a string variable that will end up as the Memo field I would suggest.

Welcome to Bytes!
Sep 23 '08 #2
pteare
8 New Member
Thanks for that. Your directions to reasearch were spot on. I've got it all just about working now. I'm having one mental block though.

I have a table of guests which stores the guest names, email addressed etc, and a table of extra which stores the options for ski lessons, ski hire etc. There is then a table called booked extras which just has the foreign keys from these two, and this table is the list of who has booked what. My query gets it's results from this bookedextras table.

As the query is made of foreign keys, the results are just numbers. How can I get the numbers as the text from the original tables? Sorry I know this should be really simple but for the life of me I can't figure it out!

My current code is below. It works but just produces a list of numbers
("2 -- 4" rather than "Phil -- Beginner lessons" as I would like it to.)

Many thanks again,

Phil.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command3_Click()
  2. Dim db As DAO.Database
  3. Dim rs1 As DAO.Recordset
  4. Set db = CurrentDb
  5. Set rs1 = db.OpenRecordset("SELECT bookedextras.guestbooking, bookedextras.extra " & _
  6.             "FROM guest INNER JOIN (booking INNER JOIN bookedextras ON booking.ID = bookedextras.guestbooking) ON guest.ID = booking.guestname " & _
  7.             "WHERE (((guest.guestname)='phil'));")
  8.  
  9. DoCmd.GoToRecord , , acNewRec
  10. email.Value = "Dear " & Forms![guest]![guestname].Value & "," & vbCrLf & "Here is your list of booked extras:" & _
  11.         vbCrLf & "Text from the query:" & vbCrLf
  12.  
  13. rs1.MoveFirst
  14. Do Until rs1.EOF
  15.     email.Value = email.Value & vbCrLf & _
  16.                 rs1!guestbooking & " -- " & rs1!extra
  17.     rs1.MoveNext
  18. Loop
  19. rs1.Close
  20. Set rs1 = Nothing
  21. Set db = Nothing
  22.  
  23. End Sub
Sep 23 '08 #3
pteare
8 New Member
Hello again,

Sorry I forgot to say I could use a few dlookups I guess but i was wondering if there were any other (more efficient?!) methods, as this is just my trial database for figuring stuff out, the really bookings one is much larger and there woudl be many tables to look things up in with all the foreign keys....

Thanks!

Phil.
Sep 23 '08 #4
NeoPa
32,556 Recognized Expert Moderator MVP
You're quite right not to use DLookup()s in this situation. This really should be a job for a properly defined query.

With the info I have to hand, I can't be too sure of things, but I would guess there is a table for extras somewhere. In fact you say so.

I'll tell you what I'll do. I'll put together some code where I guess what MetaData you have for the relevant tables, and you will have to go through the code, understand it, and make any changes required to convert it to work in your database.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command3_Click()
  2.   Dim db As DAO.Database
  3.   Dim rs1 As DAO.Recordset
  4.   Dim strSQL As String
  5.  
  6.   Set db = CurrentDb
  7.   strSQL = "SELECT tB.BookingName, tE.ExtraName " & _
  8.            "FROM (([BookedExtras] AS tBE INNER JOIN " & _
  9.            "[Booking] AS tB " & _
  10.            "ON   tBE.GuestBooking=tB.ID) INNER JOIN " & _
  11.            "[Extra] AS tE " & _
  12.            "ON   tBE.Extra=tE.ID) INNER JOIN " & _
  13.            "[Guest] AS tG " & _
  14.            "ON   tB.GuestName=tG.ID " & _
  15.            "WHERE (tG.GuestName='phil')"
  16.   Set rs1 = db.OpenRecordset(strSQL)
  17.  
  18.   DoCmd.GoToRecord , , acNewRec
  19.   email.Value = "Dear " & Forms![guest]![guestname].Value & "," & vbCrLf & "Here is your list of booked extras:" & _
  20.           vbCrLf & "Text from the query:" & vbCrLf
  21.  
  22.   rs1.MoveFirst
  23.   Do Until rs1.EOF
  24.       email.Value = email.Value & vbCrLf & _
  25.                   rs1!BookingName & " -- " & rs1!ExtraName
  26.       rs1.MoveNext
  27.   Loop
  28.   rs1.Close
  29.   Set rs1 = Nothing
  30.   Set db = Nothing
  31.  
  32. End Sub
Sep 23 '08 #5
pteare
8 New Member
thanks so much for that. If I'm understanding you and your code right then what you are saying is I should change the initial query in the first place to produce the text I need (rather than just a foreign key ID), as opposed to trying later to translate those foreign keys back to text.

That makes so much more sense than my way! Many thanks for your time and thoughts, I'm off to attack that....
Sep 24 '08 #6
pteare
8 New Member
wonderful! It's all grand and working perfectly now. Many many thanks for your help, I'm very grateful. I don't know how long it would have taken me otherwise! I've put the final working code below just for others reading through this solution who might want to see how it ended up.

Thanks again,

Phil.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command3_Click()
  2. Dim db As DAO.Database
  3. Dim rs1 As DAO.Recordset
  4. Dim strSQL As String
  5. Dim strGuestname As String
  6.  
  7. Set db = CurrentDb
  8.  
  9. strGuestname = Forms![guest]![guestname].Value
  10.  
  11. strSQL = "SELECT guest.guestname, extratype.extratype, extra.extraname " & _
  12.         "FROM extratype INNER JOIN (extra INNER JOIN (guest INNER JOIN " & _
  13.         "(booking INNER JOIN bookedextras ON booking.ID = bookedextras.guestbooking) " & _
  14.         "ON guest.ID = booking.guestname) ON extra.ID = bookedextras.extra) " & _
  15.         "ON extratype.ID = extra.extratype " & _
  16.         "WHERE (((guest.guestname)='" & strGuestname & "'));"
  17.  
  18. Set rs1 = db.OpenRecordset(strSQL)
  19.  
  20. DoCmd.GoToRecord , , acNewRec
  21. email.Value = "Dear " & strGuestname & "," & vbCrLf & "Here is your list of booked extras:" & _
  22.         vbCrLf & "Text from the query:" & vbCrLf
  23.  
  24. rs1.MoveFirst
  25. Do Until rs1.EOF
  26.     email.Value = email.Value & vbCrLf & _
  27.                 rs1!guestname & " -- " & rs1!extratype & " - " & rs1!extraname
  28.     rs1.MoveNext
  29. Loop
  30. rs1.Close
  31. Set rs1 = Nothing
  32. Set db = Nothing
  33.  
  34. End Sub
Sep 24 '08 #7
NeoPa
32,556 Recognized Expert Moderator MVP
thanks so much for that. If I'm understanding you and your code right then what you are saying is I should change the initial query in the first place to produce the text I need (rather than just a foreign key ID), as opposed to trying later to translate those foreign keys back to text.

That makes so much more sense than my way! Many thanks for your time and thoughts, I'm off to attack that....
Absolutely. You express the idea perfectly :)
Sep 24 '08 #8
NeoPa
32,556 Recognized Expert Moderator MVP
Good stuff, and thanks for posting your eventual code.

It's very gratifying when a member picks up and goes with what you're trying to say :)
Sep 24 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Yonatan Maman | last post by:
Hi Im using access 2000. and I have a strange problem. when I execute a query on TABLE_A (TABLE_A contains 2 colums: "id" INTEGER and "name" MEMO) Query1: "select id, name from TABLE_A ORDER BY...
1
by: Rose | last post by:
I have an ACCESS 2000 database and I have a table with a field setup as a MEMO. I am trying to run a query and append the MEMO field to another table - everytime I run the query - it drops data -...
1
by: Rose | last post by:
I have an access 2000 database. In it I have a table OPEN Items which has a field Issue that is setup as a MEMO field. I am trying to append information from another table with the same setup,...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
1
by: Suffrinmick | last post by:
Hello Everyone I've built a database using Access 2000 which includes a query which is built using a form containing filters. No problem. When I export the results of the query to excel, (File >...
0
by: Phil C. | last post by:
Hi, I'm using Access 2000. I have a Select Query that uses the MID function to separate the actual text of articles from the title of the articles. The articles are enterd into the...
2
by: jacoballen | last post by:
I have a query that combines the results of three related tables. The memo fields are truncated to 255 characters, but I need all the information in them. I'm aware that removing code such as...
1
by: Jeff | last post by:
A client showed me this one. In a totals query that counts items and links to a parent record, what should be the output from a memo field shows up as Chinese characters - I kid not. He has a...
3
by: seagullino | last post by:
Hello, I've developed my first Form, a simple affair that enables the user to search text in the memo fields in our database. When the "search" button is pressed, it runs a macro which runs the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.