Compile Error

G

Guest

I am trying to create a module so when I type in my product ID number the
serial number and description automatically fills in, but I am getting a
compile error: variable not defined. My code I wrote is:

Private Sub IDNo_AfterUpdate()

Dim varID, varSerialNo, varDescription As Variant
varIDNo = DLookup("IDNo", "tblIDNo", "IDNo =[IDNo] ")
varSerialNo = DLookup("SerialNo", "tblIDNo", "SerialNo =[SerialNo] ")
varDescription = DLookup("Description", "tblIDNo", "Description
=[Description] ")
If (Not IsNull(varIDNo)) Then Me![IDNo] = varIDNo
If (Not IsNull(varSerialNo)) Then Me![SerialNo] = varSerialNo
If (Not IsNull(varDescription)) Then Me![Description] = varDescription

End Sub

Please help - any help you can give would be appreciated. Thanks.
 
J

John Spencer

You have DIMmed varID, but you keep referrng to varIDNo. Is that your
problem?
 
G

Guest

I took out the Dim in front of

An now I get a syntax error. What should it read?

John Spencer said:
You have DIMmed varID, but you keep referrng to varIDNo. Is that your
problem?


vgarr said:
I am trying to create a module so when I type in my product ID number the
serial number and description automatically fills in, but I am getting a
compile error: variable not defined. My code I wrote is:

Private Sub IDNo_AfterUpdate()

Dim varID, varSerialNo, varDescription As Variant
varIDNo = DLookup("IDNo", "tblIDNo", "IDNo =[IDNo] ")
varSerialNo = DLookup("SerialNo", "tblIDNo", "SerialNo =[SerialNo] ")
varDescription = DLookup("Description", "tblIDNo", "Description
=[Description] ")
If (Not IsNull(varIDNo)) Then Me![IDNo] = varIDNo
If (Not IsNull(varSerialNo)) Then Me![SerialNo] = varSerialNo
If (Not IsNull(varDescription)) Then Me![Description] = varDescription

End Sub

Please help - any help you can give would be appreciated. Thanks.
 
D

Douglas J Steele

John's comment was that you declared the variable as varID, but you're
referring to variable varIDNo in your code.

Either change

Dim varID, varSerialNo, varDescription As Variant

to

Dim varIDNo, varSerialNo, varDescription As Variant

or change the lines

varIDNo = DLookup("IDNo", "tblIDNo", "IDNo =[IDNo] ")

and

If (Not IsNull(varIDNo)) Then Me![IDNo] = varIDNo

to

varID = DLookup("IDNo", "tblIDNo", "IDNo =[IDNo] ")

and

If (Not IsNull(varID)) Then Me![IDNo] = varID



Oh, and while it doesn't really matter in this case,

Dim varIDNo, varSerialNo, varDescription As Variant

should really be

Dim varIDNo As Variant, varSerialNo As Variant, varDescription As Variant

VBA doesn't allow "short circuiting" in its declarations: you need to
declare a type for each variable. The reason why I say it doesn't really
matter in this case is that the default if you don't declare a type, the
variable becomes a Variant.

In other words,

Dim varIDNo, varSerialNo, varDescription As Variant

and

Dim varIDNo As Variant, varSerialNo As Variant, varDescription As Variant

are both the same

However,

Dim intIDNo, intSerialNo, intDescription As Integer

and

Dim intIDNo As Integer, intSerialNo As Integer, intDescription As Integer

aren't the same: in the first case, intIDNo and intSerialNo are actually
declared as Variants, not Integers.


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


vgarr said:
I took out the Dim in front of

An now I get a syntax error. What should it read?

John Spencer said:
You have DIMmed varID, but you keep referrng to varIDNo. Is that your
problem?


vgarr said:
I am trying to create a module so when I type in my product ID number the
serial number and description automatically fills in, but I am getting a
compile error: variable not defined. My code I wrote is:

Private Sub IDNo_AfterUpdate()

Dim varID, varSerialNo, varDescription As Variant
varIDNo = DLookup("IDNo", "tblIDNo", "IDNo =[IDNo] ")
varSerialNo = DLookup("SerialNo", "tblIDNo", "SerialNo =[SerialNo] ")
varDescription = DLookup("Description", "tblIDNo", "Description
=[Description] ")
If (Not IsNull(varIDNo)) Then Me![IDNo] = varIDNo
If (Not IsNull(varSerialNo)) Then Me![SerialNo] = varSerialNo
If (Not IsNull(varDescription)) Then Me![Description] = varDescription

End Sub

Please help - any help you can give would be appreciated. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top