'窗体中调用代码
Private Sub TestCode()
Dim sTime As String
'取时间
sTime = Format(GetSQLSvrTime(), "Short Time") ' 短时间
Debug.Print sTime
'取日期:
Dim sDate As String
sDate = Format(GetSQLSvrTime(), "Long Date") ' 长日期
Debug.Print sDate
End Sub
'将此函数放在ACCESS的标准模块中
Public Function GetSQLSvrTime()
Dim rst As ADODB.Recordset
Dim strConn As String
Dim strSQL As String
Dim strServer As String
Dim strServerUser As String
Dim strPWS As String
Dim strDB As String
Set rst = New ADODB.Recordset
strServer = "192.168.1.12" '服务器IP地址
strServerUser = "sa" '服务器用户名
strPWS = "123456" '服务器密码
strDB = "AccDev_be" '数据库名称
strConn = "Provider=MSDataShape.1;Persist Security Info=True;Data Source=" & strServer & ";" _
& "User ID=" & strServerUser & ";Password=" & strPWS & ";" _
& "Initial Catalog=" & strDB & ";Data Provider=SQLOLEDB.1"
strSQL = "SELECT GETDATE() AS SvrTime"
rst.Open strSQL, strConn, adOpenDynamic, adLockOptimistic
GetSQLSvrTime = rst.Fields("SvrTime")
End Function