?
Solved

Slash in SELECT query: works in QGrid, not in VBA

Posted on 2004-04-18
2
Medium Priority
?
411 Views
Last Modified: 2008-02-20
If I paste the following SELECT statement into the SQL View of the Access 2002 Query Grid, it works fine and the Make Table "RptTable" is created:

SELECT service.file, service.prog, service.stype, service.scount, CDate(CStr([smonth]) & "/1/" & CStr([Syear])) AS SDate, student.gender, student.dob, student.race INTO RptTable
FROM service INNER JOIN student ON service.file = student.file;

HOWEVER,

If I cut and paste the exact same SELECT statement into a VBA module and try to execute it as a command it says "Type mismatch". WHAT GIVES? I think it has to do with the fact that the string CDATE function isn't working because Access2002 mysteriously puts extra spaces around my slashes, i.e.:

CDate(CStr([smonth]) & " / 1 / " & CStr([Syear])) AS SDate

SHOULD BE

CDate(CStr([smonth]) & "/1/ " & CStr([Syear])) AS SDate

HOW TO STOP ACCESS FROM INSERTING THESE SPACES? I TRIED A BUNCH OF THINGS LIKE PUTTING EACH CHARACTER IN AS A SEPARATE STRING, ETC. BUT NOTHING WORKS! IS THIS A BUG?

Here's the VBA code that says "Type Mismatch":

Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
cmd.ActiveConnection = CurrentProject.Connection
cmd.CommandType = adCmdText

strSQL = "SELECT service.file, service.prog, service.stype, service.scount, CDate(CStr([smonth]) & " / 1 / " & CStr([Syear])) AS SDate, "_ & "student.gender, student.dob, student.race INTO TestTable " _
& "FROM service INNER JOIN student ON service.file = student.file;"

cmd.CommandText = strSQL
cmd.Execute

THANKS!

Kathryn
0
Comment
Question by:birstein
[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 Comments
 
LVL 4

Expert Comment

by:brunomsilva
ID: 10853856
try something like "\/1\/". this will make access treat the next character as a literal

cheers,
  bruno
0
 
LVL 54

Accepted Solution

by:
nico5038 earned 1000 total points
ID: 10853860
Use:
Dateserial([Syear],[smonth],"01")

Cstr isn't even needed

Nic;o)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
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…
Suggested Courses

770 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