setting default value of txtbox in code

ramblinwreck

Registered User.
Local time
Today, 09:36
Joined
Apr 13, 2007
Messages
28
I did a search both here and google. I found previous discussions and tried to implement what I learned from them but my problem continues...

I have a text box in a subform to a form. When I enter information in this textbox, I want it to become the default value for the next new record.

I have entered the following code in two places (in the subform's afterupdate event and in the control's lost focus event) with the same no result.

me.txtOrderNumber.DefaultValue=me.txtOrderNumber

The code executes.

When I go to the control's properties I can see that the default value has been changed as I directed it via code.

However, when I create a new record with the appropriate button on the main form, the displayed value of the textbox is blank.

Thanks for your help.
 
do you have any other default value at the table level? say ""
 
wreck,
in the "OnExit" of your subformtextbox;
Forms![yourmainformname]![textbox]=Forms![yourmainformname]![yoursubformname]![textboxofsubform]
jim
 
do you have any other default value at the table level? say ""

no. default value at the table level is blank. no quotation marks, just blank.

Also, remember that when I check the text box's property sheet, it shows the correct default value, ie the one I changed it to via code...
 
Also, sometimes the form just gets corrupt. Try importing everything into a new, blank database and see if that helps.
 
Also, sometimes the form just gets corrupt. Try importing everything into a new, blank database and see if that helps.



it's a bound textbox.

If I import, you're saying build the form again from scratch, right?

I guess I'm reading what you wrote two possible ways:

1. export data to blank database, then build new form
2. new blank database, export form to database.

I'm hoping you meant #2....Am I right?

thanks.
 
you're close...

Create blank new mdb file by selecting File New and doing a new database. Then, not EXPORT, but while in the new database go into File > Get External Data > Import and then select everything from your old file and bring it into the new one.
 
you're close...

Create blank new mdb file by selecting File New and doing a new database. Then, not EXPORT, but while in the new database go into File > Get External Data > Import and then select everything from your old file and bring it into the new one.

ok. did that.

default value does not change. additionally, code no longer changes default value of text box.

If I didn't say this, the textbox is in a subform to the mainform. Does that change anything?

thanks.
 
i don't think you can do this - you need to save the last value in a variable, and then assign the variable in the on current method of a new record

i have seen solutions that set/retrieve stored values in a fields tag property to do this in an elegant way.

Access cookbook (O'Reilly) has a demo form that has little command buttons next to each fields that enable to turn on/turn off this carry forward behaviour, and explains this in full.

hope this helps
 
I have set the default via code before and just recently did a test to see if it would work. Can you post your database (make sure to compact and repair by selecting Tools > Database Tools > Compact and Repair) and the use WinZip or something to zip it. The file would need to be 393Kb or less in size AFTER zipping. If it is too big, I can PM you my email address.
 
I have set the default via code before and just recently did a test to see if it would work. Can you post your database (make sure to compact and repair by selecting Tools > Database Tools > Compact and Repair) and the use WinZip or something to zip it. The file would need to be 393Kb or less in size AFTER zipping. If it is too big, I can PM you my email address.

Yes. it will take me a couple days as I'm going out of town and won't get it done before I get back. Should be posted around Friday/Saturday time frame.
 
Which form and control(s) are you trying to set the default for?
 
sorry bout that.

It's the frmPartsInformation.

textbox is txtOrderNumber
 
frmPartsInformation is a subform on the frmMaintenanceEntries isn't it?
 
Okay, this should work for you. In the AfterUpdate event of the OrderNumber text box put this code:

Code:
    If txtOrderNumber <> txtOrderNumber.DefaultValue Then
        txtOrderNumber.DefaultValue = txtOrderNumber
    End If

It works for me in my tests. Don't put anything like Me.txtOrderNumber, but leave it as I have written.
 
Err guys, coming in on the end of this would it not be easier to change the default of the underlying table to this set value ?
then when new recorded added it would look at this value and use it ??or have I lost the plot - pretty much like the get next number routine ??


just a thought or should I keep my nose out of this one as there are some brains involved in this already

g
 
just read bobs solution -- I'll shut up and keep quiet
 
Okay, this should work for you. In the AfterUpdate event of the OrderNumber text box put this code:

Code:
    If txtOrderNumber <> txtOrderNumber.DefaultValue Then
        txtOrderNumber.DefaultValue = txtOrderNumber
    End If

It works for me in my tests. Don't put anything like Me.txtOrderNumber, but leave it as I have written.

Way cool for the assist. I'll add the code tonight when I get home. Any thoughts on why me.txtordernumber won't work?
 

Users who are viewing this thread

Back
Top Bottom