Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to open the Data file one time ?
Message
Information générale
Forum:
Visual Basic
Catégorie:
Bases de données DAO/RDO/ODBC/ADO
Divers
Thread ID:
00362527
Message ID:
00362636
Vues:
50
>Thank you for your reply.
>
>I am using ADO. Kindly, can you show me an example.
>
>Rgrds
>
>
>>>Hi, I am new to VB and trying hardly to work on it,
>>>and I appreciate any help.
>>>
>>>I am opening the data file "mdb" with each form I
>>>create, but I don't think this is the correct way to
>>>do it.
>>>I think that I should open the file one time
>>>when I load the application and then deal with the
>>>tables around the application.
>>>
>>>Can anyone tell me how to do this, and in which form.
>>
>>Which database access method are you using? DAO? ADO? RDO?
>>
>>The rule is normally to open a connection, keep it in a global variable and use this variable through the entire application!

This is one way of going that kind of things (there is many roads to ...):

1) Start a new Standard EXE project
2) Add a Standard module to the project
3) Set a reference to Microsoft ADO
4) In the project properties, set the Startup object to "Sub Main"
5) Paste this code to the standard module.
Option Explicit

Public gobjConn As ADODB.Connection

Public Sub Main()
    'This will open a global connection
    Set gobjConn = New ADODB.Connection
    With gobjConn
        'Change the data source property in the following line
        .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Program Files\Microsoft Visual Studio\VB98\Nwind2000.mdb;Persist Security Info=False"
        .Open
    End With
    
    'Now that the connection is open, display the application
    Form1.Show
End Sub
6) Add a Datagrid to Form1.
7) Paste this code to Form1.
Option Explicit

Private Sub Form_Load()
Dim rstData As ADODB.Recordset

    Set rstData = New ADODB.Recordset
    With rstData
        'This is where we use the global connection.
        .ActiveConnection = gobjConn
        .CursorLocation = adUseClient
        .CursorType = adOpenStatic
        .LockType = adLockReadOnly
        .Source = "SELECT * FROM Employees"
        .Open
    End With
    Set DataGrid1.DataSource = rstData
End Sub
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform