Solved

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

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

707 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

17 Experts available now in Live!

Get 1:1 Help Now