Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. SQL query

SQL query

Scheduled Pinned Locked Moved Visual Basic
databasealgorithms
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amanjot
    wrote on last edited by
    #1

    I am trying to populate a backup Access Table for specific range of dates; however, the whole table is getting backed up. It works fine with other fields (eg. the commented myCmd statement). Any suggestion will be appreciated Dim sDate, eDate, cmd As String 'starting date and end date sDate = DateTimePicker1.Value eDate = DateTimePicker2.Value Dim ssdate As Date = DateTimePicker1.Value '******* Start of Nash-Sutcliffe Efficiency Algorithm ******** '------- Populating dailySummary Table ----- myCmd = "SELECT * INTO dailySummary FROM " & inpTable & " WHERE " & fdate & " > " & ssdate 'myCmd = "SELECT * INTO dailySummary FROM " & inpTable & " WHERE " & obsDaily & " BETWEEN 4.0 AND 5.0" cmmd = New OleDbCommand(myCmd, myConnection) MsgBox(myCmd) cmmd.ExecuteScalar()

    D 1 Reply Last reply
    0
    • A Amanjot

      I am trying to populate a backup Access Table for specific range of dates; however, the whole table is getting backed up. It works fine with other fields (eg. the commented myCmd statement). Any suggestion will be appreciated Dim sDate, eDate, cmd As String 'starting date and end date sDate = DateTimePicker1.Value eDate = DateTimePicker2.Value Dim ssdate As Date = DateTimePicker1.Value '******* Start of Nash-Sutcliffe Efficiency Algorithm ******** '------- Populating dailySummary Table ----- myCmd = "SELECT * INTO dailySummary FROM " & inpTable & " WHERE " & fdate & " > " & ssdate 'myCmd = "SELECT * INTO dailySummary FROM " & inpTable & " WHERE " & obsDaily & " BETWEEN 4.0 AND 5.0" cmmd = New OleDbCommand(myCmd, myConnection) MsgBox(myCmd) cmmd.ExecuteScalar()

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You have multiple problems with this. The first is that your SQL statement is wrong. The WHERE clause is comparing your first date with the second, and alway socmming up with True, so that's why you're getting every record in the table instead of a range of them. You should be comparing the field in the table you want to those two dates, not the two dates to each other. Second, dates in Access must be wrapped in # characters. You don't have any of those in your SQL string, so the dates are not even valid, no matter what they look like. Lastly, you're using string concantenation to build the SQL query. This has numerous problems with it as dates in strings can be in many different formats and that might not match the date format Access is using. Use parameterized queries instead. Read this[^] for an explanation of why concantenating strings together to build your SQL statements is a bad thing, and how to use parameterized queries to eliminate some of the problems you're having passing dates into the SQL.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups