473,382 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

variable not defined error for field that exists?

AccessIdiot
493 256MB
I am using a MySQL back end and Access front end. One of my MySQL tables has three fields all defined as tiny ints, no nulls, default 0, field width 2.

On my form I have three checkboxes, each linked to the three fields. I have the EXACT SAME CODE for each after update:
Expand|Select|Wrap|Line Numbers
  1. Private Sub chkJHA_AfterUpdate()
  2. If chkJHA.Value = True Then
  3. dpr_safety = 1
  4. Else
  5. dpr_safety = 0
  6. End If
  7. End Sub
  8. ---------------------------------
  9. Private Sub chkVehicle_AfterUpdate()
  10. If chkVehicle.Value = True Then
  11. dpr_vehicle = 1
  12. Else
  13. dpr_vehicle = 0
  14. End If
  15. End Sub
  16. ------------------------------
  17. Private Sub chkWork_AfterUpdate()
  18. If chkWork.Value = True Then
  19. dpr_work = 1
  20. Else
  21. dpr_work = 0
  22. End If
  23. End Sub
  24.  
The first 2 work beautifully, the third gives me a compile error, variable not definded on dpr_work.

The other strange thing is that you know how the VBA editor does an autofill suggestion when you start typing me.? When I scroll through the list it shows me everything in my database EXCEPT that one field it is throwing the error on.

I can not for the life of me figure out why and I'm about totally and completely lose my mind. I've triple checked spellings, updated tables, rebooted my machine and nothing. I've confirmed that the field is in the table - both through phpmyadmin (which I'm using to create/edit the tables) and even in Access. You can see the field in the table and in the table design view. The only place it doesn't show up is in the VBA editor auto fill thing.

W.T.F.

Thanks for any help.
Sep 22 '10 #1
19 7001
AccessIdiot
493 256MB
If I create a blank form and add a checkbox, set the control source to the field in question, then go into the VBA editor it shows up? and works? So something is explicitly in my form that prevents it from working?
Sep 22 '10 #2
Mariostg
332 100+
I bet you are using Option Explicit and you did not delcare the variable...
Sep 23 '10 #3
AccessIdiot
493 256MB
Yes and yes, although is a field name a variable? I haven't declared anything anywhere and yet all the others work? dpr_vehicle, dpr_safety, and dpr_work are all tiny int fields in the MySQL db. The code for dpr_vehicle and dpr_safety work just fine.

One of my coworkers made a copy of the database and opened it on his machine and was able to get it to work with no problem. So we've replaced the file, but I still find it to be very strange?
Sep 23 '10 #4
Mariostg
332 100+
That is indeed bizarre but I am unsure I understand when you say "I haven't declared anything anywhere". Just to clarify, you have declared in the General portion of your module:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
And you have declared your variable:
Expand|Select|Wrap|Line Numbers
  1. Dim dpr_work as Integer
Because when you set Option Explicit, if you do not Dim the variable, this is exactly the error message you will get.
Sep 23 '10 #5
AccessIdiot
493 256MB
I do have Option Explicit, yes. No, I did not dim any of the variables. I've done a few access projects, though I am very much a beginner, and I can't recall ever declaring field names, and yet things work. So I am obviously practicing some very bad programming!

So since it all worked great until I tried to add this one particular bit of code is Access just very forgiving up to a point and then yells at me? Do I need to declare all fields that I am using in the vb editor?

And what about the fact that the auto complete pick list that appears when you type "Me." shows all fields available except that one?
Sep 23 '10 #6
Mariostg
332 100+
First, there is a difference between fields and variables.
Your form fields that you get from Me. will not complain whether or not you use Option Explicit. But variables will.

Make a little test you will get it...
Compile this it will work
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Sub myTest()
  4. y = 1
  5. End Sub
Compile this and it will fail
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Sub myTest()
  4. y = 1
  5. End Sub
  6.  
Now compile this and that will work
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Sub myTest()
  4. Dim y As Integer
  5. y = 1
  6. End Sub
Sep 23 '10 #7
AccessIdiot
493 256MB
So if I'm not creating a new variable to hold anything, but just specifying an actual field in the database that I want populated, it shouldn't matter if I use Option Explicit or not right? The fact that Me. shows ALL my fields EXCEPT that one is raising a red flag for me. Especially when the database is copied and moved and then shows up in that copy? That sounds like something is corrupted in the original version of the db?
Sep 23 '10 #8
Mariostg
332 100+
First question: Correct indeed. Using Option Explicit may seem like more work (or at least more typing) but it will ease debuging down the road and forces you to be consistent.
Second question: Not sure I get your point.
Third question: Maybe... Compact and repair often.

Also note that when Access reports an error, sometimes it will highlight an area but the error will be somewhere else than the highlighted area. Normally before it.

Something else puzzles me.
How come you use
If chkWork.Value = True Then
as opposed to
If Me.chkWork.Value = True Then
Sep 23 '10 #9
AccessIdiot
493 256MB
"Something else puzzles me.
How come you use
If chkWork.Value = True Then
as opposed to
If Me.chkWork.Value = True Then"

Heh - because I'm a hack coder who doesn't know what she is doing. :-) I'm actually a GIS Specialist trying to make some database things work. If I can get this up and running I can join it to some spatial data and impress my boss.

If I am referencing a field in the vb should I always use Me?

I guess the point I was trying to make earlier is that in the database design the field shows up as available. Also in the form design I can choose the field when I set the Control Source. But in the vb editor when I type Me. it's like it doesn't exist.

Except when I make a copy of the db and suddenly its available.
Sep 23 '10 #10
Mariostg
332 100+
LOL. Let's make it work then.

If I am referencing a field in the vb should I always use Me?
I always did, but I seldomly use .value... Hey I am just a hacker too. I use Me because it is the way I learned and it clerly tells me that the variable I am dealing with is coming from a form.

I see your point... But normally, while the the VBA editor AND in the form module, when you type Me. you will get a list of all its properties and methods.
Sep 23 '10 #11
AccessIdiot
493 256MB
Well, I guess I'm going to have to chalk it up to being corrupted. :-( Very frustrating. But on to other things!

Like, why am I still getting a value of -1 when the box is checked instead of the value changing to 1?
Sep 23 '10 #12
Mariostg
332 100+
Yes, MS Access is weird for that:
True = -1
False = 0
It can also be null (when it is neither True nor False)
In fact you could say :
Expand|Select|Wrap|Line Numbers
  1. If Me.chkJHA Then
to check if it true or
Expand|Select|Wrap|Line Numbers
  1. If NOT Me.chkJHA Then 
to check if it is false. But it will throw an error if it is null.
Sep 23 '10 #13
AccessIdiot
493 256MB
Default is 0 so shouldn't be a problem with the null (and I can use NZ right?). Thanks for the shortcut tip!
Sep 23 '10 #14
Mariostg
332 100+
If it defaults to 0, you are good to go. It is always best to avoid null values in any situation. Nz can be a good friend indeed.
Sep 23 '10 #15
I know these posts are from more than a year ago, but just want to say/clarify: I have run into this same problem, and what I notice is that the fields that are throwing the "Variable not defined" error are either fields whose names I changed recently in the table definition or new fields that I recently added to the table.

In the form design view, the Field List and the drop-down for the Control Source for a control reflect the current table design; but VBA seems to have a stale copy of the form's recordsource schema. So far I have had no success finding a way to refresh/update VBA's "awareness" of the recordsource's current field names... Any ideas?
Nov 11 '11 #16
NeoPa
32,556 Expert Mod 16PB
Oh wow!

I didn't see this before (So I won't *slap* BarksWorseThan for hijacking the thread - but I will say if you want a question answered then the way to do that is to post a question thread).

Melissa, there are a number of confusions in here. Some of which Mario has helped with, but others he's missed.

First off, it's best to understand the differences between variables, fields and controls. My guess (after the first post and hasn't changed since) is that you have a control missing for dpr_work. Either mis-spelled or just called something completely different - like Check29 or something. The field is probably fine, but you never renamed the control.

Forms actually can have Fields, but these are underlying and what you see on forms are controls (which may be bound to the fields). It gets more complicated from there, but I expect it's resolvable from that anyway. let us know how you get on ;-)
Nov 11 '11 #17
NeoPa -- a pleasant surprise to get a quick response! I was not really expecting much from my post here... was more interested in trying to clarify for others who may land here that the problem is real and probably NOT related (as you propose) to confusion between fields, variables, and controls.

At least in my case, I name my controls differently from the fields that are used as the control source specifically to avoid confusion when coding between the field and the control; likewise, I have yet a third convention for naming variables. So, for example, field "TractSubCode" is the control source for control "cboTractSubCode", and if I wanted some variable referring to the TractSubCode I would name it strTractSubCode.

Now, in my case, I used to have a field (in a *linked* table, in case that's important) called simply "TractSub", which used to be the Control Source for the control cboTractSub. When I renamed the column to "TractSubCode" (for semantic reasons), I changed the control source for the drop-down, and also renamed the control to "cboTractSubCode".

In VBA, however, I cannot refer to "TractSubCode", else I get the "variable not defined" error. If I type Me. Intellisense presents me with the old list of fields (among the many other options)... so "TractSub" is on the list even though it no longer exists in the table. Also, some new fields that I have added to the table are not present via Intellisense, and if I try to use them I get the "variable not defined" error, even though I can see the new and re-named fields correctly when I'm in Form Design mode.

So it would appear to be -- at least in my case, but I suspect also in the OP's case -- some kind of "stale" schema or metadata info on the VBA side of things. The Form Design sees all of the changes and additions to the table, but in VBA it's stuck on the table fields as they were when I first created the form.

Hope this helps to clarify where the problem lies. Thanks for your help.
Nov 11 '11 #18
Sorry for the typo on your id, NeoPa...
Nov 11 '11 #19
NeoPa
32,556 Expert Mod 16PB
BarksWorseThan:
At least in my case,
Herein lies the rub of course.

Though you clearly describe your situation, the thread is related to the situation of the OP (AccessIdiot as she refers to herself but I happen to know she is overly self-deprecating in that). I cannot, nor should I (You sound intelligent enough to appreciate why without detailed explanation), get involved with your particulars in this thread as that would hijack it away from the OP.

If you'd like to raise this issue yourself, in a separate thread, I'll be happy to deal with it in greater detail.

PS. It's not unlikely that all such posts, unrelated directly to the OP's specific problem, will be removed as hijacks, but I won't do so myself. I'm fussy, but I can see benefit in this, at least for now (Others may be fussier though ;-)).

PS. No problem with the ID. I didn't even see it and I find people actually getting it right is a rarity. Being fussy though, I like it when they do :-)
Nov 12 '11 #20

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

Similar topics

2
by: my | last post by:
Hi All, I have one question. Usually, we can get data from table by using rst("userid") because we know the column name userid. Now, I have a variable to hold the column name. I want to get...
6
by: beav At wn DOT com DOT au | last post by:
Students at the school where I work are getting the error "Application-defined or object-defined error." when trying to document their Access databases. If you open the documenter window, select a...
5
by: jeet_sen | last post by:
Hi, My external javascript test.js contains a variable definition var a = "Hello,world!!"; I dynamically loaded the script test.js using the following fucntion: function loadScript(url) { var...
2
by: Halid Umar A M | last post by:
Dear All, Please tell me why this error is occuring. The following is the code snippets which i have typed. struct mystructure{ struct list_head m; //error: field m has incomplete...
1
by: Sylaris | last post by:
hi, i have a problem which recursion fits perfectly, however; after working with the base function (which has no errors and works correctly) the recursions return a "function not defined" error in...
0
by: kotobe | last post by:
A newby question. I have a Variable defined in the first line of a script block. The same variable is giving me a "Variable is Undefined" error in an embedded script within the block that defined...
3
by: pollygw | last post by:
I have a page that dynamically adds rows to a table and the user can also delete any of the rows in no specific order. When the form is submitted I need to do some validation. I can't loop through...
1
by: jillB02 | last post by:
does anyone have some simple VBA code to determine if a Field exists in an Access Table? I am using Access 2003. All help and suggestions much appreciated regards from, Jill
6
by: nina01 | last post by:
Hi everybody!! I'm actually trying to construct a compiler with flex and bison. But when I try to compile the"lex.yy.c" file on cygwin I keep having this error: $gcc -c lex.yy.c In file...
1
by: arsha123 | last post by:
How can I change the value of empty session variable with another variable value defined in form. I get value of session value of id from a logon form , that is "". I have defined a variable in form...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.