Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA SQL Query

Posted on 2013-10-25
2
Medium Priority
?
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 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 49

Expert Comment

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

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 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