Results 1 to 3 of 3

Thread: Making SQL statements work

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    Making SQL statements work

    I have some databases that are Access files. These files are getting full and I am trying to create a program to remove some of the files not needed. The Problem that I have is the code, which I will post below, only deletes one table and not the others in the code. Am I missing something?

    VB Code:
    1. Dim xCon As New OleDb.OleDbConnection
    2.         Dim xCom As New OleDb.OleDbCommand
    3.         Dim xAdap As New OleDb.OleDbDataAdapter(xCom)
    4.         Dim xComBuild As New OleDb.OleDbCommandBuilder(xAdap)
    5.         Dim ConnnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Store2Office\Emp.mdb;Mode=ReadWrite;Persist Security Info=False"
    6.  
    7.  
    8.         Dim emp As String = "Emp.mdb"
    9.         Dim hqomfox As String = "hqomfox.mdb"
    10.         Dim s2o As String = "s2o.mdb"
    11.         Dim product As String = "product.mdb"
    12.         Dim s2ohourly As String = "s2ohourly.mdb"
    13.         Dim inventory As String = "inventory.mdb"
    14.         Dim composer As String = "composer.mdb"
    15.         Dim e2 As String = "e2.mdb"
    16.  
    17.         'EMP.MDB and tables
    18.  
    19.         xCom.CommandText = "Delete * from charges where s_date<#" & TextBox1.Text & "#"
    20.         xCom.ExecuteNonQuery()
    21.         xCom.CommandText = "Delete * from clockchanges where indate<#" & TextBox1.Text & "#"
    22.         xCom.ExecuteNonQuery()
    23.         xCom.CommandText = "Delete * from clockdata where indate<#" & TextBox1.Text & "#"
    24.         xCom.ExecuteNonQuery()
    25.         xCom.CommandText = "Delete * from mhj_schedule where s_date<#" & TextBox1.Text & "#"
    26.         xCom.ExecuteNonQuery()
    27.         xCom.CommandText = "Delete * from schedule where indate<#" & TextBox1.Text & "#"
    28.         xCom.ExecuteNonQuery()
    29.         xCom.Connection = xCon
    30.         xCon.ConnectionString = ConnnectionString
    31.  
    32.         If xCon.State = ConnectionState.Closed Then
    33.             xCon.Open()
    34.         End If
    35.         xCom.ExecuteNonQuery()
    36.         xCon.Close()

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Making SQL statements work

    You cannot execute a command if the connection is not open. You're calling ExecuteNonQuery five times before you open the connection, then you open the connection and call ExecuteNonQuery once. It's only that last one that will work. You have to open the connection first, then execute all your commands, then close the connection.

    Also, don't use a TextBox for date entry. Use a DateTimePicker.
    VB Code:
    1. xCom.Connection = xCon
    2.         xCon.ConnectionString = ConnnectionString
    3.         xCon.Open()
    4.  
    5.         xCom.Parameters.AddWithValue("@date", Me.DateTimePicker1.Value.Date)
    6.         xCom.CommandText = "DELETE FROM charges WHERE s_date < @date"
    7.         xCom.ExecuteNonQuery()
    8.         xCom.CommandText = "DELETE FROM clockchanges WHERE indate < @date"
    9.         xCom.ExecuteNonQuery()
    10.         xCom.CommandText = "DELETE FROM clockdata WHERE indate < @date"
    11.         xCom.ExecuteNonQuery()
    12.         xCom.CommandText = "DELETE FROM mhj_schedule WHERE s_date < @date"
    13.         xCom.ExecuteNonQuery()
    14.         xCom.CommandText = "DELETE FROM schedule WHERE indate < @date"
    15.         xCom.ExecuteNonQuery()
    16.  
    17.         xCon.Close()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    Re: Making SQL statements work

    Thanks I will try that.. and I just checked out the datetimepicker. Thanks I like that idea.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width