Computer Learning Zone CLZ Access Excel Word Windows

In science the credit goes to the man who convinces the world, not to the man to whom the idea first occurs.

-Sir William Osler
 
Home   Courses   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Home > Courses > Access > Seminars > Email > Lessons >
Back to Email Seminar Lessons    Comments List
Upload Images   @Reply   Bookmark    Link   Email  
Using Custom SQL in Text Box
Tom Mura 
      
9 months ago
I've gone round and round trying to debug this. I'm only getting a Status message saying "Email Sent". Any suggestions would be greatly appreciated.

Details    Dim rs As Recordset
    Dim SqlCodeText As String
    SqlCodeText = Forms!EmailBulkF!SqlCodeTxt
    
    Set rs = CurrentDb.OpenRecordset(SqlCodeText)
    
    ' Debugging SQL Code Text
    MsgBox "SQL Query: " & SqlCodeText
    
    While Not rs.EOF
        Status "Email Address: " & rs!Email
        
        'This is the code for sending through Outlook
        SendOutlookEmail rs!Email, Nz(CC), Nz(BCC), Subject, Body, Nz(Attachment, ""), Nz(ReplyTo, "")
        
        'This is the code for sending through gmail
        'SendEmail rs!Email, "[email protected]", Nz(Subject), Nz(Body), "[email protected]", Nz(CC), Nz(BCC), True, Nz(Attachment, "")
        
        rs.MoveNext
        
        SleepSec 10
    Wend
         rs.Close
    
    Set rs = Nothing
    Status "Email Sent"

End Sub
Tom Mura OP  @Reply  
      
9 months ago
This does produce a message box that includes the code: SELECT * FROM TeamT WHERE GradeGender = """ & Forms!EmailBulkF!GradeGenderFIlterTxt & """ AND Email IS NOT NULL AND Email <> """"

But it doesn't run.
Tom Mura OP  @Reply  
      
9 months ago
One more piece of the puzzle: When I 'Hard Code' the SQL into the record set everything works fine. The issue seems to be with using the text box to hold the sql but I can't figure out why.
Adam Schwanz  @Reply  
            
9 months ago
Why don't you just program that into the Actual code instead of a textbox?

Also where is this code from? I just went and downloaded the new email seminar to try and look for the SendOutlookEmail function, but I don't see it.

Also, what do you mean by "doesnt run". Does it error or anything? Have you tried adding a Msgbox "Test" to the end of the code after everything is done to see if it reaches that point?
Alex Hedley  @Reply  
            
9 months ago
Can you put the SQL to a variable then print or MsgBox the output
Adam Schwanz  @Reply  
            
9 months ago
You replied while I was typing that out lol.

I know it's possible but I don't remember how off the top of my head to use a "string" as actual code. You should just hard code it
Tom Mura OP  @Reply  
      
9 months ago
Thank you for help Alex and Adam!

The SendOutlookEmail function comes from Email Seminar. I may have tweaked the name but that is the code it is running.

When I say 'It doesn't run. " I mean that it doesn't put the email address in the status box and it doesn't send an email.

I can hard code it into a button but means I have to change it each time I want to run the new SQL statement. I was hoping for  a more flexible solution.
Alex Hedley  @Reply  
            
9 months ago
What part do you need to be flexible?
Tom Mura OP  @Reply  
      
9 months ago
The Sql statement of the recordset.

One time I may need to send an email to the families of players who have not provided a birth certificate for their child. Another time I'd like send an email to all of our coaches who have not completed their background check. Being able to store those Sql statements in a table and select them from a combo box would be much quicker than changing the Sql in the recordset each time.
Alex Hedley  @Reply  
            
9 months ago
Tom Mura OP  @Reply  
      
9 months ago
Thanks Alex. I've watched the Search Form 2.0 video and my searches are working properly. I added some error handling (thanks ChatGPT Canvas!) and the recordset is not producing any records.

When I use this sql: ("SELECT * FROM TeamT WHERE GradeGender = """ & Forms!EmailBulkF!GradeGenderFIlterTxt & """ AND Email IS NOT NULL AND Email <> """"") in the VBA code it works correctly. If I plug this same code into a text box and reference it in the recordset as SqlCodeText. It gives me an error saying that it can't find a table or query with that name. If I removed the leading and trailing ( and ". It runs but doesn't generate any records.

I really appreciate your help hope that this additional info can help you point me in the right direction.

Thank you!!
Alex Hedley  @Reply  
            
9 months ago
If you Debug.Print(SqlCodeText) what do you get?
Tom Mura OP  @Reply  
      
9 months ago
When I plug this sql into the vba code I get:("SELECT * FROM TeamT WHERE GradeGender = """ & Forms!EmailBulkF!GradeGenderFIlterTxt & """ AND Email IS NOT NULL AND Email <> """""). When I put it into the text field and refer to it I get nothing in the immediate window because it shows and no records. Is there some difference in the code between what goes straight into VBA and what will come from a text box?
Kevin Robertson  @Reply  
          
9 months ago
instead of Email IS NOT NULL try Not IsNull(Email).
Alex Hedley  @Reply  
            
9 months ago
I want to see what your SQL String converts to, for example
SELECT * FROM TeamT WHERE GradeGender = "abc" AND Email IS NOT NULL AND Email <> ""
Tom Mura OP  @Reply  
      
9 months ago
When I tried: ("SELECT * FROM TeamT WHERE GradeGender = """ & Forms!EmailBulkF!GradeGenderFIlterTxt & """ AND Email IS NOT NULL AND Email <> """"") 'I got "Invalid use of Null'
Tom Mura OP  @Reply  
      
9 months ago
In my previous post when I said, "When I plug this sql into the vba code I get:("SELECT * FROM TeamT WHERE GradeGender = """ & Forms!EmailBulkF!GradeGenderFIlterTxt & """ AND Email IS NOT NULL AND Email <> """"")." I should have added that it works fine. Sorry any confusion that this caused.
Alex Hedley  @Reply  
            
9 months ago
Forms!EmailBulkF!GradeGenderFIlterTxt should be converting to whatever your text is that you typed in, if it's treating that as literal text it won't work.
Tom Mura OP  @Reply  
      
9 months ago
I just tried to enter, SELECT PlayerT. * FROM PlayerT  - as the most basic sql statement.
I got this, "An error occurred: Syntax error (missing operator) in query expression 'PlayerT*.

Thanks Alex. Then what should it be converting to if not text?
Alex Hedley  @Reply  
            
9 months ago
You have Set rs = CurrentDb.OpenRecordset(SqlCodeText)
What does SqlCodeText print out?
Or you could MsgBox it.
Tom Mura OP  @Reply  
      
9 months ago
I already have Set rs = CurrrentDb.OpenRecordset (SqlCodeText) in my code. Debug.Print SqlCodeText shows the code I have entered in the Forms!EmailBulkF!GradeGenderFIlterTxt field.

Tom Mura OP  @Reply  
      
9 months ago
But that's only if the code runs. I just put this into the sql query editor and it worked fine: SELECT * FROM TeamT WHERE (((TeamT.GradeGender)=[Forms]![EmailBulkF]![GradeGenderFilterTxt]) AND ((TeamT.Email) Is Not Null))

It will not work from the text box referenced in the vba code.
Tom Mura OP  @Reply  
      
9 months ago
The message I'm getting when referring to this code from a text box in a form is: 'An error occurred: Too few parameters. Expected 1. It works find when I copy and paste the same code directly into the VBA. Is that helpful in diagnosing the issue?
Tom Mura OP  @Reply  
      
9 months ago
I narrowed down the problem to using a combo box on the email form as the problem. When I used Sql without a combo box it all worked perfectly. Thanks for all you did to try to help.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Email Seminar Lessons.
 

 
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2025 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 7/20/2025 2:59:19 AM. PLT: 2s