Ask Experts Questions for FREE Help !
Ask
    Mistery1's Avatar
    Mistery1 Posts: 39, Reputation: 1
    Junior Member
     
    #1

    Apr 29, 2009, 03:27 AM
    Inserting an aprostrophe into an Ms access database
    Hi there,

    I have the ffg code so far, which inserts info into the database. But how do I go about inserting/ writing code to insert a surname like O'Reily (take not of the aprostrophe) into the database.

    Imports System.Data.OleDb
    Public Class Form1

    Public cn As OleDbConnection
    'Dim cn As OleDbConnection
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim icount As Integer
    Dim str As String


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Documents and Settings\Mogie\Desktop\Emp.mdb;")
    cn.Open()
    str = "insert into table1 values(" & CInt(EmpNo.Text) & ",'" & Ename.Text & "','" & Department.Text & "')"
    'string stores the command and CInt is used to convert number to string
    cmd = New OleDbCommand(str, cn)
    icount = cmd.ExecuteNonQuery
    MessageBox.Show(icount)
    'displays number of records inserted


    cn.Close()
    End Sub


    End Class
    Perito's Avatar
    Perito Posts: 3,139, Reputation: 150
    Ultra Member
     
    #2

    Apr 29, 2009, 07:17 AM

    Your problem occurs in this line,

    cmd = New OldDbCommand(str, cn)

    where the apostrophe is interpreted as terminating the string since the apostrophe is a terminator in MS Access. When you have a name with an apostrophe in it, modify the name string to have two apostrophes in it:

    "O'Reilly" becomes "O''Reilly" (that's two apostrophes between the "O" and "R", not a quotation mark).
    Mistery1's Avatar
    Mistery1 Posts: 39, Reputation: 1
    Junior Member
     
    #3

    May 15, 2009, 07:19 AM
    Hi There,

    Thanks for this, it really helps.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Access Database [ 5 Answers ]

I keep getting this error message, what should I do? I tried to delete the record where the error appears but the system won't allow me to. (The Microsoft Jet database engine stopped the process because you and another user are attempting to change the same data at the same time.)

Inserting Data To Access From DataGridView [ 3 Answers ]

Hi, I'm trying to build an application where the user could enter data in a datagridview and the it should be inserted into an Access Database (or to SQL Server which ever you will advise me to use) I know who to create a DataSet, I tried creating a new DataTable in the dataset and bind that...

Want Button on Access Form for Inserting OLE Object into Table [ 1 Answers ]

Hi. I'm looking for a simple simple simple solution that will allow me to add a button to an Access form. When the button is clicked, a File Dialog box should appear allowing me to pick any file to insert into the OLE package. I've searched every corner of the web and have found a lot of...


View more questions Search