Loading

Wednesday, April 7, 2010

Visual Basic - Synchronize Client Computer Date/Time From a SQL Server

The code below is used to synchronize the client computer from MS SQL Server, sometimes the users  may change their computer local time in-order to trick the system that he/she encoded the data entry on time. Oh, if you're the database administrator or programmer it's a very headache how to figure it out, why it is happen this way. Sample scenario, accounts receivable should be encoded prior to cut-off period, then user name "Michelle" forgot to encode the data, a day after cut-off period she realize encode the accounts receivable data, she might change the local time in-order to encode in previous dates. Basically, visual basic program compares the entry date (date picker component) and the system/local time, if she change the system date less than current date, you might wonder why the data not posted in ledger to think that all entries are encoded on timely basis. So, how to prevent it. Use the code below and don't forget to create a stored procedure called "Get_Server_Date". Call this function as many as you can for example during Loan Event, Add New Entry, Edit Entry, Save Entry, Delete Entry. Anyway it's very fast, the user can't recognize that you're synchronizing from the server time.
Note: I assume the all your data tables has these two or three fields
User_Name - the one who transact the data
DateEncoded/Modified  - the transaction date base on user's local time.
Computer Name - Optional, it use to trace the users location.
If it's not added to your data table, please do apply the fields it may help you trace your transactions.

'------ VB Code ----------
'Declaration
Dim Const mCon="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Mydatabase;Data Source=MyComputerName\SQLEXPRESS"



'Note : Change the Mydatabase to your Database Name
 '            MyComputerNmae = Your Database Server Name





Private Sub Form_Load()




       Call SynChronize_Date_Server()

End Sub



Function SynChronize_Date_Server()
On Error Resume Next
    Dim tmp1 As Date
    With Main.ado1   ' Add adodc1 object to the form and name it ado1
        .ConnectionString = mCon
        .CommandType = adCmdStoredProc
        .RecordSource = "sp_getserverdate;1"
        .Refresh
        Date = Format(.Recordset!ServerDate, "mm/dd/yy")
        Time = Format(.Recordset!ServerDate, "hh:mm:ss")
    End With
End Function

Function Get_Server_Date() As Date

On Error Resume Next
    Dim tmp1 As Date
    With Main.ado1
        .ConnectionString = mCon
        .CommandType = adCmdStoredProc
        .RecordSource = "sp_getserverdate;1"
        .Refresh
        Get_Server_Date = Format(.Recordset!ServerDate, "mm/dd/yy")
    End With
End Function
'-------------------------------- STORED PROCEDURED CODE ----------------------------


USE [Inventry]
GO
/****** Object:  StoredProcedure [dbo].[sp_getserverdate]    

Script Date: 04/20/2010 01:19:56 ******/

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  proc [dbo].[sp_getserverdate]
as
set nocount on
select getdate() as ServerDate




















SHARE TWEET

Thank you for reading this article Visual Basic - Synchronize Client Computer Date/Time From a SQL Server With URL https://x-tutorials.blogspot.com/2010/04/visual-basic-synchronize-client.html. Also a time to read the other articles.

0 comments:

Write your comment for this article Visual Basic - Synchronize Client Computer Date/Time From a SQL Server above!