Default value of unbound text box (1 Viewer)

Eljefegeneo

Still trying to learn
Local time
Today, 01:37
Joined
Jan 10, 2011
Messages
904
I have a billing database where I want to change a quote or greeting that is displayed on an invoice which I can change at will. I tried all different kinds of options but finally figured out something that would work. But I am not sure if I am doing it correctly.

The Main menu has an unbound text box on which the text to display on the invoice is shown. I wanted to be able to change the default value of this unbound text box [txtquote] so that I could use the current text or change it. And I know that I have to set the default value of a text box when the form is in design view. It seems that you have to have at least one other form open for you to open another form in design view and change the default value without throwing an error message.

So my only recourse was to have a pop-up form [frmChangeText] bound by a new table on which I could enter my new text to display on the main form from which I could reference on my invoices.
Code:
Forms!frmMainMenu!txtquote = Me.txtquote
  DoCmd.Close acForm, "frmMainMenu"
  DoCmd.OpenForm "frmMainMenu", acDesign, , , acFormPropertySettings, acWindowNormal
  Forms!frmMainMenu!txtquote.DefaultValue = """" & Me.txtquote.Value & """"
  DoCmd.Close acForm, "frmMainMenu", acSaveYes
  DoCmd.OpenForm "frmMainMenu"
  Forms!frmMainMenu!txtquote = Me.txtquote
  DoCmd.Close acForm, "frmChangeText"
Without the new table albeit only a record Id and one field, the code wouldn't work. And I had to include the second to last line of the code or I would be the dreaded #Name? error.

Is this the correct method of doing this? It just seems very clumsy to me.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:37
Joined
Oct 29, 2018
Messages
21,357
Hi. Not sure what you mean, but you should be able to simply enter something like this in the Default Value property in design view:
=Forms!FormName.TextboxName
 

vba_php

Forum Troll
Local time
Today, 03:37
Joined
Oct 6, 2019
Messages
2,884
And I know that I have to set the default value of a text box when the form is in design view. It seems that you have to have at least one other form open for you to open another form in design view and change the default value without throwing an error message.
absolutely not. that's not true. 1 form open in design has always been allowed.

Code:
Forms!frmMainMenu!txtquote = Me.txtquote
  DoCmd.Close acForm, "frmMainMenu"
  DoCmd.OpenForm "frmMainMenu", acDesign, , , acFormPropertySettings, acWindowNormal
  Forms!frmMainMenu!txtquote.DefaultValue = """" & Me.txtquote.Value & """"
  DoCmd.Close acForm, "frmMainMenu", acSaveYes
  DoCmd.OpenForm "frmMainMenu"
  Forms!frmMainMenu!txtquote = Me.txtquote
  DoCmd.Close acForm, "frmChangeText"
Without the new table albeit only a record Id and one field, the code wouldn't work. And I had to include the second to last line of the code or I would be the dreaded #Name? error
ur writing the same code line twice my friend. that's an issue. the other thing you shouldn't be doing is opening a form in design view with code. any well design database in access (or even a substandard one for that matter!) would never need any reason to do this. perhaps you should upload this file and have someone see if they can replicate this weird stuff ur experiencing?
 

Eljefegeneo

Still trying to learn
Local time
Today, 01:37
Joined
Jan 10, 2011
Messages
904
Regarding having to set the "permanent" default value of an unbound text box, I refer to the following: https://social.msdn.microsoft.com/F...ult-value-of-unbound-text-box?forum=accessdev


"However, any changes you make to the DefaultValue will only be valid for the time the form is open. If you close the form, the default value will be lost. The only way to persist a new DefaultValue is to make the change in design view. That said, you can use VBA to silently open the form in design view, make the change, then save and close it.
Code:
[FONT=Arial][SIZE=2]DoCmd.OpenForm "frmMyForm", acDesign, , , acFormPropertySettings, acHidden[/SIZE][/FONT]

[FONT=Arial][SIZE=2]     Forms!frmMyForm!txtMyTextBox.DefaultValue = 12
[/SIZE][/FONT][FONT=Arial][SIZE=2]     DoCmd.Close acForm, "frmMyForm", acSaveYes"[/SIZE][/FONT]


I am going to have to make a sample DB and post it so my problem can be seen.
 

June7

AWF VIP
Local time
Today, 00:37
Joined
Mar 9, 2014
Messages
5,423
Sure, 1 form can be open in design view without other forms open, unless you want to programmatically open in design view. Where is the code to do that? Probably behind another form.

What is 'current text'?

I don't understand need to dynamically set DefaultValue of an unbound control nor why it is has to be 'permanent'. User can enter whatever they want. But if value is not saved to a record, how is it available to report? Does textbox on report reference control on form?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:37
Joined
Feb 19, 2002
Messages
42,970
I would advise against ever opening ANY object in design view in "production". This prevents you from distributing an .accde/.mde or using the Runtime engine.

Keep the value in a table if you want to vary it and look it up as necessary. You can apply the default in the form's BeforeInsert event. This event runs as soon as the user types the first character in any control for a new record. OR, you can use the Form's BeforeUpdate event to check if the field has a value. If it doesn't, you can silently add the default value at that time. Most people prefer to see the defaults immediately when the new record is being created so the BeforeInsert event is the best choice.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 19:37
Joined
Jan 20, 2009
Messages
12,849
One way to have an adjustable Default is to have it looked up in a table where you can change the value whenever you want.

A DLookup() expression can be used in the DefaultValue of the control to retrieve the stored value.
 

Eljefegeneo

Still trying to learn
Local time
Today, 01:37
Joined
Jan 10, 2011
Messages
904
Turkey eaten, family left and I now, after the prodding above that I was not exactly coding it correctly came up with the following.

Code:
  Dim sQuote As String
  sQuote = """" & Me.txtquote & """"
  DoCmd.OpenForm "frmMainMenu", acDesign, , , acFormPropertySettings, acHidden
       Forms!frmMainMenu!txtquote.DefaultValue = sQuote
         DoCmd.Close acForm, "frmMainMenu", acSaveYes
           DoCmd.OpenForm "frmMainMenu"
I tried it without going into design view, but get the dreaded #Name? error message in the unbound text box.
Much better than my first attempt and I appreciate the input on this
 

vba_php

Forum Troll
Local time
Today, 03:37
Joined
Oct 6, 2019
Messages
2,884
Code:
  Dim sQuote As String
  sQuote = """" & Me.txtquote & """"
  DoCmd.OpenForm "frmMainMenu", acDesign, , , acFormPropertySettings, acHidden
       Forms!frmMainMenu!txtquote.DefaultValue = sQuote
         DoCmd.Close acForm, "frmMainMenu", acSaveYes
           DoCmd.OpenForm "frmMainMenu"
so everything is working as it should be then, right? there doesn't look like there's anything wrong with this code. it should work fine.
 

Eljefegeneo

Still trying to learn
Local time
Today, 01:37
Joined
Jan 10, 2011
Messages
904
Yes it does! Changes to default are made only in design view
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:37
Joined
May 21, 2018
Messages
8,463
Changes to default are made only in design view
Yes everyone knows to persist a value you must be in design view, but no good vba coder does what you are suggesting to open in design view. There are many reasons not to open any object in design view. Some have been mentioned already. Instead of persisting a value, you set the value dynamically when needed. If it changes between records then use the on currrent event. For example if you want the next record to default to a value in the current record. I will echo what others have told you and do not do what you are suggesting.

I tried it without going into design view, but get the dreaded #Name? error message in the unbound text box
That is because you did something wrong, not because it is not possible to do.
 

Users who are viewing this thread

Top Bottom