INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Connecting to Access DB

Connecting to Access DB

(OP)
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 --> VB

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!

RE: Connecting to Access DB

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

RE: Connecting to Access DB

(OP)
Hello xwb

Thanks for your reply.

It's Access 2007 (.mdb).

RE: Connecting to Access DB

Not sure what | does in datasource. Shouldn't it be \

Possibly something like

Data Source=DataDirectory\students.mdb

RE: Connecting to Access DB

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close