Solved

VBA SQL Query

Posted on 2013-10-25
2
275 Views
Last Modified: 2013-10-26
I'm using the following code to get a date value (or date values) from a text box, convert it to a string variable, and use it in a make table query.  The code works fine for Case "Equals" but builds an empty table for all other values.  If I take the variable and run it in a query by grid, it works fine for all Cases.  Also I am unable to monitor the value of strSQL in a watch window.  It shows a value of <Expression not defined in context> .

Select Case FlagDates   'Sets parameter format
    Case "Equals"
        flg1STda = Me.txtStartDate.Value
        MakeTbl
    Case "Before"
        flg1STda = "<#" & Me.txtStartDate.Value & "#"    'Less than <#mm/dd/yyyy#
        MakeTbl
    Case "After"
        flg1STda = ">#" & Me.txtStartDate.Value & "#"    'Greater than >#mm/dd/yyyy#
        MakeTbl
    Case "Between"
        flg1STda = "Between #" & Me.txtStartDate.Value & "#" And "#" & Me.txtEndDate.Value & "#"  'Between #mm/dd/yyyy# and #mm/dd/yyyy#
       

Public Sub MakeTbl()
Dim strSQL As String
strSQL = "SELECT tblTest.* INTO TSTable FROM tblTest WHERE tblTest.[flag1date] = '" & flg1STda & "';"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True

What am I doing wrong?
0
Comment
Question by:Knowknot
  • 2
2 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39602029
I'n not sure of this code syntax, but immediately after tblTest.[flag1date] you follow it with the equal sign, yet you are then trying to concatenate with strings for less than / greater than / between

so you would end up with

tblTest.[flag1date] =  #Me.txtStartDate.Value# -- this works

tblTest.[flag1date] =  <#Me.txtStartDate.Value#

tblTest.[flag1date] =  >#Me.txtStartDate.Value#

tblTest.[flag1date] =  Between #Me.txtStartDate.Value# And #Me.txtStartDate.Value#

that equal before a between isn't workable for sure

I think you need to remove the equal from the fixed string, and place the wanted operators into the case expression for all 4 options.

If this works without more effort it's a damn good guess.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39602300
:) thank you, must have guessed at least partly right. Cheers, Paul
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now