Results 1 to 4 of 4
  1. #1
    guillermoftw is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2014
    Posts
    13

    Error in VBA Code

    Hi everyone,

    I'm using the DLookup function to look into a column to pull a value but the problem is i am receiving a runtime error 94 because not everytime will the column have "Related" or "Associated". Is there anyway to work around this error?



    I have posted my code below.

    Dim AdminClose As String
    Dim SQL As String
    AdminClose = DLookup("Type", "Admin Closures", "Type = 'Related'")

    If [AdminClose] = "Related" Then
    SQL = "UPDATE [WTC Related Codes] INNER JOIN [Admin Closures] ON ([WTC Related Codes].[BSOC File #]=[Admin Closures].[BSOC File #]) AND ([WTC Related Codes].[RC Code]=[Admin Closures].[DX Code]) SET [WTC Related Codes].[RC NIOSH Decision] = [Admin Closures]![Decision], [WTC Related Codes].[Denial Reason] = [Admin Closures]![Denial Reason], [WTC Related Codes].[Date of Decision] = [Admin Closures]![Date of Decision];"
    DoCmd.RunSQL SQL

    AdminClose = DLookup("Type", "Admin Closures", "Type = 'Associated'")
    Else: [AdminClose] = "Associated"
    SQL = "UPDATE [Medically Associated Codes] INNER JOIN [Admin Closures] ON ([Medically Associated Codes].[BSOC File #]=[Admin Closures].[BSOC File #]) AND ([Medically Associated Codes].[MA Code]=[Admin Closures].[DX Code]) SET [Medically Associated Codes].[MA NIOSH Decision] = [Admin Closures]![Decision], [Medically Associated Codes].[Denial Reason] = [Admin Closures]![Denial Reason], [Medically Associated Codes].[Date of Decision] = [Admin Closures]![Date of Decision];"
    DoCmd.RunSQL SQL
    End If

  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,642
    A String variable can't hold a Null, which is what the DLookup() will return if there's no value. You can change that to Variant, or wrap the DLookup() in the Nz() function.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    guillermoftw is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2014
    Posts
    13
    Hi pbaldy,

    Thanks for the response. Could you explain a variant or what the Nz function does?

    Thanks!

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    Variant is the only data type you can declare the variable as that can hold Null.

    https://msdn.microsoft.com/en-us/lib...ice.11%29.aspx
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Another code error using subquery
    By shani20 in forum Programming
    Replies: 1
    Last Post: 02-03-2015, 09:49 PM
  2. error with a code
    By ashraf_al_ani in forum Forms
    Replies: 3
    Last Post: 08-04-2014, 02:56 AM
  3. VBA Code Returning Error Run Time Error 3061
    By tgwacker in forum Access
    Replies: 2
    Last Post: 11-24-2013, 11:00 AM
  4. code logic error
    By mejia.j88 in forum Programming
    Replies: 17
    Last Post: 03-27-2012, 10:03 PM
  5. Error in Code
    By Lockrin in forum Programming
    Replies: 3
    Last Post: 02-25-2010, 03:27 PM

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