vb.net SQL help needed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • juster21
    New Member
    • Feb 2008
    • 24

    vb.net SQL help needed

    I am trying to run this query...
    Code:
    strSql = "SELECT * FROM tblMain WHERE (((tblMain.Verifier_ID)='" & lstVerifier.SelectedItem & "') AND ((tblMain.Week_Ending_Date)='" & lstWeekEndingDate.SelectedItem & "'));"
    ...but I get an error 'Data type mismatch in expression criteria'. The values are a name (text format) and a date (date/time format). How do I modify the query to pull correctly?? thanks!!
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    The problem is the format of the date you are supplying to the query string. SQL Server likes dates in the format YYYY-MM-DD so you need a function to change it to that format before you use it in your query.

    Something like:

    Code:
     
    Private Function FormatDate(ByVal DateIn As DateTime) As String
    	Dim sDate As String = (DateIn.Year & "-" & DateIn.Month & "-" & DateIn.Day).ToString
    	Return sDate
    End Function
    which you can call in your code like this:

    Code:
     
    strSql = "SELECT * FROM tblMain WHERE (((tblMain.Verifier_ID)='" & lstVerifier.SelectedItem & "') AND ((tblMain.Week_Ending_Date)='" & FormatDate(lstWeekEndingDate.SelectedItem) & "'));"
    Let me know if this helps,

    Dr B

    Comment

    • juster21
      New Member
      • Feb 2008
      • 24

      #3
      Originally posted by DrBunchman
      The problem is the format of the date you are supplying to the query string. SQL Server likes dates in the format YYYY-MM-DD so you need a function to change it to that format before you use it in your query.

      Something like:

      Code:
       
      Private Function FormatDate(ByVal DateIn As DateTime) As String
      	Dim sDate As String = (DateIn.Year & "-" & DateIn.Month & "-" & DateIn.Day).ToString
      	Return sDate
      End Function
      which you can call in your code like this:

      Code:
       
      strSql = "SELECT * FROM tblMain WHERE (((tblMain.Verifier_ID)='" & lstVerifier.SelectedItem & "') AND ((tblMain.Week_Ending_Date)='" & FormatDate(lstWeekEndingDate.SelectedItem) & "'));"
      Let me know if this helps,

      Dr B
      Does it matter that I am using Access 2003 as the database?

      Comment

      • DrBunchman
        Recognized Expert Contributor
        • Jan 2008
        • 979

        #4
        [QUOTE=juster21]Does it matter that I am using Access 2003 as the database?[/QUOTE

        It requires only a small change. Instead of delimiting the date with apostrophe's you need to use hash symbols.

        #YYYY-MM-DD# Instead of 'YYYY-MM-DD'. So your sql statement would be:

        Code:
        strSql = "SELECT * FROM tblMain WHERE (((tblMain.Verifier_ID)='" & lstVerifier.SelectedItem & "') AND ((tblMain.Week_Ending_Date)=#" & FormatDate(lstWeekEndingDate.SelectedItem) & "#));"
        Let me know if this works for you.

        Cheers,

        Dr B

        Comment

        • juster21
          New Member
          • Feb 2008
          • 24

          #5
          It requires only a small change. Instead of delimiting the date with apostrophe's you need to use hash symbols.

          #YYYY-MM-DD# Instead of 'YYYY-MM-DD'. So your sql statement would be:

          Code:
          strSql = "SELECT * FROM tblMain WHERE (((tblMain.Verifier_ID)='" & lstVerifier.SelectedItem & "') AND ((tblMain.Week_Ending_Date)=#" & FormatDate(lstWeekEndingDate.SelectedItem) & "#));"
          Let me know if this works for you.

          Cheers,

          Dr B
          wonderful!! thank you!!

          Comment

          Working...