|
-
Mar 25th, 2010, 04:36 AM
#1
Thread Starter
Lively Member
Update SQL Statement
Should this work, I can't see a problem with it... but it's not working
SQL Code:
Private Sub myAddNew(ByVal CusRef As Integer)
Dim ConnectionString As String
Dim SQLString As String
Dim whichButtonDialogResult As DialogResult
Dim dbCommand As System.Data.OleDb.OleDbCommand
Dim Connection As System.Data.OleDb.OleDbConnection
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data "
ConnectionString += "Source=" & "Opticians.accdb "
Connection = New System.Data.OleDb.OleDbConnection(ConnectionString)
'SQLString = "INSERT INTO SpecSalesTable (DateReceived) "
SQLString = "Update SpecSalesTable "
SQLString += "SET DateReceived = #" & Date.Today & "#"
SQLString += "WHERE SpecSalesID = SpecIDTextBox.Text "
whichButtonDialogResult = MessageBox.Show("Are you sure these details are correct?", "Update!", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If whichButtonDialogResult = DialogResult.Yes Then
Connection.Open()
If CBool(ConnectionState.Open) Then
dbCommand = New System.Data.OleDb.OleDbCommand(SQLString, Connection)
Try
dbCommand.ExecuteNonQuery()
MessageBox.Show("Spectacles Returned, SpecSalesTable Updated! ")
Catch ex As Exception
MessageBox.Show(" Error updating... ") 'IF ERROR DISPLAYS MESSAGE
End Try
End If
End If
Connection.Close()
End Sub
-
Mar 25th, 2010, 06:04 AM
#2
Re: Update SQL Statement
Moved To Database Development
What is the error message?
-
Mar 25th, 2010, 06:12 AM
#3
Thread Starter
Lively Member
Re: Update SQL Statement
Well from that code I'm only getting my own error message, it's connecting to the database, but goes to the catch...
-
Mar 25th, 2010, 07:13 AM
#4
Re: Update SQL Statement
change this
MessageBox.Show(" Error updating... ") 'IF ERROR DISPLAYS MESSAGE
to this
MessageBox.Show(" Error updating... " & System.Enviornment.NewLine & ex.Message) 'IF ERROR DISPLAYS MESSAGE
and post the real error
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Mar 25th, 2010, 07:23 AM
#5
Re: Update SQL Statement
This is your problem"
SQLString += "WHERE SpecSalesID = SpecIDTextBox.Text "
Your text box is inside your strng... so it is LITTERALY USING SpecIDTextBox.Text..... not the contents...
1) In the future, try displaying your SQL in a messagebox before executring it so that you are sure you know what you are executing
2) SQL doesn't know jack about your textboxes, so you can't use the name inside it like that
3) Read the DB FAQ section on parameters, that's the best way to solve the problem.
-tg
-
Mar 25th, 2010, 07:48 AM
#6
Thread Starter
Lively Member
Re: Update SQL Statement
Data type mismatch in criteria expression...
vb Code:
SQLString = "Update SpecSalesTable " SQLString += "SET DateReceived = #" & Date.Today & "#" SQLString += "WHERE SpecSalesID = '" & SpecIDTextBox.Text & "' "
-
Mar 25th, 2010, 07:51 AM
#7
Thread Starter
Lively Member
Re: Update SQL Statement
"Update SpecSalesTable SET DateReceived = #25/03/2010#"
There's the SQL String
-
Mar 25th, 2010, 07:56 AM
#8
Re: Update SQL Statement
Is SpecSalesId a Number or Text?
If a number then
vb.net Code:
SQLString = "Update SpecSalesTable "
SQLString &= "SET DateReceived = #" & Date.Today & "#"
SQLString &= "WHERE SpecSalesID = " & SpecIDTextBox.Text
if text then
vb.net Code:
SQLString = "Update SpecSalesTable "
SQLString &= "SET DateReceived = #" & Date.Today & "#"
SQLString &= "WHERE SpecSalesID = '" & SpecIDTextBox.Text & "'"
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Mar 25th, 2010, 07:59 AM
#9
Thread Starter
Lively Member
Re: Update SQL Statement
Once again Gary your my saviour 
number....
Thanks to everyone!
-
Mar 25th, 2010, 08:51 AM
#10
Re: Update SQL Statement
 Originally Posted by Kielo
"Update SpecSalesTable SET DateReceived = #25/03/2010#"
There's the SQL String
SQLString += "SET DateReceived = #" & Date.Today & "#"
You have got a bug there, and you are likely to notice it next week.
DateReceived is only being set to the 25th of March because there aren't 25 months... on April 1st your SQL statement will include #01/04/2010# , which will mean that January 4th will be stored in the database.
Either append the value to the SQL statement correctly (as explained in the FAQ article How do I use values (numbers, strings, dates) in SQL statements? ), or save yourself lots of hassle for that and lots of other things by following TG's advice.
For an explanation of why you should be using parameters (and links to code examples), see the article Why should I use Parameters instead of putting values into my SQL string? from our Database Development FAQs/Tutorials (at the top of this forum).
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|