Multiple DLookup Criteria

User avatar
burrina
4StarLounger
Posts: 550
Joined: 30 Jul 2014, 23:58

Multiple DLookup Criteria

Post by burrina »

Code: Select all

 If DLookup("[PCUserName]", "tblUser", "[PCUserName]='" <> "[ComputerLoginName] & "'")) Then
My syntax is wrong?

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple DLookup Criteria

Post by HansV »

What exactly do you want to accomplish?
Best wishes,
Hans

User avatar
burrina
4StarLounger
Posts: 550
Joined: 30 Jul 2014, 23:58

Re: Multiple DLookup Criteria

Post by burrina »

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.

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple DLookup Criteria

Post by HansV »

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

User avatar
burrina
4StarLounger
Posts: 550
Joined: 30 Jul 2014, 23:58

Re: Multiple DLookup Criteria

Post by burrina »

Thanks Hans,

User avatar
burrina
4StarLounger
Posts: 550
Joined: 30 Jul 2014, 23:58

Re: Multiple DLookup Criteria

Post by burrina »

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?

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple DLookup Criteria

Post by HansV »

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

User avatar
burrina
4StarLounger
Posts: 550
Joined: 30 Jul 2014, 23:58

Re: Multiple DLookup Criteria

Post by burrina »

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

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple DLookup Criteria

Post by HansV »

You assign a value to ComputerLoginName but you don't do anything with it. Why not?
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple DLookup Criteria

Post by HansV »

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

User avatar
burrina
4StarLounger
Posts: 550
Joined: 30 Jul 2014, 23:58

Re: Multiple DLookup Criteria

Post by burrina »

CurrentDb.Execute sSQL, dbFailOnError ' does not work?

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple DLookup Criteria

Post by HansV »

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

User avatar
burrina
4StarLounger
Posts: 550
Joined: 30 Jul 2014, 23:58

Re: Multiple DLookup Criteria

Post by burrina »

Here is what I get. Errors ?
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple DLookup Criteria

Post by HansV »

My mistake. The MsgBox line should go BELOW the line

sSQL = "INSERT ...
Best wishes,
Hans

User avatar
burrina
4StarLounger
Posts: 550
Joined: 30 Jul 2014, 23:58

Re: Multiple DLookup Criteria

Post by burrina »

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.

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Multiple DLookup Criteria

Post by HansV »

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? :scratch:
Best wishes,
Hans

User avatar
burrina
4StarLounger
Posts: 550
Joined: 30 Jul 2014, 23:58

Re: Multiple DLookup Criteria

Post by burrina »

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,