?
Solved

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

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

Cstr isn't even needed

Nic;o)
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

850 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