[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Select not working

Posted on 2013-11-30
12
Medium Priority
?
209 Views
Last Modified: 2013-12-06
I have this database where I need to check if the membership is expiring next month.  The field is a 4 character text field mmyy.  I have code to reverse the comparison data to yymm to make it easier to compare and to account for going from December to January:


           
            If Format(Now(), "mm") = 12 Then
               NextMonthYear = Format(Now(), "yy") + 1 & "01"
               
            Else: NextMonthYear = Format(Now(), "yy") & Format(Now(), "mm") + 1  'reversed for comparison
           
            End If
                       
            strSQL = NormalSelect & ", [Expiration] FROM Table1 " & _
                 "WHERE [Life] <> Yes " & _
                 " AND (Right([Expiration],2) & (Left([Expiration],2) <= " & NextMonthYear & _
                 " ORDER BY [Last]"
                   
            CurrentDb.QueryDefs("Membership Expiring Next Month").SQL = strSQL
            DoCmd.OpenQuery "Membership Expiring Next Month"
           
I know the [Life] <> Yes is working but this portion:

" AND (Right([Expiration],2) & (Left([Expiration],2) <= " & NextMonthYear & _

is giving me an error at runtime.

Any help is appreciated.

Thanks,
Ric
0
Comment
Question by:Joppa
[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
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 14

Expert Comment

by:Bill Ross
ID: 39687684
Hi,

I suggest you use built in date arithmetic.

No need for NextMonthYear

Just use Format(DateAdd("m",1,date()),"YYMM") which will always yield the following correct month.  No if... is required.

Compare that

...AND clng([Expiration]) <= clng(Format(DateAdd("m",1,date()),"YYMM"));

Regards,

Bill
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39687688
what is  NornalSelect ?

try this

          strSQL = NormalSelect & ", [Expiration] FROM Table1 " & _
                 "WHERE [Life] <> Yes " & _
                 " AND (Right([Expiration],2) & (Left([Expiration],2) <= " & NextMonthYear & _
                 " ORDER BY [Last]"

' add this line
        Debug.Print strSQL


see what was printed in the  immediate window and post here


.
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 39687968
Use real date valuess. It's much simpler.

           strSQL = NormalSelect & ", [Expiration] FROM Table1 " & _
                 "WHERE [Life] <> Yes " & _
                 "AND DateDiff('m', Date(), DateValue([Expiration])) = 1 " & _
                 "ORDER BY [Last]"

/gustav
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:Joppa
ID: 39688062
capricorn1:

from the immediate window -

SELECT First, Last, Street, City, State, Zip, [Phone #] , [Expiration] FROM Table1 WHERE [Life] <> Yes  AND (Right([Expiration],2) & (Left([Expiration],2) <= 1401 ORDER BY [Last]


NormalSelect is used so I won't have to keep typing this:

 NormalSelect = "SELECT First, Last, Street, City, State, Zip, [Phone #] "

Ric
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39688085
change this

NormalSelect = "SELECT First, Last, Street, City, State, Zip, [Phone #] "


with

NormalSelect = "SELECT [First], [Last], Street, City, State, Zip, [Phone #] "


.
if you get a type mismatch error, use this
     
     strSQL = NormalSelect & ", [Expiration] FROM Table1 " & _
                 "WHERE [Life] <> Yes " & _
                 " AND (Right([Expiration],2) & (Left([Expiration],2) <= '" & NextMonthYear & "' _
                 " ORDER BY [Last]"
0
 

Author Comment

by:Joppa
ID: 39688089
Gustav,

  DateValue won't work in my case because the table was designed to have only 4 digits to represent the expiration date.  For example 1113 would be for Nov 2013.  There are no separators for DateValue to use.

Ric
0
 

Author Comment

by:Joppa
ID: 39688102
Bill,
 
  In order to get it to compile and recognize the CLng[Expiration] I had to take it out of the quotes but now it gives me a runtime error.  I've tried a lot of different formats but can't get it to work.

            strSQL = NormalSelect & ", [Expiration] FROM Table1 " & _
                 "WHERE [Life] <> Yes " & _
                 " AND " & CLng([ Expiration]) <= CLng(Format(DateAdd("m", 1, Date), "YYMM")) & " ORDER BY [Last]"

It doesn't even get to the debug.print statement to see what strSQL looks like.

Ric
0
 

Author Comment

by:Joppa
ID: 39688111
Capricorn1,

This also gave me a runtime error but I do get the string output.  I also made the suggested changes to NormalSelect.


            strSQL = NormalSelect & ", [Expiration] FROM Table1 " & _
                    "WHERE [Life] <> Yes " & _
                    " AND (Right([Expiration],2) & (Left([Expiration],2) <= " & NextMonthYear & _
                    " ORDER BY [Last]"
The output:

SELECT [First], [Last], Street, City, State, Zip, [Phone #] , [Expiration] FROM Table1 WHERE [Life] <> Yes  AND (Right([Expiration],2) & (Left([Expiration],2) <= 1401 ORDER BY [Last]

Ric
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 39688117
is the field [Life] a  YesNo field?

try this one


             strSQL = NormalSelect & ", [Expiration] FROM Table1 " & _
                 "WHERE [Life] <> -1 " & _
                 " AND Right([Expiration],2) & Left([Expiration],2) <= '" & NextMonthYear & "'" & _
                 " ORDER BY [Last]"



btw you have  two extra "("  before "Right" and "Left"
0
 
LVL 14

Expert Comment

by:Bill Ross
ID: 39688164
Hi,

Try this:

 strSQL =  "SELECT First, Last, Street, City, State, Zip, [Phone #], CLng([Expiration]) AS Expr1 FROM Table1 WHERE [Life] <> Yes AND Expr1 <= " & CLng(Format(DateAdd("m", 1, Date), "YYMM")) & " ORDER BY [Last]"

Note space between " and ORDER BY....

If that works we can modify again for the NormalSelect.

Regards,

Bill
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 39688325
>  For example 1113 would be for Nov 2013

OK. Then still use date values or you easily get into trouble:


strSQL = NormalSelect & ", [Expiration] FROM Table1 " & _
    "WHERE [Life] <> Yes " & _
    "AND DateDiff('m', Date(), DateSerial(2000 + Val(Right([Expiration], 2)), Val(Left([Expiration], 2), 1)) = 1 " & _
    "ORDER BY [Last]"

/gustav
0
 

Author Closing Comment

by:Joppa
ID: 39702640
I tried the others and this one worked the best for me.

thanks,
Ric
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

649 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