Solved

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

Posted on 2004-04-18
2
409 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 250 total points
ID: 10853860
Use:
Dateserial([Syear],[smonth],"01")

Cstr isn't even needed

Nic;o)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

696 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