Create a "Trial" Period

This fairly short piece of code allows the program to check whether a trial period of any length has expired, notifying the user how many days are left. The user is given the option to register the program or just not use it any more. It uses the VB registry setting functions. Encryption can be added when saving to the registry so that almost nobody at all can use the program for longer than the trial period.

Option Explicit

Private Const TRIAL_PERIOD_DAYS As Integer = 30

Private Function TrialPeriodDaysLeft(DaysTrial As Integer) As Integer
Dim DateStart As Date

DateStart = GetSetting(App.Title, "Trial Period", "Date Start", 0)
If DateStart = 0 Then
   SaveSetting App.Title, "Trial Period", "Date Start", Date
   TrialPeriodDaysLeft = DateDiff("d", DateStart, Date) > DaysTrial
End If
End Function

Private Sub AppSetRegistered(Registered As Boolean)
SaveSetting App.Title, "Trial Period", "Registered", Registered
End Sub

Private Function AppRegistered() As Boolean
AppRegistered = GetSetting(App.Title, "Trial Period", "Registered", False)
End Function

Private Sub Form_Load()
If Not AppRegistered Then
   Dim DaysLeft As Integer
   DaysLeft = TrialPeriodDaysLeft(TRIAL_PERIOD_DAYS)
   If DaysLeft < 0 Then
       If MsgBox("The trial period for " & App.Title & " has expired." & vbCrLf & _
         "Would you like to register to continue using this program?", vbQuestion + vbYesNo) = vbYes Then
           ' Open up order form here
           ' Use: 'AppSetRegistered True' to register program
           ' Exit your program here
       End If
       MsgBox "You have " & DaysLeft & " " & IIf(DaysLeft = 1, "day left", "days left") & " to use this program.", vbInformation
   End If
End If
End Sub

You might also like...




Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.” - Antoine de Saint Exupéry