Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connecting to Access DB

Status
Not open for further replies.

Blueie

Technical User
May 12, 2012
72
0
0
GB
Hello

Can I ask, please, that someone passes their eye over this to see that I have got it right and that it does what it is supposed to do which is: a) connect to the database; b) open it; c) check that the email address entered by the user is the same as in the Access email column called strEmail; and d) closes Access.

I have spent hours trying to resolve a 'connection doesn't initialize' with cmd.ExecuteNonQuery(), but I am now not getting that error.

Code:
Protected Sub btnPassSend_Click(sender As Object, e As EventArgs) Handles btnPassSend.Click

        Dim conn As New OleDbConnection
        Dim OleDbConnection As New OleDbConnection
        Dim cmd As New OleDbCommand

        ' Check if email field is empty

        If UserEmail.Text = "" Then

            ErrorMsg.Text = "Please complete the required fields"
            ErrorMsg.Visible = True

        End If

        'Connect to Access

        conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|students.mdb;"

        conn.Open()

        cmd = New OleDbCommand("SELECT strEmail FROM university WHERE strEmail=@strEmail", conn)

 'Check the user's email address in the strEmail column in Access corresponds to that entered by the user in 
 'the TextBox whose ID is "UserEmail"

        cmd.Parameters.AddWithValue("@strEmail", UserEmail.Text)

        cmd.Connection = conn

        cmd.ExecuteNonQuery()

        'close Access connection 

        conn.Close()

        'SMTP code follows...

Thanks for your time!
 
What kind of access database is it: 97-2003 i.e. .mdb or 2007 onwards i.e. accdb?

JET4.0 will only connect to an .mdb. If you wish to connect to accdb, use ACE.OLEDB.12.0
 
Hello xwb

Thanks for your reply.

It's Access 2007 (.mdb).
 
Not sure what | does in datasource. Shouldn't it be
Possibly something like

Data Source=DataDirectory\students.mdb
 
No, it is supposed to look like this:

Code:
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|students.mdb;" providerName="System.Data.OleDb"/>

That's the correct path. The database is kept in a folder called App_Data and I think DataDirectory refers to that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top