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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB to Access 1

Status
Not open for further replies.

tk7834

Programmer
Jul 9, 2002
15
US
I'm thinking this is a fairly easy question, but I have never done it before.....

I'm making a simple VB app that can pull from and add values to an Access database. I've got the db set up, but what do I need to do in VB to get it hooked up? So far I've created a data from the toolbar on the VB form and searched for my mdb database in the DatabaseName, but I'm not sure where to go from here. Any ideas? Thanks!
 
I use ADO like this (you need a reference to Microsoft ActiveX Data Objects 2.5)
Code:
    Dim con as ADODB.Connection
    Dim rs as ADODB.Recordset
    Dim connectionString as String
    Dim sqlQuery as String

    set con = New ADODB.Connection
    set rs = New ADODB.Recordset

    connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                       "Data Source=c:\db1.mdb" & _
                       ";Jet OLEDB:Database Password=myPwd"

    con.open connectionString

    sqlQuery = "select myField from myTable"

    with rs
        .Open sqlQuery, con
    
        if not (.bof and .eof) then
            .moveFirst
            do while not .eof
                debug.print rs(0)
                .moveNext
            loop
        else
            debug.print "Empty recordset"
        end if
    end with

    rs.Close
    set rs = Nothing
    set con = Nothing




 
Or data objects 2.6 or 2.7....

Or if that is confusing to you you can add a Data Environment to your project under the Project menu or a Data Object that you can configure through the IDE (next to last object in the toolbox of a default project).
 
You could also add a new form to your project, choosing the Data Wizard form. When asked, select the ADO code option, and you'll be able to learn most of the basics of data access from pre-written code.

OK its not perfect code, and you will later learn a few refinements, but it will get you started on the code method of db access

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top