contractor
Having Fun With Access
- Local time
- Yesterday, 16:00
- Joined
- Apr 12, 2012
- Messages
- 47
Cronk, The only code that i have provided was to get network username, the other part belongs to himself...
Gee, this has been a labored thread.
Contractor, be aware that the code you supplied last neither declared tmpDB, nor set it as a database object.
Rookie, dates are stored as double precision numbers but displayed in date format with the integer part representing the number of days since 30/12/1899 and the fraction being the part of 24 hours eg 0.25 is 6:00:00[FONT="][/FONT]
Option Compare Database
Option Explicit
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Function GetUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If lngX <> 0 Then
GetUserName = Left$(strUserName, lngLen - 1)
Else
GetUserName = ""
End If
End Function
A very important advice, do not use On Error Resume Next until you exactly know why you would avoid the error.. With that statement you will fail to understand the actual error.. In this case the Execute statement is never run..
Use a proper Error handler..
funwithaccess.. Here you go.. I am still using the old code I posted in Post#7
You can simply use Environ("username") to get the windows username.. List of Environ variables available @UtterAccessWiki.. Although I have seen various posts describing that usage of an API is better.. So I have used Rain's code to get the Username..
I was not sure why you had defined the loginDate and loginTime as String in the table..
I quote this again..
Here is the updated version..
tmpDB.Execute "INSERT INTO [Usage Log](NTID, loginDate, loginTime) VALUES(" & Chr(34) & GetUserName() & Chr(34) & ", " & CDbl(Date) & ", " & CDbl(TimeValue(Now)) & ")"
tmpDB.Execute "INSERT INTO [Usage Log](NTID, loginDate, loginTime) VALUES(" & Chr(34) & GetUserName() & Chr(34) & ", " & CDbl(Date) & ", " & CDbl(TimeValue(Now)) & ")"
@Rain : I am intrigued to know a better way of writing the INSERT statement..This code executes an SQL Statement. It is not written very well but it should work.
The line will Execute the CurrentDB (declared as tmpDB) to INSERT INTO the table (Usage Log), the VALUES GetUserName(), Date and Time into the fields NTID, loginDate, loginTime.
That is all that one line does..
@Rain : I am intrigued to know a better way of writing the INSERT statement..
It's not about disagreeing Rain. The comment, "not written well" regarding the INSERT query made me think twice..Let me know if you disagree.
Private Sub cmdFind_Click()
Dim tmpDB As DAO.Database
Dim qry As QueryDef
Dim intCount As Integer
Set tmpDB = CurrentDB
For Each qry In tmpDB.QueryDefs
DoCmd.Close acQuery, qry.Name, acSaveYes
Next
Combo99.Value = ""
Combo89.Value = ""
Combo71.Value = ""
Combo55.Value = ""
Combo85.Value = ""
Combo212.Value = ""
intCount = 0
If DCount("Alias", "SD Documentation Query") > 0 Then
intCount = intCount + 1
DoCmd.OpenQuery "SD Documentation Query", acViewNormal, acReadOnly
End If
If DCount("SearchableAlias", "Info Gathering Query") > 0 Then
intCount = intCount + 1
DoCmd.OpenQuery "Info Gathering Query", acViewNormal, acReadOnly
End If
If DCount("AssetNumber", "Xerox Assets Query") > 0 Then
intCount = intCount + 1
DoCmd.OpenQuery "Xerox Assets Query", acViewNormal, acReadOnly
End If
If DCount("AssetNumber", "Xerox Assets Query - CHP") > 0 Then
intCount = intCount + 1
DoCmd.OpenQuery "Xerox Assets Query", acViewNormal, acReadOnly
End If
If DCount("[IP Address]", "Xerox IP Query") > 0 Then
intCount = intCount + 1
DoCmd.OpenQuery "Xerox IP Query", acViewNormal, acReadOnly
End If
If DCount("[IP Address]", "Xerox IP Query - CHP") > 0 Then
intCount = intCount + 1
DoCmd.OpenQuery "Xerox IP Query", acViewNormal, acReadOnly
End If
If DCount("SerialNumber", "Xerox Serial Query") > 0 Then
intCount = intCount + 1
DoCmd.OpenQuery "Xerox Serial Query", acViewNormal, acReadOnly
End If
If DCount("SerialNumber", "Xerox Serial Query - CHP") > 0 Then
intCount = intCount + 1
DoCmd.OpenQuery "Xerox Serial Query", acViewNormal, acReadOnly
End If
If intCount = 0 Then MsgBox "No results found in ServiceBase." & vbCrLf & "Please provide a specific word, phrase, or alias.", vbExclamation + vbOKOnly, "ServiceBase Search Results"
tmpDB.Execute "INSERT INTO [Usage Log](NTID, loginDate, loginTime) VALUES(" & Chr(34) & GetUserName() & Chr(34) & ", " & CDbl(Date) & ", " & CDbl(TimeValue(Now)) & ")"
cmdFind_Click_Exit:
Exit Sub
End Sub
It's not about disagreeing Rain. The comment, "not written well" regarding the INSERT query made me think twice..
Well I understand what you mean by surplus usage of declaration of the tmpDB. I consider me a miser when it comes to declaring objects,in general terms, declaring an object would create a local object, so in case you need to perform multiple executions the same one object referring to the DB can be used efficiently, instead of creating and emptying everytime CurrentDB is used.. I am not sure if I am explaining it properly here..
However for this particular operation, CurrentDB would suffice..![]()
funwithaccess
I would think NOW is as good a Time as any to learn some naming conventions.
See attached.
Chr(34) is Double Quotes mark.. What Longer Strings are you dealing with?