Solved

Databases and VB - Help needed

Posted on 2002-04-21
7
139 Views
Last Modified: 2010-05-02
Hi, I have a school assignment to do. It has to involve some sort of a database solution.
I've decided to make an address book (i know its been done and overdone). The problem I'm facing is ADO, or what I found to be the only way of using Access databases.
I have been able to make a database and set out the tables and such, yet I am still unaware of how to put data into it, and then read from it.
I've been reading the whole MSDN which the school has, and still cant find much on it.
Can someone point me in the right direction? Perhaps I should use something else - apart from ADO?

Thanx for everything
0
Comment
Question by:romka
7 Comments
 
LVL 3

Expert Comment

by:sridhar_PJ
Comment Utility
0
 
LVL 6

Expert Comment

by:xSinbad
Comment Utility
I prefer DAO and would use such as follows.

'To connect and open a table;

Dim RS as recordset
Dim DB as Database
Dim strData

set DB = opendatabase("c:\database.mdb")
set RS = DB.Openrecordset("TableName", VBopenDynaset)

'To move through the recordset

' First Record
RS.MoveFirst

'Next Record
RS.MoveNext

' Add recod to a variable
strData = RS!FieldName

' Change data in field
RS.Edit
RS!FieldName = "Whatever"
RS.Update


'Then to clean up afterwards
set RS = Nothing
set DB = Nothing



I hope that helps you out a little


Cheers
Marcus





0
 
LVL 3

Expert Comment

by:sridhar_PJ
Comment Utility
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:alfanhendro
Comment Utility
using DAO,if you use MsAccess 2000, you may need to convert your database to previous version.
-open your database
-select tools
     ->database utility
     ->convert database
     ->to prior access database version

Regards
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 200 total points
Comment Utility
Ok, here is a simple ADO tutorial that you can adapt very easily:

*** Form Code ***

Private cnnAccess As ADODB.Connection
Private rstData As ADODB.Recordset

Private Sub Form_Load()
  'Open Connection
  Set cnnAccess = New ADODB.Connection
  cnnAccess.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\myfolder\mydatabase.mdb;"
  cnnAccess.Open
  'Open Recordset
  Set rstData = New ADODB.Recordset
  rstData.Open "Select * From MyTable",cnnAccess,adOpenStatic,adLockOptimistic
End Sub

Private Sub Form_UnLoad()
  'Close and destroy recordset
  If rstData.State <> adStateClosed Then
    rstData.Close
  End If
  Set rstData = Nothing
  'Close and destroy connection
  If cnnAccess.State <> adStateClosed Then
    cnnAccess.Close
  End If
  Set cnnAccess = Nothing
End Sub

You now have the basics to access your data, you can use the recordset's methods and properties to retrieve/update/insert/delete records.

Private Sub DisplayValues()
  With rstData
    txtField1.Text = .Fields("Field1").Value
    txtField2.Text = .Fields("Field2").Value
  End With
End Sub

Private Sub SaveRecord()
  With rstData
    .Fields("Field1").Value = txtField1.Text
    .Fields("Field2").Value = txtField2.Text
    .Update
  End With
End Sub

Private Sub NewRecord()
  rstData.AddNew
End Sub

And so on.
0
 

Author Comment

by:romka
Comment Utility
Thank you very much for your help.
It so far has helped my project

Thank you
0
 

Author Comment

by:romka
Comment Utility
Thank you very much.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now