DateTimePicker, Access

torg

Well-known member
Joined
Nov 7, 2002
Messages
60
Location
Norway
I`ve got a DateTimePicker DtpDate

Code:
 dtpDate.CustomFormat = "dd.MM.yyyy"

I`ve got an access database with a table
Code:
 table tblExpenses(amount, Date)
tblExpenses.Date format format is DateTime (i.e : dd.mm.yyyy (11.01.2003))

I want to select sum(Amount) from tblExpenses where tblexpenses.date = dtpDate.value

Code:
Dim Date As String = dtpDate.Value.Month
            Dim Expences As String = cboUtgift.Text
            adExpences= New OleDb.OleDbDataAdapter("SELECT Sum(tblExpences.Amount) AS Totals  from tblExpences where " & Date & " = Month(tblExpences.Date) ", Me.OleDbConnection)
            adExpences.Fill(dsExpences, "dtExpences")
             Me.OleDbConnection.Close()

What am I missing?

give me a code example if possible
 
Forgot to say: the code acts like the "WHERE" clause doesn`t work. As if the comparison isn`t a valid comparison. Anybody got an Idea about how to solve this problem?
 
You have it backwards, try this...

Code:
"......from tblExpences where tblExpences.Date = #" & dtpDate.value & "#",
 
I tried as you described, but received following message:
"Syntax error in Date in query expression "tblExpences.Date = #19.01.2003"".

tableExpemces.Date has Date/Time as Datatype, and short Date as format. dtpDate format is Short

Code:
adInntekt = New OleDb.OleDbDataAdapter("select sum(tblExpences.Amount) as Tor FROM tblExpenceswhere tblExpences.date = #" & dtpDate.value & "# ", Me.OleDbConnection)
 
It may have a poblem with the field name Date, its a reserved word. In that case use square brackets...



.....Where [date] =....
 
I solved the problem by doing the following:
Code:
Dim myString As String = dtpDato.Value
    Dim aString As String
        aString = (Replace(myString, ".", "/"))
        MessageBox.Show(aString)

        Try
            Me.OleDbConnection.Open()
    Dim Expences As String = cboExpences.Text
            adExpences= New OleDb.OleDbDataAdapter("select (tblExpences.Amount) as tot FROM tblExpences where tblExpences.date = #" & aString & "#;) ", Me.OleDbConnection)
            adExpences.Fill(dsExpences, "dtExpences")
             Me.OleDbConnection.Close()
 
Yeah, my fault, I forgot that dtpDato is returning a date type, so you could simply eliminate the pound (#) sign in you SQL string.
 
btw, its preferable to use Replace from the .NET library...

Code:
aString = myString.Replace(".","/")
 
Last edited by a moderator:
Back
Top