burrina
4StarLounger
Posts: 550 Joined: 30 Jul 2014, 23:58
Post
by burrina » 16 Feb 2022, 10:38
Code: Select all
If DLookup("[PCUserName]", "tblUser", "[PCUserName]='" <> "[ComputerLoginName] & "'")) Then
My syntax is wrong?
HansV
Administrator
Posts: 78524 Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands
Post
by HansV » 16 Feb 2022, 11:07
What exactly do you want to accomplish?
Best wishes,
Hans
burrina
4StarLounger
Posts: 550 Joined: 30 Jul 2014, 23:58
Post
by burrina » 16 Feb 2022, 11:36
I think I have it working Hans. But if there is no user in tblUser like ComputerLoginName which equals Environ("username") Then
' you can't come in.
HansV
Administrator
Posts: 78524 Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands
Post
by HansV » 16 Feb 2022, 12:04
That might be
Code: Select all
If IsNull(DLookup("[PCUserName]", "tblUser", "[PCUserName]='" & [ComputerLoginName] & "'")) Then
or
Code: Select all
If DCount("[PCUserName]", "tblUser", "[PCUserName]='" & "[ComputerLoginName] & "'") = 0 Then
Best wishes,
Hans
burrina
4StarLounger
Posts: 550 Joined: 30 Jul 2014, 23:58
Post
by burrina » 16 Feb 2022, 12:31
Thanks Hans,
burrina
4StarLounger
Posts: 550 Joined: 30 Jul 2014, 23:58
Post
by burrina » 16 Feb 2022, 13:31
Code: Select all
Dim ComputerName As String
Dim GetComputerName As String
Dim sSQL As String
ComputerName = GetComputerName
If IsNull(DLookup("[ComputerName]", "tblUser")) Then
DoCmd.SetWarnings False
sSQL = "INSERT INTO tblUser (ComputerName) VALUES & ('" & ComputerName & "'))"
DoCmd.SetWarnings True
End If
'Why does this not work?
HansV
Administrator
Posts: 78524 Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands
Post
by HansV » 16 Feb 2022, 13:36
The result of the three lines
Code: Select all
Dim ComputerName As String
Dim GetComputerName As String
ComputerName = GetComputerName
is that ComputerName equals the empty string "".
Do you have a function GetComputerName elsewhere in your code? If so, delete the 2nd of the above lines: Dim GetComputerName As String
Best wishes,
Hans
burrina
4StarLounger
Posts: 550 Joined: 30 Jul 2014, 23:58
Post
by burrina » 16 Feb 2022, 13:41
Yes I do.
Tried this, no luck
Code: Select all
Dim User, Criteria As String
Dim ComputerLoginName As String
Dim ComputerName As String
Dim sSQL As String
ComputerLoginName = Environ("UserName")
ComputerName = GetComputerName
If IsNull(DLookup("[ComputerName]", "tblUser")) Then
DoCmd.SetWarnings False
sSQL = "INSERT INTO tblUser (ComputerName) VALUES & ('" & ComputerName & "'))"
DoCmd.SetWarnings True
End If
HansV
Administrator
Posts: 78524 Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands
Post
by HansV » 16 Feb 2022, 13:48
You assign a value to ComputerLoginName but you don't do anything with it. Why not?
Best wishes,
Hans
HansV
Administrator
Posts: 78524 Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands
Post
by HansV » 16 Feb 2022, 13:49
Also, the lines
Code: Select all
If IsNull(DLookup("[ComputerName]", "tblUser")) Then
DoCmd.SetWarnings False
sSQL = "INSERT INTO tblUser (ComputerName) VALUES & ('" & ComputerName & "'))"
DoCmd.SetWarnings True
End If
do exactly nothing: you don't execute the SQL string.
Best wishes,
Hans
burrina
4StarLounger
Posts: 550 Joined: 30 Jul 2014, 23:58
Post
by burrina » 16 Feb 2022, 13:55
CurrentDb.Execute sSQL, dbFailOnError ' does not work?
HansV
Administrator
Posts: 78524 Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands
Post
by HansV » 16 Feb 2022, 14:01
Above the line
sSQL = "INSERT ...
insert
MsgBox sSQL
What do you see?
By the way, does tblUser contain more than one record? If so, you can't predict what DLookup("[ComputerName]", "tblUser") will return, since you don't use the Criteria argument.
Best wishes,
Hans
burrina
4StarLounger
Posts: 550 Joined: 30 Jul 2014, 23:58
Post
by burrina » 16 Feb 2022, 14:11
Here is what I get. Errors ?
You do not have the required permissions to view the files attached to this post.
HansV
Administrator
Posts: 78524 Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands
Post
by HansV » 16 Feb 2022, 14:14
My mistake. The MsgBox line should go BELOW the line
sSQL = "INSERT ...
Best wishes,
Hans
burrina
4StarLounger
Posts: 550 Joined: 30 Jul 2014, 23:58
Post
by burrina » 16 Feb 2022, 14:18
Here is plain vanilla demo, don't understand what I am missing?
You do not have the required permissions to view the files attached to this post.
HansV
Administrator
Posts: 78524 Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands
Post
by HansV » 16 Feb 2022, 14:27
You don't assign a value to ComputerName so it is "". In a previous version you had
ComputerName = GetComputerName
but that is not in your demo. You do assign a value to Criteria but you don't use it.
What are you trying to accomplish?
Best wishes,
Hans
burrina
4StarLounger
Posts: 550 Joined: 30 Jul 2014, 23:58
Post
by burrina » 16 Feb 2022, 14:38
Made it simple. But I gave up and just used an append query. Sorry for the confusion. Code later on being used Hans, not important right now.
Thanks,