473,508 Members | 4,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compile Error: Sub or Function Not Defined

3 New Member
Hello all.

I am trying to use DLookup to auto-populate fields in an otherwise simple database. I am using Access 2010. I have created three different databases and the code still does not work. I think there may be glitches in this particular copy of the program. Please evaluate the details below and tell me what you think.

Table 1 has fields: Product ID, CustomerName, CustomerAddr, and ComplaintLevel.

Table 2 has fields: Resolution ID, CustomerName, CustomerAddr, Product ID, ComplaintLevel, ResolutionDate.

I want DLookup to populate Product ID field of Table 2 once CustomerName has been selected. CustomerName options are provided via a combo box that pulls from Table 1.

Here is my code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub CustomerName_AfterUpdate()
  2. Product ID = DLookup("Product ID", "Table 1", "CustomerName=" & CustomerName)
  3. End Sub
I get a Compile Error. What do you suppose is at issue here?
Jan 5 '12 #1
3 5535
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Never blame glitches on the program itself; although there are known bugs with Access, you have to rule out errors you have introduced first.

Firstly, if you want to assign values to a control called Product ID you must enclose the reference to it in brackets, [like this].

Secondly, if you are referring to a string literal value within a where clause (the third parameter supplied to the DLookup), you must enclose the value supplied in single quotes, like this:

Expand|Select|Wrap|Line Numbers
  1. Me![Product ID] = DLookup("Product ID", "Table 1", "CustomerName = '" & Me!CustomerName & "'")
The compile error you mention resulted from the incorrect reference to the field Product ID. VBA would interpret this as a variable called Product followed by another called ID with no operator between them.

The lack of the single quotes in the Where clause would cause a run-time error when Access tries to interpret the text in the customer name as some kind of missing variable instead of passing the string to the DLookup.

As I mentioned, don't blame the program until you rule your own actions out first!

-Stewart
Jan 5 '12 #2
CapriPHR
3 New Member
Thank you for the feedback, Stewart. I wrote the code that way because I was instructed by other resources to use that format. Writing the code as you have indicated did not fix the problem. I didn't get another error message, but nothing happened at all once I selected CustomerName.

I adjusted the formatting of fields and VB code as follows and now it returns a value for ProductID, but not the one associated with the specified CustomerName:
Expand|Select|Wrap|Line Numbers
  1. Private Sub CustomerName_AfterUpdate()
  2. ProductID = DLookup("ProductID", "Table1", "CustomerName = '" & Me!CustomerName & "'")
  3. End Sub
My previous resources did not stress the importance of quotation marks, so thanks for that feedback!
Jan 5 '12 #3
CapriPHR
3 New Member
Here is the VB code that finally worked:
Expand|Select|Wrap|Line Numbers
  1. Private Sub CustomerName_AfterUpdate()
  2. Me![ProductID] = DLookup("ProductID", "Table1", "CustomerName = '" & Me!CustomerName & "'")
  3. Me![CustomerAddr] = DLookup("CustomerAddr", "Table1", "CustomerName = '" & Me!CustomerName & "'")
  4. Me![ComplaintLevel] = DLookup("ComplaintLevel", "Table1", "CustomerName = '" & Me!CustomerName & "'")
  5. End Sub
Thanks again for your feedback!
Jan 5 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3486
by: MLH | last post by:
After following the instructions at http://support.microsoft.com/?id=258049 I get an error at compile time complaining about c As CloseCommand Function InitApplication()...
22
7772
alpnz
by: alpnz | last post by:
I am getting a Compile error on the following Code. What have I missed Private Sub create_eplab_Click() ' First sort out the variables. Including label content, quantity to print etc ...
5
5024
by: wong_powah | last post by:
#include <vector> #include <iostream> using std::cout; using std::vector; enum {DATASIZE = 20}; typedef unsigned char data_t;
0
1464
by: joemo2003 | last post by:
Need help, please! In an excel vba automation, in my code have some codes like "Global vsapp as visio.application", but in some other computer don't have visio installed, and i won't use that visio...
3
12049
by: blakerrr | last post by:
Hi everyone, I am trying to export a table to an excel file using vba on a form's button click event. I am getting the error: Compile error: User-defined type not defined. And it highlights...
13
9049
by: forrestgump | last post by:
I am currently trying to use the below VBA to import information into excel from access. This VBA is in the excle sheet:- Public Sub getrs() Dim adoconn As ADODB.Connection Dim adors As...
1
3005
by: coolminded | last post by:
hello all, i'm using vb6 and sql. whenever i compile my project it shows the message "User-defined not defined". i have kept all the references associated with my project. even though, it shows...
6
19901
by: travjbad1 | last post by:
I am new to the forum and new to Access, so please be simple and descriptive if possible. I am having a problem with a button on a form that saves, opens a report in pdf, and emails the report to the...
1
2083
by: WECcoder | last post by:
This code used to work prior to our group IT update to Word I thought Word.Application was a generic type defined to Access? Private Sub cmdDatasheetsDigital_Click() Set db = CurrentDb Dim Wrd...
0
1372
by: usharani K | last post by:
Urgent Help !!!!!!!!!!!while compiling the code I am getting the compile error: Compile error: User-defined type not defined in form_load. ---------------------------- Private Sub Form_Load() ...
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7128
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5635
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.