VB3 vs VB + Access

PederSpendrup

New member
Local time
Today, 11:02
Joined
Jul 28, 2009
Messages
4
Years ago I worked a lot with VB3 and databases,no complicated things just very simple and straigt forward.
For adding new data I used to use the below VB3 code

Dim Db As Database,T as table
Set Db = OpenDatabase(" database.mdb" )
Set T=Db.OpenTable("tablename")
T.AddNew
T ("Field name" )= Text1
:
:
T.Update
T.Close
Db.Close

Now after years of other activities I am trying to understand VB6.
Apparently a number of things have changed and I can not get this code right in VB6.
Have downloaded a lot of books and tips but it is all very confusing mixed up with errorhandlers etc.
Would appriciate any help with a simple code.
I am using VB6 and Access 2002-2003 or Access 2007
Thanks in advance
Peder
 
You didn't mention what the problem is.

You might want to check and make sure you have the right references. You'll probably need to include references to Access and/or DAO.
 
Dim Db As Database,T as table
Set Db = OpenDatabase(" database.mdb" )
Set T=Db.OpenTable("tablename")
T.AddNew
T ("Field name" )= Text1
:
:
T.Update
T.Close
Db.Close

To the best of my knowledge Table has been depricated as an object type. You need to add the reference to DAO and use the Recordset type. If DAO is not an option you could use ADO.

Code:
Dim Db As Database,T as DAO.Recordset
Set Db = OpenDatabase(" database.mdb" )
Set T=Db.OpenRecordset("tablename")
T.AddNew
T ("Field name" )= Text1
:
:
T.Update
T.Close
Db.Close
 
My problem is that I can not get this simple code right in VB6.
VB6 is in many aspects quite different to VB3.So my problem is to write a code that can add new records to an Access database table.
Peder
 
Thank you very much.
I have tried the code which in my opinion should work but unfortunately
I get the following error message:
Compile error userdefined type not defined.Then
Dim Db As Database is marked as beeing the error.
Have got the same error with other code trials.

Peder
 
Thank you very much.
I have tried the code which in my opinion should work but unfortunately
I get the following error message:
Compile error userdefined type not defined.Then
Dim Db As Database is marked as beeing the error.
Have got the same error with other code trials.

Peder

Set a reference to Microsoft DAO 3.6 and then change your code to

Dim Db As DAO.Database

You may need other things pertaining to DAO and also you may need to use SQL DDL to do table alterations -

ALTER TABLE ...etc.
 
Sorry but I still get the same error.
If I use the same code in the VB for Access 2007 then " T as DAO.Recordset" is the error.

As I don´t like to give up I spent the day trying and finally came to the following which appears to work.
Just remains to do the rest in the project.

Private Sub Command1_Click()
Dim dbsDatabasename As DAO.Database
Dim rstrecordsetname As DAO.Recordset

Set dbsDatabasename = CurrentDb
Set rst10 = dbsDatabasename.OpenRecordset("recordsetname")

rstrecordsetname.AddNew
rstrecordsetname!Fieldname = Text21


rstrecordsetname.Update
rstrecordsetname.Close
dbsDatabasename.Close


End Sub

Thanks for all help

Peder
 
Last edited:

Users who are viewing this thread

Back
Top Bottom