Apend Query SQL to VBA module

T

Thanks, Chad

Hello, I have an apend query i wanted to use in a module and I cant get it to
work. I tried the code below but gives me an error "Query input must contain
at least on table or query" Thanks!


Public Function RunYearlyReview()
Dim db As DAO.Database
Dim strSql As String

strSql = "INSERT INTO tblYearlyReview ( DateOfHire, FirstName, LastName,
Status, CurrentMonth )" & _
"SELECT tblUser.DateOfHire, tblUser.FirstName, tblUser.LastName,
tblUser.Status, Month([DateOfHire]) AS CurrentMonth" & _
"WHERE (((tblUser.Status)=False) AND
((Month([DateOfHire]))=Month(Now())));"

DoCmd.RunSQL strSql

End Function
 
D

Douglas J. Steele

You appear to be missing the name of the table from which you're selecting:

strSql = "INSERT INTO tblYearlyReview ( DateOfHire, FirstName, LastName,
Status, CurrentMonth )" & _
"SELECT tblUser.DateOfHire, tblUser.FirstName, tblUser.LastName,
tblUser.Status, Month([DateOfHire]) AS CurrentMonth" & _
"FROM tblUser " & _
"WHERE (((tblUser.Status)=False) AND
((Month([DateOfHire]))=Month(Now())));"
 
B

boblarson

Your other part of the statement (the SELECT part) needs a FROM clause:

strSql = "INSERT INTO tblYearlyReview ( DateOfHire, FirstName, LastName,
Status, CurrentMonth )" & _
"SELECT tblUser.DateOfHire, tblUser.FirstName, tblUser.LastName,
tblUser.Status, Month([DateOfHire]) AS CurrentMonth" & _

FROM xxxxxxxxxxxxxxxxx

"WHERE (((tblUser.Status)=False) AND
((Month([DateOfHire]))=Month(Now())));"

--
Bob Larson

Tutorials at http://www.btabdevelopment.com

__________________________________
 
T

Thanks, Chad

I have added the from and am still gettin the same error?

strSql = "INSERT INTO tblYearlyReview ( DateOfHire, FirstName, LastName,
Status, CurrentMonth )" & _
"SELECT tblUser.DateOfHire, tblUser.FirstName, tblUser.LastName,
tblUser.Status, Month([DateOfHire]) AS CurrentMonth" & _
"FROM tblUser" & _
"WHERE (((tblUser.Status)=False) AND
((Month([DateOfHire]))=Month(Now())));"
 
D

Douglas J. Steele

You're missing spaces between the various lines you're concatenating
together.

strSql = "INSERT INTO tblYearlyReview ( DateOfHire, FirstName, LastName,
Status, CurrentMonth ) " & _
"SELECT tblUser.DateOfHire, tblUser.FirstName, tblUser.LastName,
tblUser.Status, Month([DateOfHire]) AS CurrentMonth " & _
"FROM tblUser " & _
"WHERE (((tblUser.Status)=False) AND
((Month([DateOfHire]))=Month(Now())));"
 
T

Thanks, Chad

Oh OK I see what your talking about! It works but how do I get rid of the
notification that its about to append rows? DoCmd.setwarnings = false then
true?

Thanks,
Chad

Douglas J. Steele said:
You're missing spaces between the various lines you're concatenating
together.

strSql = "INSERT INTO tblYearlyReview ( DateOfHire, FirstName, LastName,
Status, CurrentMonth ) " & _
"SELECT tblUser.DateOfHire, tblUser.FirstName, tblUser.LastName,
tblUser.Status, Month([DateOfHire]) AS CurrentMonth " & _
"FROM tblUser " & _
"WHERE (((tblUser.Status)=False) AND
((Month([DateOfHire]))=Month(Now())));"



--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Thanks said:
I have added the from and am still gettin the same error?

strSql = "INSERT INTO tblYearlyReview ( DateOfHire, FirstName, LastName,
Status, CurrentMonth )" & _
"SELECT tblUser.DateOfHire, tblUser.FirstName,
tblUser.LastName,
tblUser.Status, Month([DateOfHire]) AS CurrentMonth" & _
"FROM tblUser" & _
"WHERE (((tblUser.Status)=False) AND
((Month([DateOfHire]))=Month(Now())));"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top