Sean Rhudy
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.OL EDB.4.0;Da ta Source=c:/backup/receiptpr ogram/rece iptmanagem ent/receip t.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])=DateP art(ww,Now ())"
Cmd.CommandType = CommandType.Text
Da = New OleDb.OleDbDataAdapter(Cmd )
Datagridjournal.DataSource = JournalDataSet.Receiptinfo
Da.Fill(JournalDataSet.Rec eiptinfo)
Catch ex As Exception
MsgBox(ex.Message)
Finally
Con.Close()
Cmd.Connection = Nothing
End Try
End Sub
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.OL
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])=DateP
Cmd.CommandType = CommandType.Text
Da = New OleDb.OleDbDataAdapter(Cmd
Datagridjournal.DataSource
Da.Fill(JournalDataSet.Rec
Catch ex As Exception
MsgBox(ex.Message)
Finally
Con.Close()
Cmd.Connection = Nothing
End Try
End Sub
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')'.
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.Y ear & " AND Month([pdate]) = " & Me.DateTimePicker1.Value.M onth & " AND DatePart(ww,[pdate]) = DatePart(ww, '" & Me.DateTimePicker1.Value.T oString("y yyyMMdd") & "')"
"SELECT * FROM Receiptinfo WHERE Year([pdate]) = " & Me.DateTimePicker1.Value.Y
ASKER
Now I get the following error:
No value given for one or more required parameters.
No value given for one or more required parameters.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you!!!
No worries - sorry it took me so many attempts! Haven't done any work in access for some time now :)
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.Y
Then the month:
Cmd.CommandText = "SELECT * FROM Receiptinfo WHERE Year([pdate]) = " & Me.DateTimePicker1.Value.Y
And finally the week:
"SELECT * FROM Receiptinfo WHERE Year([pdate]) = " & Me.DateTimePicker1.Value.Y
Note that you might need to change the format of the ToString("") to suit.
Cheers,
Hillwaaa