Switch to: V12V11V10V9V8V7V6V5

Lesson 2 - "Employees" Database on Server

In the previous lesson we have created an application for the work with local database. In the given lesson we will improve the application in such a way that it will be able to work with both local and remote databases located under Valentina Server. We will use the project got in the step 6 of the lesson 1 as the basis.

Step 1: Project Preparation for the Work with Server

Modification of initialization

Copy folder Lesson1\step6 to the convenient place for you. Open project file.

To solve the task of initialization modification you should create a new global variable mClient.

To do this you should do the next:

  • At top of the source add the code:
Dim mClient As Boolean 
Dim mConn As New VConnection

VDatabase class object creation modification

To do it you should do the next:

  • Double Click on Form1.
  • Change the text of Form_Load() in order it looks like this:
Private Sub Form_Load()
    mClient = True

    If mClient Then
        Valentina.InitClient
    Else
        Valentina.Init 32
    End If

    CreateOrOpenDatabase

modification of CreateOrOpenDatabase() function:

  • Double Click on Form1.
  • Change the text of CreateOrOpenDatabase() in order it looks like this:
    Dim f As String
    
    f = "dbPersons.vdb"

    Set mDatabase = New VDatabase

    If mClient Then
        mConn.Init "localhost", "sa", "sa"
        mConn.Open
        mDatabase.InitClient mConn 
    Else
        mDatabase.InitLocal
    End If

    'If Database exists we open it, otherwise we create it.
    On Error GoTo Err
    mDatabase.Open f
    Set mTblPerson = mDatabase.Table("Person")

    ' move to the first record
    mTblPerson.FirstRecord
    PopulateFields
    Exit Sub
    
Err:
    If Valentina.ErrNumber <> 532480 Then
        mDatabase.Create f
        CreateStructure
        PopulateFields
    Else
        MsgBox Valentina.ErrString
        Unload Me
    End If

If Valentina Server is located on another computer, you should change “localhost” for the address of your server.

Project start

In order to start this example you should have Valentina Server already started. For better viewing of this example work we recommend you to start server in terminal window and to have VerboseLevel = 3 in ini-file of the server. Look in ValentinaServer.pdf more precisely

After this project started, you may pay attention that database will be created not in its folder, but in the folder where Valentina Server stores database.

Step 2: Adding error checking

Improving Form_Load() event

To do this you should do the next:

  • Double Click on Form1.
  • Change the text of Form_Load() in order it looks like this:
Private Sub Form_Load()
    mClient = True
    
    On Error GoTo Err
    
    Set mDatabase = New VDatabase
        
    If mClient Then
        Valentina.InitClient
        
        mConn.Init "localhost", "sa", "sa"
        mConn.Open
        
        mDatabase.InitClient mConn
    Else
        Valentina.Init 9 * 1048576
        
        mDatabase.InitLocal
    End If
   
    CreateOrOpenDatabase
    
    Exit Sub
    
Err:
    MsgBox Valentina.ErrString
    Unload Me
End Sub

Changes in Form_CreateOrOpenDatabase() procedure

To do this you should do the next:

  • Double Click on Form1.
  • Change the text of CreateOrOpenDatabase() in order it looks like this:
Private Sub CreateOrOpenDatabase()
    Dim f As String
    
    f = "dbPersons.vdb"

    'If Database exists we open it, otherwise we create it.
    On Error GoTo Err
    mDatabase.Open f
    Set mTblPerson = mDatabase.Table("Person")

    ' move to the first record
    mTblPerson.FirstRecord
    PopulateFields
    Exit Sub
    
Err:
    If Valentina.ErrNumber <> 532480 Then // not a ConnectionError
        mDatabase.Create f
        CreateStructure
        PopulateFields
    Else
        MsgBox Valentina.ErrString
        Unload Me
    End If
End Sub