Solved

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

Posted on 2004-04-18
2
395 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ms Access Many To Many Relationship Explantion Help Please 5 41
data analyst 3 50
Export Query data to excel file 14 36
append to an ms access field 6 25
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

895 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

15 Experts available now in Live!

Get 1:1 Help Now