Link to home
Start Free TrialLog in
Avatar of dbrckovi
dbrckoviFlag for Croatia

asked on

Using MS Access 2000 database (mdb) in Visual Basic 6

Hi!

I have a MS Access 2000 database containing a table.
The table has 10 fields which are of textual or numerical format.

What is the best/simplest/most reliable    way for me to connect to this database in Visual Basic 6?

These are some functions I'd like to be able to perform on those databases:
 - get the data under some Row/Col coordinates in a table
 - change the data under some Row/Col coordinates
 - add new columns (Records)
 - delete existing columns
 - search the table for some data

I'm not very familliar with Access and database programming, and I'd like to keep it as simple as possible.
I'm using the database only becouse I'm planning to have a large amount of data which is too difficult to manage using text files or similar "home-made" methods.

My first attempt to do it was to use Adodc and DataGrid controls to display a table, but I'm wondering if this is the good approach.
It gave me the ability to view and modify the exitsing data, but I was unable to find a way to add new Record (Column) or delete the existing one.

Also I didn't find a way to search through the table.
(There is a way to loop through all cells, and compar the data in each cell with the word I'm looking for, but I'm affraid it will become too slow as the database grows.)

Do you have any suggestions on how to connect to database and be able to perform these simple operations?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Arundel_Castle
Arundel_Castle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dbrckovi

ASKER

Thanks!

I'll try it in a few moments, but in the meanwhile can I ask another quick question?

How can I reset autonumber field in a table? My auto numbers are allways starting from different number (incremental).
I heard somewhere that this is a feature which prevents conflicts, but there is no data which could be conflicted yet.

So can I reset autonumbers to start back from 1?

( I'm not planning to use this field for anything. It's just annoying! )
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.

I have been able to make these basic functions work. It will be enough to start my project.
I know there's a whole world of databases for me to learn, but I'll learn it as I develop my app.

I'm experimental type. I can't help it. I need to have something "visible" before I learn theory.
This is why I never learned databases. All books were trying to teach me complete theory before showing me how this works in practice.

jkwasson

You're right. It's not worth the trouble :-)
This fileld won't be visible to the end user anyway.
About the Auto Number Type Fields in Access Database tables;
Select "Tools > Database Utilities > Compact Database".
If your table has no records, this will reset the value back to 1. If your table has records, this will set the auto number sequence back to the next incremental number already in the field.

bdb
Thanks bdbrown.

Hi again to everyone!

Datagrid sollution still works. :-)
But now I'm trying to examine different approaches.

Currently I'm trying to learn how to use DAO objects to open some database.

I managed to open the databse and a table.
I can get the list of Fields and number of Records.

But I can't figure how to get a specific value from that table.

 - this is my code:
'--------------------------------------------------
Private Sub Command1_Click()
    Dim myDatabase As Database
    Dim myTable As TableDef
    Dim myField As Field

    Set myDatabase = OpenDatabase("e:\davor\videoteka\videoteka2.mdb")
    Set myTable = myDatabase.TableDefs("Filmovi")
    Set myField = myTable.Fields("Broj")
   
    For Each x In myTable.Fields
        Print x.Name
    Next x

    Print myField.Name
    Print myTable.RecordCount
End Sub
'-----------------------------------------------------

If the table looks like this:

======================================================
Number (auto)        Name           Surname              Address                Telephone
-------------------------------------------------------------------------------------------------
    1                    SDFSDa         dFGErgerg           ERTFFFR 45            11324454
    2                    FGHJRRJ        regdsffgdf            dfgsdfer  43            57456455
    3                    JGFBBDE        xcwEWDF            ERGDFErr  4             5645322
    4                    dfgETHh         trteRTEFF           retewrgG 5              12333222
    5                    SDFSDa         ASDASDA            SASDQ 2                 23423626
    6                    rtzGHRT         GHRTTTR            wESDFG2 3             66544433
=======================================================

How do I get a specific value (Cell)?            For example: How do I get and Address value of record under Number 5    (SASDQ 2)  ?

I'm using DAO 3.6.



If someone answers this, I'll open new "Points for ..." question, and point to it.
dbrckovi;

You can use a lookup function on an indexed field using the Table type recordset as follows;
(assuming the field "Number" in your example is the "PrimaryKey" index)

'--------------------------------------------------
Private Sub Command1_Click()
    Dim myDatabase As Database
    Dim myTable As Recordset

    Set myDatabase = OpenDatabase("e:\davor\videoteka\videoteka2.mdb")
    Set myTable = myDatabase.TableDefs("Filmovi", dbOpenTable) ' open as Table-type

    myTable.Index = "PrimaryKey"
    myTable.Seek "=", 5 ' any number you wish to find in the indexed field

    If myTable.NoMatch then
        Print "Record not found"
    Else
         'Record found
         Print myTable.Fields("Address")
    End If

     Set MyTable = Nothing
     MyDatabase.Close

End Sub
'----------------------------------------------------    

You can also use the "FindFirst", "FindNext", "FindLast" methods of a dynaset- or snapshot-type recordset.

    Set myTable = myDatabase.TableDefs("Filmovi", dbOpenDynaset) ' open as Dynaset

    MyTable.FindNext "[Number] = 5"

bdb
Hi!

I get an error in this line:          Set myTable = myDatabase.TableDefs("Filmovi", dbOpenTable)
It says: Wrong number of arguments or invalid property asignment.

As I can see, TableDefs recieves only one argument, but if I remove dbOpenTable, I get Type mismatch error in the same line.

What is wrong?
Sorry,  my mistake - the line should read;

    Set myTable = myDatabase.OpenRecordset("Filmovi", dbOpenTable)  ' open as Table-type

bdb
bdbrown:
Thanks. It works.

I have oppened the "Points for" question here:
https://www.experts-exchange.com/questions/21159100/Points-for-bdbrown.html

Post something there and you'll get the points you deserved.
Avatar of weeb0
weeb0

Would like to ask a question beyond this one.

If access 2000 is not installed on the computer, is it possible to connect to the database ?????

if yes, with which connection type can we use ?  I know that adodb is not working ...

thanx
weeb0;

You should really open a new post for a new question, especially if your not the author of the original question.

bdb