WUGNET, the Windows User Group Network
Your Complete Resource Center for "The Best" in Shareware, Computing Tips and Support, Windows Industry News... and much more!
Home Forums Shareware Windows Tips Hot Offers FREE Newsletters Arcade Contact Us About Partners
Search WUGNET: RSS Feeds RSS Feeds Advertise with WUGNET    |    Shareware eBooks
HomeHome FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

VBA SQL statement help

 
   Home -> Office other -> Getting Started RSS
Next:  Access Screen Display Problem  
Author Message
SoggyCashew

External


Since: Aug 08, 2008
Posts: 75



(Msg. 1) Posted: Sat Aug 16, 2008 10:29 pm
Post subject: VBA SQL statement help
Archived from groups: microsoft>public>access>gettingstarted (more info?)

Hello, I am trying to figure out how to write a sql statement that will count
how many times excused absence was used within the past 6 months starting on
todays date. I wrote the statement below but it doesnt give me any results?
What am I missing? Thanks!

Public Function GetVacationAndHolidays()
Dim strSql
Dim frm As Form
Dim db As DAO.Database
Dim rs As DAO.Recordset

Set frm = Forms!frmCalendar
Set db = CurrentDb

'TEST
strSql = "SELECT Count(tblInput.InputID) AS TotDays FROM tblInput "
strSql = strSql & "WHERE tblInput.InputDate = >DateAdd('M',-6,Date())
And tblInput.UserID = 2 AND ((tblInput.InputText)='Excused Absence';"
Set rs = db.OpenRecordset(strSql, dbOpenSnapshot)
frm!txtTEST = rs!TotDays
rs.Close
strSql = "'"


Set rs = Nothing
db.Close
Set db = Nothing
End Function



--
Thanks,
Chad
Back to top
Login to vote
Dale Fye

External


Since: Jul 06, 2004
Posts: 127



(Msg. 2) Posted: Sun Aug 17, 2008 7:49 am
Post subject: Re: VBA SQL statement help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Personally, I'd use DCOUNT to do this. It is optimized for this type of
thing, and requires a lot less code.

Public Function GetExcusedAbsense(intUserID) as integer

Dim strCriteria as string

strCriteria = "[InputDate] >= #" & DateAdd('M',-6,Date()) & "# AND " _
& "[UserID] = " & intUserID & " AND " _
& "[InputText]='Excused Absence'"
GetExcusedAbsense = DCOUNT("InputID", "tblInput", strCriteria)

End Function

If you insist on using your code, then you neen to move the DateAdd( )
function outside of the quotes, and set it off with # symbols (as I did
above). Other than that, I think your code looks about right.

HTH
Dale



"SoggyCashew" <SoggyCashew.TakeThisOut@discussions.microsoft.com> wrote in message
news:EA2A5369-9A4B-4F07-9C83-2787617A9355@microsoft.com...
> Hello, I am trying to figure out how to write a sql statement that will
> count
> how many times excused absence was used within the past 6 months starting
> on
> todays date. I wrote the statement below but it doesnt give me any
> results?
> What am I missing? Thanks!
>
> Public Function GetVacationAndHolidays()
> Dim strSql
> Dim frm As Form
> Dim db As DAO.Database
> Dim rs As DAO.Recordset
>
> Set frm = Forms!frmCalendar
> Set db = CurrentDb
>
> 'TEST
> strSql = "SELECT Count(tblInput.InputID) AS TotDays FROM tblInput "
> strSql = strSql & "WHERE tblInput.InputDate = >DateAdd('M',-6,Date())
> And tblInput.UserID = 2 AND ((tblInput.InputText)='Excused Absence';"
> Set rs = db.OpenRecordset(strSql, dbOpenSnapshot)
> frm!txtTEST = rs!TotDays
> rs.Close
> strSql = "'"
>
>
> Set rs = Nothing
> db.Close
> Set db = Nothing
> End Function
>
>
>
> --
> Thanks,
> Chad
Back to top
Login to vote
SoggyCashew

External


Since: Aug 08, 2008
Posts: 75



(Msg. 3) Posted: Sun Aug 17, 2008 7:49 am
Post subject: Re: VBA SQL statement help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Dale, Im going to have to go with what I got because I have several other
text boxes with diferent names other than "Excused Absence" that im using in
this function. Im not sure I know how to do what your saying or telling me
how to fix the one I have? I tried the SQL below with no luck. It outlines
the line and sets focus to the 'M'

strSql = "SELECT Count(tblInput.InputID) AS TotDays FROM tblInput "
strSql = strSql & "WHERE tblInput.UserID = 2 AND
((tblInput.InputText)='Excused Absence';"
strSql = "[InputDate] >= #" & DateAdd('M',-6,Date()) & "#
Set rs = db.OpenRecordset(strSql, dbOpenSnapshot)
frm!txtTEST = rs!TotDays
rs.Close
strSql = "'"

--
Thanks,
Chad


"Dale Fye" wrote:

> Personally, I'd use DCOUNT to do this. It is optimized for this type of
> thing, and requires a lot less code.
>
> Public Function GetExcusedAbsense(intUserID) as integer
>
> Dim strCriteria as string
>
> strCriteria = "[InputDate] >= #" & DateAdd('M',-6,Date()) & "# AND " _
> & "[UserID] = " & intUserID & " AND " _
> & "[InputText]='Excused Absence'"
> GetExcusedAbsense = DCOUNT("InputID", "tblInput", strCriteria)
>
> End Function
>
> If you insist on using your code, then you neen to move the DateAdd( )
> function outside of the quotes, and set it off with # symbols (as I did
> above). Other than that, I think your code looks about right.
>
> HTH
> Dale
>
>
>
> "SoggyCashew" <SoggyCashew RemoveThis @discussions.microsoft.com> wrote in message
> news:EA2A5369-9A4B-4F07-9C83-2787617A9355@microsoft.com...
> > Hello, I am trying to figure out how to write a sql statement that will
> > count
> > how many times excused absence was used within the past 6 months starting
> > on
> > todays date. I wrote the statement below but it doesnt give me any
> > results?
> > What am I missing? Thanks!
> >
> > Public Function GetVacationAndHolidays()
> > Dim strSql
> > Dim frm As Form
> > Dim db As DAO.Database
> > Dim rs As DAO.Recordset
> >
> > Set frm = Forms!frmCalendar
> > Set db = CurrentDb
> >
> > 'TEST
> > strSql = "SELECT Count(tblInput.InputID) AS TotDays FROM tblInput "
> > strSql = strSql & "WHERE tblInput.InputDate = >DateAdd('M',-6,Date())
> > And tblInput.UserID = 2 AND ((tblInput.InputText)='Excused Absence';"
> > Set rs = db.OpenRecordset(strSql, dbOpenSnapshot)
> > frm!txtTEST = rs!TotDays
> > rs.Close
> > strSql = "'"
> >
> >
> > Set rs = Nothing
> > db.Close
> > Set db = Nothing
> > End Function
> >
> >
> >
> > --
> > Thanks,
> > Chad
>
>
>
Back to top
Login to vote
John Spencer

External


Since: Jan 21, 2007
Posts: 444



(Msg. 4) Posted: Sun Aug 17, 2008 1:03 pm
Post subject: Re: VBA SQL statement help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

The operator is ">=" and not "= >".

strSql = "SELECT Count(tblInput.InputID) AS TotDays FROM tblInput "

strSql = strSql & "WHERE tblInput.InputDate >= DateAdd(""M"",-6,Date())
And tblInput.UserID = 2 AND tblInput.InputText='Excused Absence' "



'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================


SoggyCashew wrote:
> Hello, I am trying to figure out how to write a sql statement that will count
> how many times excused absence was used within the past 6 months starting on
> todays date. I wrote the statement below but it doesnt give me any results?
> What am I missing? Thanks!
>
> Public Function GetVacationAndHolidays()
> Dim strSql
> Dim frm As Form
> Dim db As DAO.Database
> Dim rs As DAO.Recordset
>
> Set frm = Forms!frmCalendar
> Set db = CurrentDb
>
> 'TEST
> strSql = "SELECT Count(tblInput.InputID) AS TotDays FROM tblInput "
> strSql = strSql & "WHERE tblInput.InputDate = >DateAdd('M',-6,Date())
> And tblInput.UserID = 2 AND ((tblInput.InputText)='Excused Absence';"
> Set rs = db.OpenRecordset(strSql, dbOpenSnapshot)
> frm!txtTEST = rs!TotDays
> rs.Close
> strSql = "'"
>
>
> Set rs = Nothing
> db.Close
> Set db = Nothing
> End Function
>
>
>
Back to top
Login to vote
SoggyCashew

External


Since: Aug 08, 2008
Posts: 75



(Msg. 5) Posted: Sun Aug 17, 2008 1:03 pm
Post subject: Re: VBA SQL statement help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

John I get an expected: expression error at the ' before the excused absence?
--
Thanks,
Chad


"John Spencer" wrote:

> The operator is ">=" and not "= >".
>
> strSql = "SELECT Count(tblInput.InputID) AS TotDays FROM tblInput "
>
> strSql = strSql & "WHERE tblInput.InputDate >= DateAdd(""M"",-6,Date())
> And tblInput.UserID = 2 AND tblInput.InputText='Excused Absence' "
>
>
>
> '====================================================
> John Spencer
> Access MVP 2002-2005, 2007-2008
> The Hilltop Institute
> University of Maryland Baltimore County
> '====================================================
>
>
> SoggyCashew wrote:
> > Hello, I am trying to figure out how to write a sql statement that will count
> > how many times excused absence was used within the past 6 months starting on
> > todays date. I wrote the statement below but it doesnt give me any results?
> > What am I missing? Thanks!
> >
> > Public Function GetVacationAndHolidays()
> > Dim strSql
> > Dim frm As Form
> > Dim db As DAO.Database
> > Dim rs As DAO.Recordset
> >
> > Set frm = Forms!frmCalendar
> > Set db = CurrentDb
> >
> > 'TEST
> > strSql = "SELECT Count(tblInput.InputID) AS TotDays FROM tblInput "
> > strSql = strSql & "WHERE tblInput.InputDate = >DateAdd('M',-6,Date())
> > And tblInput.UserID = 2 AND ((tblInput.InputText)='Excused Absence';"
> > Set rs = db.OpenRecordset(strSql, dbOpenSnapshot)
> > frm!txtTEST = rs!TotDays
> > rs.Close
> > strSql = "'"
> >
> >
> > Set rs = Nothing
> > db.Close
> > Set db = Nothing
> > End Function
> >
> >
> >
>
Back to top
Login to vote
Dale Fye

External


Since: Jul 06, 2004
Posts: 127



(Msg. 6) Posted: Sun Aug 17, 2008 6:53 pm
Post subject: Re: VBA SQL statement help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Try it like:

strSql = "SELECT Count(InputID) AS TotDays " _
& "FROM tblInput " _
& "WHERE InputDate >= #" & DateAdd("m",-6,Date()) & "# " _
& " AND UserID = 2 " _
& " AND InputText= 'Excused Absence' "

Dale

"SoggyCashew" <SoggyCashew.DeleteThis@discussions.microsoft.com> wrote in message
news:FFEA2473-A7C8-465B-8E0B-7F984B2F147D@microsoft.com...
> John I get an expected: expression error at the ' before the excused
> absence?
> --
> Thanks,
> Chad
>
>
> "John Spencer" wrote:
>
>> The operator is ">=" and not "= >".
>>
>> strSql = "SELECT Count(tblInput.InputID) AS TotDays FROM tblInput "
>>
>> strSql = strSql & "WHERE tblInput.InputDate >= DateAdd(""M"",-6,Date())
>> And tblInput.UserID = 2 AND tblInput.InputText='Excused Absence' "
>>
>>
>>
>> '====================================================
>> John Spencer
>> Access MVP 2002-2005, 2007-2008
>> The Hilltop Institute
>> University of Maryland Baltimore County
>> '====================================================
>>
>>
>> SoggyCashew wrote:
>> > Hello, I am trying to figure out how to write a sql statement that will
>> > count
>> > how many times excused absence was used within the past 6 months
>> > starting on
>> > todays date. I wrote the statement below but it doesnt give me any
>> > results?
>> > What am I missing? Thanks!
>> >
>> > Public Function GetVacationAndHolidays()
>> > Dim strSql
>> > Dim frm As Form
>> > Dim db As DAO.Database
>> > Dim rs As DAO.Recordset
>> >
>> > Set frm = Forms!frmCalendar
>> > Set db = CurrentDb
>> >
>> > 'TEST
>> > strSql = "SELECT Count(tblInput.InputID) AS TotDays FROM tblInput "
>> > strSql = strSql & "WHERE tblInput.InputDate =
>> > >DateAdd('M',-6,Date())
>> > And tblInput.UserID = 2 AND ((tblInput.InputText)='Excused Absence';"
>> > Set rs = db.OpenRecordset(strSql, dbOpenSnapshot)
>> > frm!txtTEST = rs!TotDays
>> > rs.Close
>> > strSql = "'"
>> >
>> >
>> > Set rs = Nothing
>> > db.Close
>> > Set db = Nothing
>> > End Function
>> >
>> >
>> >
>>
Back to top
Login to vote
Display posts from previous:   
       Home -> Office other -> Getting Started All times are: Eastern Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Categories:
 Windows XP
 Windows Vista
 Windows Other
 Office
  Office Other
 Security
 WinRAR
  • Home |
  • Shareware |
  • Windows Tips |
  • Hot Offers |
  • FREE Newsletters |
  • Arcade |
  • Forums |
  • eBooks |
  • About WUGNET |
  • Partners |
  • Contact

  • WUGNET Privacy Policy |
  • Link to WUGNET