Results 1 to 16 of 16

Thread: Database Command Not working

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    14

    Database Command Not working

    Hey guys,

    I'm trying to run an Update statement through VB.Net to my database, code is as follows:

    VB Code:
    1. Dim strConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\jcarney\My Documents\Visual Studio Projects\WindowsApplication2\WindowsApplication2\Relearn.mdb"
    2.     Dim cnnDatabase As New OleDb.OleDbConnection(strConnectionString)
    3.  
    4. Private Sub btnScrambler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScrambler.Click
    5.         tableName = "CustomerID"
    6.         Dim myCommand As New OleDb.OleDbCommand
    7.         Try
    8.             cnnDatabase.Open()
    9.             myCommand.Connection = cnnDatabase
    10.             myCommand.CommandText = "UPDATE CustomerID " & _
    11.                                     "SET [Name]= REPLACE(Name,'M','u') " & _
    12.                                     "WHERE CustomerID;"
    13.  
    14.             myCommand.ExecuteNonQuery()
    15.  
    16.             Dim strSQL = "SELECT * FROM " & tableName
    17.             Dim adpDatabase As New OleDb.OleDbDataAdapter(strSQL, cnnDatabase)
    18.             Dim dstRelearn As New DataSet
    19.             adpDatabase.Fill(dstRelearn, "Parts")
    20.  
    21.             Dim rowParts As DataRow
    22.             Dim litParts As ListViewItem
    23.             For Each rowParts In dstRelearn.Tables("parts").Rows
    24.                 litParts = lvwTable.Items.Add(rowParts("CustomerID"))
    25.                 litParts.SubItems.Add(rowParts("Name"))
    26.                 litParts.SubItems.Add(rowParts("Address"))
    27.                 litParts.SubItems.Add(rowParts("City"))
    28.             litParts.SubItems.Add(rowParts("State"))
    29.             Next
    30.         Catch ex As Exception
    31.             MsgBox(ex.Message)
    32.         Finally
    33.             cnnDatabase.Close()
    34.         End Try
    35.     End Sub


    I keep getting the following error:

    "Undefined function 'REPLACE' in expression."

    any help would greatly be appreciated.

  2. #2
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    Re: Database Command Not working

    wouldn't you want.

    VB Code:
    1. myCommand.CommandText = "UPDATE CustomerID " & _
    2.                                     "SET [Name[B]]=" & REPLACE(Name,'M','u')  [/B] & _
    3.                                     " WHERE CustomerID;"


    ?
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    14

    Re: Database Command Not working

    thanks, got rid of that error, but now:

    VB Code:
    1. myCommand.CommandText = "UPDATE CustomerID " & _
    2.                                         "SET [Name]=" & REPLACE(Name, "M", "u")   & _
    3.                                         " WHERE CustomerID;"

    gives me the error:

    "Syntax error (missing operator) in query expression 'Form1WHERE CustomerID'"

    Any help here?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Database Command Not working

    Strings in SQL need to be surrounded by ' characters, eg:
    VB Code:
    1. "SET [Name]= '" & REPLACE(Name, "M", "u") & "'"  & _

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    14

    Re: Database Command Not working

    Thanks so much for helping me out guys,

    Now when my form loads and I run the SQL statement it changes all values in my "Name" column to "Form1" instead of just changing the "M"'s to "u"'s???

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Database Command Not working

    Ah, I see what you want now.. to do that you need to use functions that are specific to the DBMS you are using. As you are using Access, the Replace function as you originally had it (the version that TokersBall_CDXX and I posted uses the name of the form, instead of the field called name) should be fine.. I can't think of any alternatives right now.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    14

    Re: Database Command Not working

    VB Code:
    1. myCommand.CommandText = "UPDATE CustomerID " & _
    2.                                     "SET [Name]= REPLACE(Name,'M','u') " & _
    3.                                     "WHERE CustomerID;"

    so you're saying this should work? (It throws a "Undefined function 'Replace' in expression" error when I try to run it after my form loads)

  8. #8
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Database Command Not working

    My guess is, if you think Name is a reserved word (I don't remember for sure in Access), be consistent on making it literal:
    VB Code:
    1. myCommand.CommandText = "UPDATE CustomerID " & _
    2.                                     "SET [Name]= REPLACE([Name],'M','u') " & _
    3.                                     "WHERE CustomerID;"
    EDIT:
    I just looked it up:
    http://support.microsoft.com/kb/q209187/
    It is indeed reserved.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    14

    Re: Database Command Not working

    I've done this already, thanks for pointing it out though, and I still get the same error...

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Database Command Not working

    You need to use an alternative to REPLACE, but I'm having problems thinking of any.. I'll have a play around and post back later.

  11. #11

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    14

    Re: Database Command Not working

    Thank you, I really appreciate all the help guys!!

  12. #12

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    14

    Re: Database Command Not working

    Sidenote,

    Is there a way to take a value from the table and pass it into an array. For example:

    VB Code:
    1. Dim strNames As String
    2. Dim aryNames()
    3.  
    4. Try
    5.             cnnDatabase.Open()
    6.             cmdDatabase.Connection = cnnDatabase
    7.             cmdDatabase.CommandText = "SELECT * FROM CustomerID WHERE CustomerID = 1001;"
    8.  
    9.             Dim rdrMyReader As OleDb.OleDbDataReader = cmdDatabase.ExecuteReader
    10.  
    11.             rdrMyReader.Read()
    12.             strNames = rdrMyReader("Name")
    13.  
    14. 'Then take the strNames string and pass it into the array aryNames() such that each character occupies one space.  So if the name was "Sue" it would be
    15. aryNames(0) = S
    16. aryNames(1) = u
    17. aryNames(2) = e

    ???

  13. #13
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Database Command Not working

    You can turn any string into a character array by using the String.ToCharArray method.

  14. #14
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: Database Command Not working

    Hi!! Just a ideia why dont u reduce ur code instead of
    VB Code:
    1. Dim strConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\jcarney\My Documents\Visual Studio Projects\WindowsApplication2\WindowsApplication2\Relearn.mdb"
    2.     Dim cnnDatabase As New OleDb.OleDbConnection(strConnectionString)
    3.  
    4. Private Sub btnScrambler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScrambler.Click
    5.         tableName = "CustomerID"
    6.         Dim myCommand As New OleDb.OleDbCommand
    7.         Try
    8.             cnnDatabase.Open()
    9.             myCommand.Connection = cnnDatabase
    10.             myCommand.CommandText = "UPDATE CustomerID " & _
    11.             "SET [Name]= REPLACE(Name,'M','u') " & _
    12.             "WHERE CustomerID;"
    13.             myCommand.ExecuteNonQuery()
    14.  
    15.             Dim strSQL = "SELECT * FROM " & tableName
    16.             Dim adpDatabase As New OleDb.OleDbDataAdapter(strSQL, cnnDatabase)
    17.             Dim dstRelearn As New DataSet
    18.             adpDatabase.Fill(dstRelearn, "Parts")
    19.  
    20.             Dim rowParts As DataRow
    21.             Dim litParts As ListViewItem
    22.             For Each rowParts In dstRelearn.Tables("parts").Rows
    23.                 litParts = lvwTable.Items.Add(rowParts("CustomerID"))
    24.                 litParts.SubItems.Add(rowParts("Name"))
    25.                 litParts.SubItems.Add(rowParts("Address"))
    26.                 litParts.SubItems.Add(rowParts("City"))
    27.             litParts.SubItems.Add(rowParts("State"))
    28.             Next
    29.         Catch ex As Exception
    30.             MsgBox(ex.Message)
    31.         Finally
    32.             cnnDatabase.Close()
    33.         End Try
    34.     End Sub

    U could put:

    VB Code:
    1. Dim cnnDatabase As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\jcarney\My Documents\Visual Studio Projects\WindowsApplication2\WindowsApplication2\Relearn.mdb")
    2. Private Sub btnScrambler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScrambler.Click
    3.         Dim myCommand As New OleDb.OleDbCommand("UPDATE CustomerID " & "SET [Name]= REPLACE(Name,'M','u') " & "WHERE CustomerID;", cnnDataBase
    4.         Try
    5.             cnnDatabase.Open()
    6.             myCommand.ExecuteNonQuery()
    7.  
    8.             Dim adpDatabase As New OleDb.OleDbDataAdapter("SELECT * FROM CustomerID", cnnDatabase)
    9.             Dim dstRelearn As New DataSet
    10.             adpDatabase.Fill(dstRelearn, "Parts")
    11.  
    12.             Dim rowParts As DataRow
    13.             Dim litParts As ListViewItem
    14.             For Each rowParts In dstRelearn.Tables("parts").Rows
    15.                 litParts = lvwTable.Items.Add(rowParts("CustomerID"))
    16.                 litParts.SubItems.Add(rowParts("Name"))
    17.                 litParts.SubItems.Add(rowParts("Address"))
    18.                 litParts.SubItems.Add(rowParts("City"))
    19.             litParts.SubItems.Add(rowParts("State"))
    20.             Next
    21.         Catch ex As Exception
    22.             MsgBox(ex.Message)
    23.         Finally
    24.             cnnDatabase.Close()
    25.         End Try
    26.     End Sub

    It's just a ideia of mine...
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Database Command Not working

    Both of the following work fine for me in Access (I cant test your code at the moment):
    VB Code:
    1. "SET [Name]= REPLACE([Name],'M','u') " & _
    VB Code:
    1. "Left([Name], Instr(1,[Name],'M')-1) & 'u' & " & _
    2.             "Mid(FName, Instr(1,FName,'M')+1)
    ..the second one only changes one character tho, so you'll need to run it repeatedly until no records are changed (to check this you will also need to use a Where clause that limits to values that contain the letter).
    Last edited by si_the_geek; Jun 13th, 2006 at 02:32 PM.

  16. #16

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    14

    Re: Database Command Not working

    Ok so I've changed my plan of attack on this...

    My question now is... is there a way to step through an entire column of information without knowing what the last field's value is going to be.

    For example: repeat until Field value = null... or something like that?

    Here's my new code (using two List boxes so I can quickly double check that operations/functions work correctly)...

    VB Code:
    1. Dim strConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\jcarney\My Documents\Visual Studio Projects\WindowsApplication2\WindowsApplication2\Relearn.mdb"
    2.     Dim cnnDatabase As New OleDb.OleDbConnection(strConnectionString)
    3.     Dim cmdDatabase As New OleDb.OleDbCommand
    4.     Dim tableName As String
    5.     Sub frmThinker_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Try
    7.             cnnDatabase.Open()
    8.             tableName = "CustomerID"
    9.  
    10.             Dim strSQL = "SELECT * FROM " & tableName
    11.             Dim adpDatabase As New OleDb.OleDbDataAdapter(strSQL, cnnDatabase)
    12.  
    13.             Dim dstRelearn As New DataSet
    14.             adpDatabase.Fill(dstRelearn, "Parts")
    15.  
    16.             Dim rowParts As DataRow
    17.             Dim litParts As ListViewItem
    18.             For Each rowParts In dstRelearn.Tables("parts").Rows
    19.                 litParts = lvwOne.Items.Add(rowParts("CustomerID"))
    20.                 litParts.SubItems.Add(rowParts("Name"))
    21.                 litParts.SubItems.Add(rowParts("Address"))
    22.                 litParts.SubItems.Add(rowParts("City"))
    23.                 litParts.SubItems.Add(rowParts("State"))
    24.             Next
    25.         Catch ex As Exception
    26.             MsgBox(ex.Message)
    27.         Finally
    28.             cnnDatabase.Close()
    29.         End Try
    30.     End Sub
    31.  
    32.     Sub btnButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton.Click
    33.         Dim strNames As String
    34.         Dim aryNames() As Char
    35.         tableName = "CustomerID"
    36.  
    37.         Try
    38.             '***Convert Values
    39.             cnnDatabase.Open()
    40.             cmdDatabase.Connection = cnnDatabase
    41.             cmdDatabase.CommandText = "SELECT * FROM CustomerID WHERE CustomerID = 1001;"
    42.  
    43.             Dim rdrMyReader As OleDb.OleDbDataReader = cmdDatabase.ExecuteReader
    44.             rdrMyReader.Read()
    45.             strNames = rdrMyReader("Name")
    46.             aryNames = strNames.ToCharArray()
    47.  
    48.             Dim intArrayCounter As Integer = 0
    49.             Dim intCurrent As Short = 0
    50.             Dim intNew As Short = 0
    51.  
    52.             Do Until intArrayCounter = 10
    53.                 If aryNames(intArrayCounter) = "M" Then
    54.                     aryNames(intArrayCounter) = "u"
    55.                End If
    56.                 intArrayCounter += 1
    57.             Loop
    58.            
    59.             cnnDatabase.Close()
    60.  
    61.  
    62.             '***Update Database
    63.             cnnDatabase.Open()
    64.             Dim myCommand As New OleDb.OleDbCommand
    65.             myCommand.Connection = cnnDatabase
    66.             myCommand.CommandText = "UPDATE CustomerID SET [Name]= '" & aryNames & "' WHERE CustomerID = 1001;"
    67.             myCommand.ExecuteNonQuery()
    68.             cnnDatabase.Close()
    69.  
    70.  
    71.  
    72.             '***Fill Second Grid
    73.             cnnDatabase.Open()
    74.             Dim strSQL = "SELECT * FROM " & tableName
    75.             Dim adpDatabase As New OleDb.OleDbDataAdapter(strSQL, cnnDatabase)
    76.  
    77.  
    78.             Dim dstRelearn As New DataSet
    79.             adpDatabase.Fill(dstRelearn, "Parts")
    80.  
    81.             Dim rowParts As DataRow
    82.             Dim litParts As ListViewItem
    83.             For Each rowParts In dstRelearn.Tables("parts").Rows
    84.                 litParts = lvwTwo.Items.Add(rowParts("CustomerID"))
    85.                 litParts.SubItems.Add(rowParts("Name"))
    86.                 litParts.SubItems.Add(rowParts("Address"))
    87.                 litParts.SubItems.Add(rowParts("City"))
    88.                 litParts.SubItems.Add(rowParts("State"))
    89.             Next
    90.         Catch ex As Exception
    91.             MsgBox(ex.Message)
    92.         Finally
    93.             cnnDatabase.Close()
    94.         End Try
    95.  
    96.     End Sub

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