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: -
Private Sub TTLex_DblClick(Cancel As Integer)
-
-
Dim stDocName As String
-
Dim stLinkCriteria As String
-
-
stDocName = "frmExpense"
-
stLinkCriteria = "[CONTAINER]='" & Me![CONTAINER] & "'"
-
-
DoCmd.OpenForm stDocName, , , stLinkCriteria, , acDialog
-
-
Exit_cmdDetails_Click:
-
Exit Sub
-
-
Err_cmdDetails_Click:
-
MsgBox Err.Description
-
Resume Exit_cmdDetails_Click
-
-
End Sub
-
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: -
Private Sub Form_Current()
-
-
If Me.NewRecord Then
-
[CONTAINER].DefaultValue = Forms![frmExport]![CONTAINER]
-
End If
-
-
End Sub
-
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.
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. - 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
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.
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?
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].
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 : - Private Sub Form_Open(Cancel As Integer)
-
Me.txtContainer.DefaultValue = Forms!frmExport.txtContainer
-
End Sub
or even : - Private Sub Form_Open(Cancel As Integer)
-
Me.txtContainer.DefaultValue = Split(Me.Filter, "'")(1)
-
End Sub
There is always the danger of a mis-reference if the control is named the same as the field.
Your code: - If Me.NewRecord Then
-
[CONTAINER].DefaultValue = Forms![frmExport]![CONTAINER]
-
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.
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: -
Private Sub Form_Open(Cancel As Integer)
-
-
If Me.NewRecord Then
-
Me.txtCONTAINER.DefaultValue = Forms!frmExport.txtCONTAINER
-
End If
-
-
End Sub
-
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.
I suggest you try to read out the defaultvalue after its been set, to see what happens.
A simple messagebox should provide more information: - Private Sub Form_Open(Cancel As Integer)
-
If Me.NewRecord Then
-
Me.txtCONTAINER.DefaultValue = Forms!frmExport.txtCONTAINER
-
Msgbox "Default Value set to:" & Me.txtContainer.DefaultValue
-
End If
-
End Sub
-
Unfortunately nothing new - no #Name? error, but also blank in the required field.
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?
That is the thing - the messagebox did not return anything - no message displayed.
NeoPa 32,556
Expert Mod 16PB alekseiaiteno: - Private Sub Form_Open(Cancel As Integer)
-
-
If Me.NewRecord Then
-
Me.txtCONTAINER.DefaultValue = Forms!frmExport.txtCONTAINER
-
End If
-
-
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 : - Msgbox "Default Value set to:" & Me.txtContainer.DefaultValue
But not in the If block it was suggested in ;-) - Private Sub Form_Open(Cancel As Integer)
-
Me.txtCONTAINER.DefaultValue = Forms!frmExport.txtCONTAINER
-
Call MsgBox("Default Value set to:" & Me.txtCONTAINER.DefaultValue)
-
End Sub
@Mihail
I'm afraid you do misunderstand something in this case, as what you say is not correct.
I think I may have found the workaround in a slightly different way.
I have updated the code in the frmExpense: -
Private Sub Form_Load()
-
-
If Me.NewRecord Then
-
Me.txtCONTAINER = Forms!frmExport.txtCONTAINER
-
End If
-
-
End Sub
-
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.
Indeed.
But this will force the user to fill all required fields. How you implement the ESCAPE option ?
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?
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 -
Private Sub Form_Open(Cancel As Integer)
-
Me.txtCONTAINER.DefaultValue = Forms!frmExport.txtCONTAINER
-
Call MsgBox("Default Value set to:" & Me.txtCONTAINER.DefaultValue)
-
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.
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. - 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.
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.
IT WORKS!
the code you advised above -
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!
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
by: craigc |
last post by:
hi,
i'm trying to create an asp.net web service remotely as detailed at :
...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |