Ask Experts Questions for FREE Help!
 

Free Answers in 3 Easy Steps

Register Now
3 Steps
 


Ask QuestionsprogressAnswer QuestionsprogressBuild ReputationprogressBecome an Expert
 
At Ask Me Help Desk you can ask questions in any topic and have them answered for free by our experts. To ask questions or participate in answering them you must register for a free account. By registering you will be able to:
  • Get free answers from experts in any of our 300+ topics.
  • Accept money for answers that you provide.
  • Communicate privately with other members (PM).
  • See fewer ads.
  Answer this Question    Ask about Visual Basic    Ask about another Subject  
 

Mistery1
Apr 29, 2009, 03:27 AM
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
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
May 15, 2009, 07:19 AM
Hi There,

Thanks for this, it really helps.