p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Visual Basic 2005 Basics (http://p2p.wrox.com/forumdisplay.php?f=137)
-   -   visual basic 2005 (http://p2p.wrox.com/showthread.php?t=53788)

nidhigupta001 February 8th, 2007 01:25 AM

visual basic 2005
 
I want to connect my database in ms-access to vb 2005 and also want to show the first record..can u please tell me how can i do it...



nadhan_f February 9th, 2007 09:59 PM

Hi nidhigupta001,

I'm a learner. The below sample code might help you in connecting Access through VB.
N regarding displaying the first record, Its all how you want to retrieve the table in the .mdb and use accordingly as the database may hold any N number of tables.
Correct me if I'm wrong.
------------------------------------
Public con As New ADODB.Connection
Public rs As New ADODB.Recordset
Private Sub ConTest_Click()
    Dim con As New ADODB.Connection
    ' rs is the record set
    Dim rs As New ADODB.Recordset
    ' the (source=) is the path of the database
    con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ConnectionTest\EmpInfo.mdb;Persist Security Info=False"
End Sub

Aspire Inspire Achieve

anubisascends February 11th, 2007 04:06 PM

You can also try this:

Dim myCommand as new OledbCommand
Dim myConnection as new OledbConnection([Connection String])

myCommand.Connection = myConnection
myCommand.CommandText = [SQL string]

myCommand.Connection.Open()
myCommand.[command type]
myCommand.Connection.Close()

There are a few tricks to connecting to an Access Database. You need to know the connection string, you can locate the connection string at www.Connectionstrings.com. Older versions of Access use the Jet engine, which will need to be stated in the connection string, as nadhan_f showed in the con.Open line, while Access 07 uses a different engine.

The command text needs to show the sql string that you are going to execute (ie "Select * from sto_Employees")

The command type will tell the application what to run, some examples are:
ExecuteQuery - used for select queries
ExecuteNonQuery - used for insert, update and delete queries
ExecuteReader - used to run a data reader

What command type you use will depend on what you are trying to do.

Hope this helps.

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.
Albert Einstein
US (German-born) physicist (1879 - 1955)


All times are GMT -4. The time now is 04:11 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.