Skip to content

Instantly share code, notes, and snippets.

@kylemarsh
Created August 14, 2012 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylemarsh/3352023 to your computer and use it in GitHub Desktop.
Save kylemarsh/3352023 to your computer and use it in GitHub Desktop.
Explaining a variable-passing problem a friend was having.
# Basics (things you already know)
"abc" # the string "abc"
SomeVar = "abc" # SomeVar is a variable whose value is the string "abc"
func() # func is a function we're calling with no arguments
func("abc") # call func and pass the string "abc" as the first argument
func(SomeVar) # call func and pass the value stored in the variable SomeVar as the first argument
# the important thing here is that we pass an "expression" and we *evaluate*
# the expression and just pass the result of that...SomeVar *evaluates* to the string "abc"
# Extrapolating from the basics
"abc" + "def" # append the string "def" to the string "abc". result is the string "abcdef" *string append may be different in Access
"def" + SomeVar # append the value stored in SomeVar to the string "def". result is the string "defabc" if SomeVar contains "abc"
"def=SomeVar" # the string "def=SomeVar" (SomeVar is not treated as a variable, since it's part of the string)
# Taking this to your Access problem:
Loc = DLookup("Location", "Part List", "PartNumber='abc'")
# DLookup is a function that takes three arguments...from looking at it I'd say it's the name
# of the field to return, the table name and a condition. All three are strings, and all three
# going to get dropped into a SQL query that looks something like this:
"SELECT Location FROM Part List WHERE PartNumber='abc'"
# the condition looks like [Field Name]=[value] and the value can either be another field name
# or a string. If it's a string, it has to be enclosed in single-quoted. So when you try passing
# "PartNumber=ToolNum" to DLookup, you're actually passing that string, literally, as-is, rather
# than putting the value of the variable ToolNum into the string that you're passing. Then it
# creates SQL that looks something like this:
"... WHERE PartNumber=ToolNum"
# And so it's trying to look for the field named ToolNum to compare to the field named PartNumber.
# To do what you want you need to append the value stored in the variable ToolNum to the string
# "PartNumber=" before it gets passed into DLookup. Looks something like this:
Loc = DLookup("Location", "Part List", "PartNumber=" + ToolNum)
# Basics (things you already know)
"abc" # the string "abc"
SomeVar = "abc" # SomeVar is a variable whose value is the string "abc"
func() # func is a function we're calling with no arguments
func("abc") # call func and pass the string "abc" as the first argument
func(SomeVar) # call func and pass the value stored in the variable SomeVar as the first argument
# Extrapolating from the basics
"abc" + "def" # append the string "def" to the string "abc". result is the string "abcdef" *string append may be different in Access
"def" + SomeVar # append the value stored in SomeVar to the string "def". result is the string "defabc" if SomeVar contains "abc"
"def=SomeVar" # the string "def=SomeVar" (SomeVar is not treated as a variable, since it's part of the string)
# Taking this to your Access problem:
Loc = DLookup("Location", "Part List", "PartNumber='abc'")
# DLookup is a function that takes three arguments...from looking at it I'd say it's the name
# of the field to return, the table name and a condition. All three are strings, and all three
# going to get dropped into a SQL query that looks something like this:
"SELECT Location FROM Part List WHERE PartNumber='abc'"
# the condition looks like [Field Name]=[value] and the value can either be another field name
# or a string. If it's a string, it has to be enclosed in single-quoted. So when you try passing
# "PartNumber=ToolNum" to DLookup, you're actually passing that string, literally, as-is, rather
# than putting the value of the variable ToolNum into the string that you're passing. Then it
# creates SQL that looks something like this:
"... WHERE PartNumber=ToolNum"
# And so it's trying to look for the field named ToolNum to compare to the field named PartNumber.
# To do what you want you need to append the value stored in the variable ToolNum to the string
# "PartNumber=" before it gets passed into DLookup. Looks something like this:
Loc = DLookup("Location", "Part List", "PartNumber=" + ToolNum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment