Solved

Databases and VB - Help needed

Posted on 2002-04-21
7
140 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
ID: 6958831
0
 
LVL 6

Expert Comment

by:xSinbad
ID: 6958848
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
ID: 6958911
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:alfanhendro
ID: 6958994
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
ID: 6959357
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
ID: 6970473
Thank you very much for your help.
It so far has helped my project

Thank you
0
 

Author Comment

by:romka
ID: 6970496
Thank you very much.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

911 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

20 Experts available now in Live!

Get 1:1 Help Now