Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy question about changing default value in text box 1

Status
Not open for further replies.

emik

MIS
Jul 24, 2006
80
CA
I have a text box with a default value. I want the user to be able to type in a new value and save it as the default value of that text box so that the next time the form is opened it will have the previous path.

I have this code:

Dim default_path As String
default_path = txtDir.Value
txtDir.DefaultValue = default_path

I'm sure I'm missing something simple here. Thanks for your help.
 
Do you just need to save the form when the user closes it?

DoCmd.Close acForm, Me.Name, acSaveYes

Of course the user will need the correct permissions to save the changes.

Ed Metcalfe.

Please do not feed the trolls.....
 
No I don't need to save the form, I only need to change the default value of the text box.

In the text box properties there "default value" I have it currently set to "C:\Program Files". I use it so that when an open dialog box opens to goes to that path. If the user changes that path to "C:\" I want them to be able to save it, so everytime they open the box they don't have to navigate (it will usually be a static location).

Thanks
 
You may want to think about saving that default value to a table in your database. The Default Value property of a textbox is a design time setting...therefore, in order to save it, you would need to allow the users to save the change to the form itself.
 
Ok I understand, thanks for your help!
 
Hi!

Me!YourTextBox.DefaultValue = """" & "C:\" & """"

will change it, but I don't think it will stick unless you put the form into design view first so you have to run the code from another form.

hth


Jeff Bridgham
Purdue University
Graduate School
Data Analyst
 
I tried to put it into a table as suggested, so the datasource is a field in the table. When the user clicks the "Save" button it opens a recordset to save the record:

Dim edit_default_path As Recordset
Set edit_default_path = CurrentDb.OpenRecordset("table")

With edit_default_path
.Edit
!default_path = txtDir.Value
.Update
End With

Set edit_default_path = Nothing


But I get an error:

Write conflict: This record has been changed by another user since you started editing it. If you save the changes you will overwrite the changes the other user made.

-I'm the only one using the program, it's on my C:

Any ideas?
 
Have you the table open in a form? If you edit a record on a form and also in a recordset, you will get this problem.
 
Yes the table is open because it is the datasource for various text boxes.

The solution I used was to create a new table and have a field "Default_path". On form load it opens that recordset to populate the text box and whenever the user saves it just updates that record.

My only other question is, is there a way to verify if the user entered a valid path? Like if they type in something that doesn't exist my open dialog will automatically go to "My Documents" but I would like to be able to tell the user "The Path you entered is invalid".

My open dialog is simply:

Dim strFileName As String
Dim xlApplication As Excel.Application
Dim strInitDir As String 'Default location for open dialog

Set xlApplication = New Excel.Application

If Len(Form_frm_Main_Menu.txtDir) > 0 Then
strInitDir = Form_frm_Main_Menu.txtDir.Value
Else
strInitDir = "C:\DATA"
End If

xlApplication.DefaultFilePath = strInitDir
Set xlApplication = Nothing
Set xlApplication = New Excel.Application

strFileName = xlApplication.GetOpenFilename("Excel Files (*.xls), *.xls")



 
You can use Dir:
Dir("C:\DATA", vbDirectory)
 
Say the user enters 'C:\NoData' in txtDir, in an appropriate event, perhaps Before Update, you can use Dir to test if this directory exists:

[tt]If Dir(Me.txtDir,vbDirectory)="" Then
msgbox "Invalid directory"
Else
'whatever
End If[/tt]

 
I was looking for a way to do this awhile ago, thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top