Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150

    Another Dlookup query string issue

    Annoying.
    Trying to pass a string - from a specific field's record - to the Dlookup to see for matches.

    This code works:

    Code:
    Sub CrossRefUserID()
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("UsersConnected")
    Dim cName As String
    
    
    cName = "DELLT75"
    
    
    rs.Edit
        rs.Fields("UserID").Value = DLookup("UserID", "Employees", "Domain='" & cName & "'")
    
    
    
    
    End Sub
    However... When passing string variable cName from recordset like....

    Code:
    Code:
    Sub CrossRefUserID()
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("UsersConnected")
    Dim cName As String
    
    
    rs.Edit
        'cName = rs.Fields("computer name").Value
        rs.Fields("UserID").Value = DLookup("UserID", "Employees", "Domain='" & cName & "'")
    
    
    
    
    End Sub
    The Dlookup function throws an error about the query string syntax. Doing a msgbox on cName & in the second code returns a value. So... i'm guessing this is related to the super fun query string in the crtieria for Dlookup.

    Any ideas?



    Thanks in advance.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    What value is there and what error is thrown?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150
    Quote Originally Posted by pbaldy View Post
    What value is there and what error is thrown?

    Hi,

    Please see below

    Code:
    Sub CrossRefUserID()
    
    
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("UsersConnected")
    Dim cName As String, userid As String
    
    
    
    
    'cName = "BobsPC"  - No error when passing name directly
    rs.Edit
        cName = rs.Fields("computer name").Value
        MsgBox cName ' this returns message box with "BobsPC" - OK! So why would it not work below?
        rs.fields("userid") = DLookup("UserID", "Employees", "[Domain] = '" & cName & "'")
    
    
    End Sub
    Edit: The error is 3075 - syntax on query string or whatever. Its with the Dlookup, which makes absolutely no sense based on the evidence i've just provided.

  4. #4
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150
    It is very clear the issue Dlookup is having is with this line...

    Code:
    cName = rs.Fields("computer name").Value
    As soon as the cName stirng variable is referenced by the recordset, Dlookup has a fit over it.

    Also prevalent in DCount. The syntax is right. When using Dcount with directly passing cName, i get 1 return. As soon as the rs.fields line is used to defined cName, then boom. Error.

  5. #5
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Try moving the rs.edit line:
    Code:
    'cName = "BobsPC"  - No error when passing name directly
    
    
        cName = rs.Fields("computer name").Value
        MsgBox cName ' this returns message box with "BobsPC" - OK! So why would it not work below?
    rs.Edit
        rs.fields("userid") = DLookup("UserID", "Employees", "[Domain] = '" & cName & "'")
    
    
    End Sub

  6. #6
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150
    Hi,


    Solved the issue by changing the syntax... which makes no sense how you can pass a string directly with quotes. But when the string is referenced from an object, it won't accept. Newsflash, its still a string. If that wasn't extremely frustrating for the last 2 hours....






    Correct syntax (referencing variable cName as string through the recordset field):

    Code:
    cName = rs.Fields("computer name").Value
    userid = DCount("UserID", "Employees", "Domain=' & cName & '")

    Thanks for your guys help.

  7. #7
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150
    OK Update.

    While I prematurely solved the thread... this is not solved.

    Apparently it is impossible to use Dlookup or Dcount with an input variable as criteria from a recordset.

    The following returns no syntax error, but returns 0 matches (there is a match - trust me)

    Code:
    Dim cName As Variant, matchCnt As Integer
    
            cName = rs.Fields("computer name").Value
           
            matchCnt = DCount("UserID", "Employees", "Domain=' & cName & '")
            MsgBox matchCnt
    The following returns no syntax error, but the syntax has changed and the cName variable is directly referenced, and it FINDS A MATCH

    Code:
    Dim cName As Variant, matchCnt As Integer
    
    
    cName = "JEFF752"
    
    
           
    matchCnt = DCount("UserID", "Employees", "Domain='" & cName & "'")
    MsgBox matchCnt
    So, the issue is now that DCount is not finding matches when it should be. I prove this with the first code listed above where...

    Code:
    Dim cName As Variant, matchCnt As Integer
    
            cName = rs.Fields("computer name").Value
           msgbox cName 
            matchCnt = DCount("UserID", "Employees", "Domain=' & cName & '")
            MsgBox matchCnt
    The msgbox returns... yep.. you guessed it. "JEFF752"... and returns 0 for msgbox matchcnt...


    So basically this Dlookup function is pointless.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    The quotes are incorrect in the failing version.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150
    Hi,

    Which failing version? They are all failing in my opinion

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Really? You said this syntax worked. Use it.
    Quote Originally Posted by ironfelix717 View Post
    The following returns no syntax error ... and it FINDS A MATCH

    matchCnt = DCount("UserID", "Employees", "Domain='" & cName & "'")
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Don't know your exact table setup, but this worked for me...
    Keep in mind, if UserID in UsersConnected is an autonumber, it can't be updated.

    Click image for larger version. 

Name:	Lookup.png 
Views:	14 
Size:	21.7 KB 
ID:	36109
    Last edited by davegri; 11-03-2018 at 12:19 PM. Reason: more

  12. #12
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150
    Thanks for the reply davegri... I return with more information.

    I am so frustrated with Access right now. This is clearly a bug.

    When creating a dummy database the following code works. Syntax is correct and returns matches as it should.

    Code:
    Sub CrossRefUserID()
    
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim mtchcnt As Integer, cName As String
    Set db = CurrentDb
    Set rs = db.OpenRecordset("UsersConnected")
    
    
    cName = rs.Fields("computer name")
    MsgBox cName
    mtchcnt = DCount("UserID", "Employees", "Domain='" & cName & "'")
    MsgBox mtchcnt
    End Sub

    Now, when i use that EXACT code in my actual database, the "Error 3075: Syntax Error in string query expression"

    Explain that. HAAAA.

    Thanks for the help. Scratching my head.

  13. #13
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Thanks for the help. Scratching my head.
    Something is different in the actual DB. If you can post it here someone will figure it out.

  14. #14
    ironfelix717 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    150
    Quote Originally Posted by davegri View Post
    Something is different in the actual DB. If you can post it here someone will figure it out.
    Unfortunately I can't post the DB as its proprietary. I could potentially reduce a copy and prove a sanitized version with just the form/tables in suspect.


    So, i yet again return with more information... The point of this procedure is to compare connections to user computer names. If a connection contains a computer ID that is not registered in user computer names, then its a foreign connection and should be disconnected (hacker?).

    I get the connections by running the following code (i tweaked it but don't take credit)







    Code:
    
    
    Sub RunUserCheck()
      Dim cn As ADODB.Connection
    
    
      Set cn = New ADODB.Connection
      ' Open the connection
      With cn
        .CursorLocation = adUseServer
        .Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\Mac\Home\Desktop\NewMRP\Database\Database_Main.accdb"
      End With
    
        Call UpdateUserRoster(cn)
    end sub
    
    ---------------------------------------------------------
    
    
    Sub UpdateUserRoster(cnnConnection As ADODB.Connection)
    
    
      Dim rstTmp As New ADODB.Recordset
      Dim strTmp As String
      Set db = CurrentDb
      Set rs = db.OpenRecordset("UsersConnected")
    
    
      ' This is the value to pass to Jet to get the user roster back.
      Const cstrJetUserRosterGUID As String = "{947bb102-5d43-11d1-bdbf-00c04fb92675}"
    
    
      On Error GoTo PROC_ERR
    
    
      ' Jet exposes the user roster as a provider-specific schema rowset.
      ' To get Jet to return this, we open a recordset and pass the special GUID value.
      Set rstTmp = cnnConnection.OpenSchema(adSchemaProviderSpecific, , cstrJetUserRosterGUID)
    
    
      ' The recordset contains four fields:
      ' COMPUTER_NAME: The machine name of the user's computer
      ' LOGIN_NAME:    The name the user logged into Access with
      ' CONNECTED:     True if the user is still connected
      ' SUSPECT_STATE: Connection was terminated normally or not (generally returns nothing if the user terminated normally or is still in the database)
    
    
      If Not rs.BOF = True Then rs.MoveFirst
    
    
    
    
    '  DoCmd.SetWarnings (False)
    '  DoCmd.RunSQL "DELETE * FROM [UsersConnected]"
    '  DoCmd.SetWarnings (True)
    
    
      With rstTmp
        Do Until .EOF
                rs.AddNew
                rs.Fields("Computer Name") = .Fields(0).Value      'Take note to these - i am adding the computer names of the connection to a table "UsersConnected"
                rs.Fields("Windows User Name") = .Fields(1).Value
                rs.Fields("Connected") = .Fields(2).Value
                rs.Fields("Suspect State") = .Fields(3).Value
                rs.Update
         .MoveNext
        Loop
      End With
      rstTmp.Close
        rs.Close
        Call CrossRefUserID  'Compare "UsersConnected" to "Employees" 
    
    
    PROC_EXIT:
    
    
      Exit Sub
    
    
    PROC_ERR:
      MsgBox "Error: " & Err.Number & ". " & Err.Description, , "ADOShowUserRosterToString"
      Resume PROC_EXIT
    
    End Sub
    
    
    End Sub

    OK... So, the reason the dummy database does not throw an error is because the value is directly typed into the table. When I run the code, it finds a match and no error... However, when I use my actual database and it populates the table via the Sub UpdateUserRoster() code, then SOMETHING happens with the data. I don't know that answer. But WHATEVER is imported into my table is NOT the same as me TYPING into the table manually.

    For instance "BobsComputer" typed in by me throws no error. If I let the code run and "BobsComputer" is found as a connected user and wrote to the table, the syntax error returns.


    So... SOMETHING is fishy with the UpdateUserRoster code and "what" exactly it is importing into my table. Apparently its not the same as typing it in - yet its still short text.

    Need a database expert! Thanks guys.

  15. #15
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Do you have OPTION EXPLICIT at the top of every code module?
    If not, add that and then Debug > Compile from the VBE menu.
    Have you done a compact/repair?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. String criteria used in DLookup
    By joecamel9166 in forum Programming
    Replies: 9
    Last Post: 03-11-2016, 09:37 AM
  2. DLookup 2nd character in a text string
    By DubCap01 in forum Forms
    Replies: 5
    Last Post: 03-06-2015, 02:37 PM
  3. How to use DLookup with string variable Criteria
    By JrMontgom in forum Programming
    Replies: 1
    Last Post: 03-09-2014, 07:55 PM
  4. Command Like DLookup but to store a string
    By viruzman in forum Programming
    Replies: 2
    Last Post: 07-28-2011, 02:04 PM
  5. # added to string with Dlookup
    By normever in forum Programming
    Replies: 1
    Last Post: 02-27-2009, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums