Access 2007 & VB 2008 - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Access 2007 & VB 2008

PsyborgPsyborg Posts: 5Member
I have a real problem here. I am connecting to an access 2007 database (accdb) via an OLEDB connection, and I have no idea why this code isn't working!
[code]Imports System.Data.OleDb
Public Class Form1

Private Sub Form1_Load(...)
Dim dbCon As OleDbConnection
Dim dbCmd As OleDbCommand
Dim dbRead As OleDbDataReader

dbCon = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:My.accdb;Persist Security Info=False;")
dbCon.Open()

dbCmd = New OleDbCommand("SELECT * FROM CITY", dbCon)
dbRead = dbCmd.ExecuteReader
Do While dbRead.Read
MsgBox(dbRead(1))
Loop
End Sub
End Class
[/code]
The result is the program crashes with no errors. I tried it in a different form/project and it launches the forum without any messages. So, in short, I'm getting no error messages and I don't know why. It will not go past dbCon.Open(). Please help!

Comments

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    If the program is crashing, it has to be throwing an exception...

    to get the error message try this:

    [code]
    Private Sub Form1_Load(...)
    Dim dbCon As OleDbConnection
    Dim dbCmd As OleDbCommand
    Dim dbRead As OleDbDataReader
    try
    dbCon = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:My.accdb;Persist Security Info=False;")
    dbCon.Open()
    catch ex as exception
    msgBox(ex.Message, vbOkOnly, "Error During dbCon.Open()")
    return
    End Try

    dbCmd = New OleDbCommand("SELECT * FROM CITY", dbCon)
    dbRead = dbCmd.ExecuteReader
    Do While dbRead.Read
    MsgBox(dbRead(1))
    Loop
    End Sub

    [/code]

    My gut feeling is that this is a connection string issue
    try this:
    [code]
    Dim DBName as String = "c:path_of_dbdbname.dbext"
    Dim ConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBName & ";User ID=Admin;Password="

    dbCon = New OleDbConnection(ConString)
    [/code]
  • PsyborgPsyborg Posts: 5Member
    Thanks! I tested everything out, and it still didn't work. I was looking around, and I realized that it's because first, I'm using the compiler in x64 so it can't find the provider, and second that I was using the code in the form load event, so it wouldn't throw errors.
  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    Did you find a solution?

    If not, you might find a more proper connection string (that has a provider that you can see on your x64 box) here: http://www.connectionstrings.com/

    I go there a lot when I am writing con-strings for DB's I don't normally work with.

    Good Luck!
    Sean Campbell - firesickle.com
  • PsyborgPsyborg Posts: 5Member
    Yeah, thanks. I changed the compiler to target x86 machines, and moved the code to a button event. Thanks for the help!
Sign In or Register to comment.