[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

Get Database Time and not local computer

I have the following code in VB6 below.  The Now function retrieves the time on the computer.  However, I am interacting with my SQL database and would like to get the Now function to get the time of the database.  

how can I do this?


If Day(Now) < 9 Then
            selectMonth = Format(DateAdd("m", -1, Now), "mmmm")
            frmDataEnvAttendance.Combo3 = selectMonth
            frmDataEnvAttendance.Combo3.Locked = True
            Else
            selectMonth = Format(Now, "mmmm")
            frmDataEnvAttendance.Combo3 = selectMonth
            frmDataEnvAttendance.Combo3.Locked = True
            End If

Open in new window

0
al4629740
Asked:
al4629740
2 Solutions
 
jtdebeerCommented:
Run this query:

Select Getdate()

0
 
al4629740Author Commented:
Is this how the code should change?

2:
3:
4:
5:
6:
7:
8:
9:
If GetDate(Now) < 9 Then
            selectMonth = Format(DateAdd("m", -1, Now), "mmmm")
            frmDataEnvAttendance.Combo3 = selectMonth
            frmDataEnvAttendance.Combo3.Locked = True
            Else
            selectMonth = Format(Now, "mmmm")
            frmDataEnvAttendance.Combo3 = selectMonth
            frmDataEnvAttendance.Combo3.Locked = True
            End If

Open in new window

0
 
hiteshgoldeneyeCommented:
hi just to elaborate jtdebeer's solution
please refer this link for more information
http://www.sqlteam.com/article/dates-in-sql-server

in VB6
you will need to execute a query, if you are not familiar with how to achieve this then do the following

Add a Reference to Microsoft ActiveX Data Objects 2.x library

Then use the following code

Dim constr As String,currentServerTime as Variant
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sqlQuery As String

constr = "Data Source=10.48.133.101;Initial Catalog=Email-LOG;User Id=sme;Password=sme;Provider=SQLOLEDB.1;"
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
con.Open constr
sqlQuery = "Select getdate()"
rs.Open sqlQuery, con, adOpenStatic, adLockReadOnly
currentServerTime = rs.fields(0)
rs.close
con.close

If Day(currentServerTime ) < 9 Then
            selectMonth = Format(DateAdd("m", -1, currentServerTime ), "mmmm")
            frmDataEnvAttendance.Combo3 = selectMonth
            frmDataEnvAttendance.Combo3.Locked = True
            Else
            selectMonth = Format(currentServerTime , "mmmm")
            frmDataEnvAttendance.Combo3 = selectMonth
            frmDataEnvAttendance.Combo3.Locked = True
            End If
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now