Code for connecting to Microsoft Access databse, opening and closing connection

Dim cn As New OleDbConnection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Data.mdb;Persist Security Info=True"
cn.Open()
cn.Close()

hello sir. i tried that code but it doesn't work. it says that it's not defined. what should i do?

did you add a header to it ? add 'imports system.data.oledb' as the header and try

which dbr u using-- access 2003 or 2007 ??
the code is for 2003
for 2007 use
"Provider=Microsoft.ACE.OLEDB.11.0;Data Source=|DataDirectory|Data.ACCdb;Persist Security Info=FALSE"

N A MSGBOX

'firs import

imports system.data.oledb
Dim con As New OleDbConnection()
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=h:\MAINDB.mdb;"
con.open
statements
con.close