473,394 Members | 1,867 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,394 software developers and data experts.

#Name? error in Access Form when trying to add new record

Hello all!

Have researched the issue all day to to avail, therefore turning to your help for the solution.

I have a Form A which is accessed via another Form B only (i.e. not possible for user to go directly to a Form A and add values).
So when the user clicks on a button in the Form B he is taken to the Form A.
In case Form A has a record (CONTAINER) correspondent to key record in Form B, then the correct record is filtered based on the code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub TTLex_DblClick(Cancel As Integer)
  2.  
  3. Dim stDocName As String
  4. Dim stLinkCriteria As String
  5.  
  6. stDocName = "frmExpense"
  7. stLinkCriteria = "[CONTAINER]='" & Me![CONTAINER] & "'"
  8.  
  9. DoCmd.OpenForm stDocName, , , stLinkCriteria, , acDialog
  10.  
  11. Exit_cmdDetails_Click:
  12. Exit Sub
  13.  
  14. Err_cmdDetails_Click:
  15. MsgBox Err.Description
  16. Resume Exit_cmdDetails_Click
  17.  
  18. End Sub
  19.  
However if there is no correspondent record in Form A there is a code on the Current state to create a new record showing the correspondent record name from Form B:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2.  
  3. If Me.NewRecord Then
  4. [CONTAINER].DefaultValue = Forms![frmExport]![CONTAINER]
  5. End If
  6.  
  7. End Sub
  8.  
The first part where there are correspondent records works fine. The trouble starts when no correspondent record is found - Form A shows #Name? instead of a record name from Form B.

I have checked Control Source, Label names - everything is in order. Don't know where else to look.

Your help will be much appreciated.
May 8 '12 #1

✓ answered by TheSmileyCoder

About the ESCAPE option
Imagine the following scenario, you have a form for say students, and another form for adding Parents of students. Imagine that the form for adding parents is not a subform, but a form that you open from your students form, and during that process INSTEAD of setting the default value of the student ID field to match the student form, you set the value of the field. The form is now DIRTY (meaning a unsaved New/Edit is in progress). If the user presses ESC key the record will be undone, and the value of the student ID field will return to null. If you have not handled that case, the user can now enter a parent, that will NOT be tied to a student ID, which I hope you can see and understand why that would be bad. However, if you use the DefaultValue approach the above scenario cannot happen, because even if the user undo, and then create a record the student ID field will be properly filled out.

I created a test db, and tried to recreate your issue. Only when it became apparant that this particular error only occurs for TEXT fields. (note the different between a textbox which can show many differnt types of data, and TEXT which is a datatype in access.)

The problem is that say your textbox txtContainer bound to the field container, contains the string "test".

Now the code runs fine, and sets the defaultvalue of the textbox on frmExpense to test. However when the form loads, access thinks that Test is a function, or variable, and then complains (The #Name?) that it can't find anything by that name.

The solution is to tell access that its dealing with a string. The following code accomplishes that by adding single quotes around the value of frmExport.txtContainer when adding it to the defaultvalue field in frmExpense.

Expand|Select|Wrap|Line Numbers
  1. Me.txtContainer.DefaultValue = "'" & Forms!frmExport.txtContainer & "'"
Once you have tested it and you find it to work, you can off course remove the msgbox line, it was only ever there to help debug.

20 6262
TheSmileyCoder
2,322 Expert Mod 2GB
First of, its a bad idea to use the same name for both the field of the table and the control on your form. A common approach is to prefix the control name with a tb or txt for textbox.

Start by fixing that, and let us know if it helps.
May 8 '12 #2
don't know what exactly yu are referring to as I have 2 Forms and the CONTAINER in both refers to a record in the initial table, so I really cannot change the one of these as then it would not work.

Perhaps you could explain more precisely or some more info needed from my side?
May 8 '12 #3
NeoPa
32,556 Expert Mod 16PB
It looks like the controls on both [Form A] (which appears to be [frmExpense] - It's actually clearer if the name you use in the explanation matches the name in the code) and [Form B] ([frmExport]) for the field [CONTAINER] are also named [CONTAINER]. Access defaults to that approach, which sux large, but it's still good advice to use [txtContainer] as a name for controls if the bound field is named [CONTAINER].
May 9 '12 #4
NeoPa
32,556 Expert Mod 16PB
Your code doesn't do much wrong to be fair, though I'd suggest putting the DefaultValue code in the Form_Open() event procedure rather than the Form_Current() one.

Something like :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.     Me.txtContainer.DefaultValue = Forms!frmExport.txtContainer
  3. End Sub
or even :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.     Me.txtContainer.DefaultValue = Split(Me.Filter, "'")(1)
  3. End Sub
There is always the danger of a mis-reference if the control is named the same as the field.
May 9 '12 #5
Mihail
759 512MB
Your code:
Expand|Select|Wrap|Line Numbers
  1. If Me.NewRecord Then
  2.     [CONTAINER].DefaultValue = Forms![frmExport]![CONTAINER]
  3. End If
And yours explanation:
The trouble starts when no correspondent record is found - Form A shows #Name? instead of a record name from Form B.

If I not misunderstand something, Forms![frmExport]![CONTAINER] do not exist in this case. So, Access say to you that he (Access) has no value to working with in order to establish the [CONTAINER].DefaultValue.
May 9 '12 #6
Hello NeoPa,

thank you for the explanation.
I have now changed the name to txtCONTAINER in both forms and inserted the code you advised to Open state of frmExpense. That yields the same result with the #Name? error.

I also tried to leave bits of my previous code in:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.  
  3.     If Me.NewRecord Then
  4.     Me.txtCONTAINER.DefaultValue = Forms!frmExport.txtCONTAINER
  5.     End If
  6.  
  7. End Sub
  8.  
and the #Name issue goes away in frmExpense and shows a blank field instead, which is incorrect as I need it to show the correspondent field from frmExport.

Will try to play around, and will appreciate your further advice.
May 9 '12 #7
TheSmileyCoder
2,322 Expert Mod 2GB
I suggest you try to read out the defaultvalue after its been set, to see what happens.
A simple messagebox should provide more information:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.   If Me.NewRecord Then
  3.     Me.txtCONTAINER.DefaultValue = Forms!frmExport.txtCONTAINER
  4.     Msgbox "Default Value set to:" & Me.txtContainer.DefaultValue
  5.   End If
  6. End Sub
  7.  
May 9 '12 #8
Unfortunately nothing new - no #Name? error, but also blank in the required field.
May 9 '12 #9
TheSmileyCoder
2,322 Expert Mod 2GB
The messagebox was never intended as a solution, but as part of a debug procedure. However if you don't inform us of the results then we will rapidly go nowhere. What result did the messagebox produce on your screen?
May 9 '12 #10
That is the thing - the messagebox did not return anything - no message displayed.
May 9 '12 #11
NeoPa
32,556 Expert Mod 16PB
alekseiaiteno:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.  
  3.     If Me.NewRecord Then
  4.     Me.txtCONTAINER.DefaultValue = Forms!frmExport.txtCONTAINER
  5.     End If
  6.  
  7. End Sub
This shows a fundamental misunderstanding of the issue Aleksei. The DefaultValue property pertains to the form, and all records thereon. It is set when the form opens before you ever get to positioning the pointer to a new record. Think about that for a second.

I must say I agree with Smiley here, in that your reported experiences point to a problem with the [txtCONTAINER] value from the other form. Is [frmExport] even still open at this stage? I expect it would be if it called for this form ([frmExpense]) to be opened. You need to let us know what you see when using Smiley's suggestion :
Expand|Select|Wrap|Line Numbers
  1. Msgbox "Default Value set to:" & Me.txtContainer.DefaultValue
But not in the If block it was suggested in ;-)
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.     Me.txtCONTAINER.DefaultValue = Forms!frmExport.txtCONTAINER
  3.     Call MsgBox("Default Value set to:" & Me.txtCONTAINER.DefaultValue)
  4. End Sub
@Mihail
I'm afraid you do misunderstand something in this case, as what you say is not correct.
May 9 '12 #12
I think I may have found the workaround in a slightly different way.

I have updated the code in the frmExpense:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3.     If Me.NewRecord Then
  4.     Me.txtCONTAINER = Forms!frmExport.txtCONTAINER
  5.     End If
  6.  
  7. End Sub
  8.  
Notice I've changed the Form_Open to Form_Load. And the DefaultValue is gone.
This creates a new record and puts the data in the CONTAINER record on frmExpense when the blank field is double clicked.
May 9 '12 #13
Mihail
759 512MB
Indeed.
But this will force the user to fill all required fields. How you implement the ESCAPE option ?
May 9 '12 #14
NeoPa
32,556 Expert Mod 16PB
As Mihail says, this is a flawed approach that will automatically dirty the buffer on entry to the form. DefaultValue was absolutely the correct way to go. Don't give up just because understanding the complexities is difficult, otherwise you'll be just as lost and confused next time you have to deal with any similar issue.

What you've seen makes perfect sense. Your code was designed wrongly, so the results were other than you expected. That is a good thing, not a bad one. It tells you to look deeper at the actual logic, and to understand it more fully before coding.

Did you try the code I suggested in post #12? If so, what did you see in the message box?
May 9 '12 #15
Mihail, NeoPa, I'm back to try what you suggest as you definitely have more savvy that I do.
Not quite sure what "this will force the user to fill all required fields. How you implement the ESCAPE option" and "that will automatically dirty the buffer on entry to the form" mean :) but let's leave this aside.

I have added the code you suggested above
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.     Me.txtCONTAINER.DefaultValue = Forms!frmExport.txtCONTAINER
  3.     Call MsgBox("Default Value set to:" & Me.txtCONTAINER.DefaultValue)
  4. End Sub
and what I get is a message box with the correct CONTAINER value, but then it goes to the frmExpense and the [Container] field has #Name? error in it.
May 10 '12 #16
TheSmileyCoder
2,322 Expert Mod 2GB
About the ESCAPE option
Imagine the following scenario, you have a form for say students, and another form for adding Parents of students. Imagine that the form for adding parents is not a subform, but a form that you open from your students form, and during that process INSTEAD of setting the default value of the student ID field to match the student form, you set the value of the field. The form is now DIRTY (meaning a unsaved New/Edit is in progress). If the user presses ESC key the record will be undone, and the value of the student ID field will return to null. If you have not handled that case, the user can now enter a parent, that will NOT be tied to a student ID, which I hope you can see and understand why that would be bad. However, if you use the DefaultValue approach the above scenario cannot happen, because even if the user undo, and then create a record the student ID field will be properly filled out.

I created a test db, and tried to recreate your issue. Only when it became apparant that this particular error only occurs for TEXT fields. (note the different between a textbox which can show many differnt types of data, and TEXT which is a datatype in access.)

The problem is that say your textbox txtContainer bound to the field container, contains the string "test".

Now the code runs fine, and sets the defaultvalue of the textbox on frmExpense to test. However when the form loads, access thinks that Test is a function, or variable, and then complains (The #Name?) that it can't find anything by that name.

The solution is to tell access that its dealing with a string. The following code accomplishes that by adding single quotes around the value of frmExport.txtContainer when adding it to the defaultvalue field in frmExpense.

Expand|Select|Wrap|Line Numbers
  1. Me.txtContainer.DefaultValue = "'" & Forms!frmExport.txtContainer & "'"
Once you have tested it and you find it to work, you can off course remove the msgbox line, it was only ever there to help debug.
May 10 '12 #17
NeoPa
32,556 Expert Mod 16PB
AlexeiAiteno:
and what I get is a message box with the correct CONTAINER value, but then it goes to the frmExpense and the [Container] field has #Name? error in it.
Can you post exactly what it said Alexei. It may also help to indicate at this point, what the type of the field CONTAINER is in your table. Numeric, textual and date type fields all behave differently in particular circumstances.
May 10 '12 #18
IT WORKS!
the code you advised above
Expand|Select|Wrap|Line Numbers
  1. Me.txtContainer.DefaultValue = "'" & Forms!frmExport.txtContainer & "'"
seems to do the requested operation.

As for the Msg box it said previously: Default value is set to:AAAA1234567
With the new code it adds single quote marks around the AAAA1234567

CONTAINER is a text field (what I now understand is a string in Access terms).
I still have a lot to learn and your help and comments are very appreciated!
May 11 '12 #19
TheSmileyCoder
2,322 Expert Mod 2GB
Good to hear that its finally working. Keep in mind for your next question that we can only work with the information we get. The more accurately you describe your situation, and for example the EXACT result from a msgbox will help us to not only answer faster, but more precisely.

A text field and a string is not exactly the same. A text field is bound to a table, and once written to, it has a physical location on a diskdrive. A text field can only contain as many charecters as you have set its size to (maximum 255), whereas a string can contain alot more, I believe its limited to 2.147.000 or so, but it may be larger. A string is contained in memory and as such is lost when it goes out of program scope.
May 11 '12 #20
NeoPa
32,556 Expert Mod 16PB
The .DefaultValue property can take references as well as literal values. Therefore, it is necessary to differentiate between a literal text string and a text string which is the name of an item (which would be a reference to that item). When entering such a value in Design View, Access automatically adds quotes for you. When doing it by VBA code, you need to add these yourself for string values, and add hashes (#) for date values. I hope this makes what is happening, and why, clearer for you.
May 11 '12 #21

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

Similar topics

3
by: SQLScott | last post by:
Forgive me if this seems trivial but I cannot seem to find an answer. For testing purposes, one of my methods in my Web Service writes to a file (at least tries). I am using the following line of...
1
by: L Mehl | last post by:
On my cmdSaveRecord, I check a couple of fields to confirm that an entry has been made before allowing the Save. I decided to - not put validation rules in table structure, and - validate on...
1
by: Todd | last post by:
My form contains a combo box and the "not in list" event is triggerred when the user enters a value that is not among the values listed in the combo box. I want the user to be able to add a new...
3
by: JvCoach23 | last post by:
I'm trying to run a LoadAllImages(ds.Tables(0), "vcFileName", "d:\Image") Public Shared Sub LoadAllImages(ByVal MyDataTable As DataTable, ByVal FilePathField As String, ByVal ImageField As...
0
by: craigc | last post by:
hi, i'm trying to create an asp.net web service remotely as detailed at : ...
4
by: jambalapamba | last post by:
hi all i am loading a html document in my iframe which comes from the same domain, when i am trying to acess the document(html page) in the iframe using java script i am getting access...
1
by: myipmike | last post by:
Hi there, I have an app that produces labels and which runs fine under Access 2000, 2002 and 2003. The compiled 2002 .mde version runs fine under Access 2007 but only sometimes ! i.e. under XP...
5
Dököll
by: Dököll | last post by:
Hey Fans! Perhaps you can help me... I am using MS Access 2003. I have tried a number of things to get rid of the text #Error from appearing on form fields when no data present. Tried the...
3
by: wbabbit | last post by:
I am getting an "Access Denied" error when trying to access an executable in my web directory. I have tried cmd.exe, ftp.exe, even notepad.exe. I have given everyone, including the anonymous user,...
4
by: Rotorian | last post by:
Hello, This is a design question not a "how to" I would like know if it can be done before searching for a non existant answer :). If a record is created via a form, can a tab be created on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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...

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.