Link to home
Start Free TrialLog in
Avatar of Sean Rhudy
Sean RhudyFlag for United States of America

asked on

using monthcalendar to query

Hello, I have a form called journal.vb, right now as it is, the btnSearch button is clicked and any records in the current month and year are displayed in a datagrid.  The field in the database I am using is called "pdate".  I have a monthcalendar named "monthcalendar1" I would like the user to be able to choose a date on the calendar, then either choose year, month, or week.  Of course I would no longer have a search button, just 3 buttons named year, month, and week.  If the date 12/03/2006 is selected on the calendar, and the user clicks year, all records with a pdate in 2006 will be shown, if they click month, all records with a pdate of december 2006 will be shown, and if week is clicked, all records with a pdate in that week will be shown.  Here is the code I am working with:

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim Da As OleDb.OleDbDataAdapter


        Try
            Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/backup/receiptprogram/receiptmanagement/receipt.mdb"
            Con.Open()
            Cmd.Connection = Con
        Catch ex As Exception
        End Try



        Try
            JournalDataSet.Clear()
            Cmd.CommandText = "SELECT * FROM Receiptinfo WHERE Month([pdate]) = Month(Now()) AND Year([pdate]) = Year(Now()) AND DatePart(ww,[pdate])=DatePart(ww,Now())"

            Cmd.CommandType = CommandType.Text
            Da = New OleDb.OleDbDataAdapter(Cmd)
            Datagridjournal.DataSource = JournalDataSet.Receiptinfo
            Da.Fill(JournalDataSet.Receiptinfo)

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Con.Close()
            Cmd.Connection = Nothing
        End Try
    End Sub
Avatar of Hillwaaa
Hillwaaa
Flag of Australia image

Hi seanrhudy,

Ok - firstly, I'd suggest using a DateTimePicker instead of a MonthCalendar, as you only need to select one date.  

Then, you can set the commandtext as follows (for the year option):
Cmd.CommandText = "SELECT * FROM Receiptinfo WHERE Year([pdate]) = " & Me.DateTimePicker1.Value.Year

Then the month:
Cmd.CommandText = "SELECT * FROM Receiptinfo WHERE Year([pdate]) = " & Me.DateTimePicker1.Value.Year & " AND Month([pdate]) = " & Me.DateTimePicker1.Value.Month

And finally the week:
"SELECT * FROM Receiptinfo WHERE Year([pdate]) = " & Me.DateTimePicker1.Value.Year & " AND Month([pdate]) = " & Me.DateTimePicker1.Value.Month & "DatePart(ww,[pdate]) = DatePart(ww, '" & Me.DateTimePicker1.Value.ToString("yyyyMMdd") & "')"

Note that you might need to change the format of the ToString("") to suit.

Cheers,
Hillwaaa
Avatar of Sean Rhudy

ASKER

Month and Year work fine.  When I choose week, I get the following error:

Syntax error(missing operator) in query expression 'Year([pdate]) = 2006 AND Month([pdate]) = 12DatePart(ww,[pdate]) = DatePart(ww, '20061205')'.
There was a missing space and 'AND' - sorry! - try this:

"SELECT * FROM Receiptinfo WHERE Year([pdate]) = " & Me.DateTimePicker1.Value.Year & " AND Month([pdate]) = " & Me.DateTimePicker1.Value.Month & " AND DatePart(ww,[pdate]) = DatePart(ww, '" & Me.DateTimePicker1.Value.ToString("yyyyMMdd") & "')"
Now I get the following error:

No value given for one or more required parameters.
ASKER CERTIFIED SOLUTION
Avatar of Hillwaaa
Hillwaaa
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!!!
No worries - sorry it took me so many attempts!  Haven't done any work in access for some time now :)